0% found this document useful (0 votes)
16 views

SRC

The document defines a Game class that extends Canvas and implements Runnable. The Game class initializes a JFrame, creates a BufferedImage, and contains methods for starting the game loop, ticking, rendering graphics, and running the main thread.

Uploaded by

Bryan Afolou
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

SRC

The document defines a Game class that extends Canvas and implements Runnable. The Game class initializes a JFrame, creates a BufferedImage, and contains methods for starting the game loop, ticking, rendering graphics, and running the main thread.

Uploaded by

Bryan Afolou
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

package graficos;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;

public class Game extends Canvas implements Runnable{

private Thread thread;


private boolean isRunning;
public static JFrame frame;
private final int WIDTH = 160;
private final int HEIGHT = 120;
private final int SCALE = 4;

private BufferedImage image;

public Game() {
this.setPreferredSize(new Dimension(WIDTH*SCALE,HEIGHT*SCALE));
initiFrame();
image = new BufferedImage(WIDTH, HEIGHT,BufferedImage.TYPE_INT_RGB);
}
public void initiFrame() {
frame = new JFrame("TELINHA DO GAME #1");
frame.add(this);
frame.setResizable(false);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

public synchronized void start() {


thread = new Thread(this);
isRunning = true;
thread.start();
}

public synchronized void stop() {

public void tick() {

public void render() {


BufferStrategy bs = this.getBufferStrategy();
if(bs == null) {
this.createBufferStrategy(3);
return;
}
Graphics g = image.getGraphics();
g.setColor(Color.cyan);
g.fillRect(0, 0, WIDTH, HEIGHT);

g.setColor(Color.yellow);
g.fillOval(0, 0, 30, 30);
g.setColor(Color.green);
g.fillRect(0, 80, 165, 70);
g.setColor(Color.white);
g.fillOval(120, 10, 30, 15);
g.setColor(Color.white);
g.fillOval(80, 10, 30, 15);
g.setColor(Color.white);
g.fillOval(40, 10, 30, 15);
g = bs.getDrawGraphics();
g.drawImage(image,0,0,WIDTH*SCALE,HEIGHT*SCALE,null);
bs.show();
}

public static void main(String args []) {


Game game = new Game();
game.start();
}

public void run() {


long lastTime = System.nanoTime();
double amountTicks = 60.0;
double ns = 1000000000 / amountTicks;
double delta = 0;
int frame = 0;
double timer = System.currentTimeMillis();
while(isRunning) {
long now = System.nanoTime();
delta+=( now - lastTime) / ns;
lastTime = now;
if(delta >= 1) {
tick();
render();
frame++;
delta--;
}

if(System.currentTimeMillis() - timer >= 1000) {


System.out.println("FPS: "+frame);
frame = 0;
timer+=1000;

}
}
}

You might also like