Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.packag;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import java.awt.image.BufferedImage;
- public class Game extends JPanel implements KeyListener, Runnable {
- public static final int WIDTH = 400;
- public static final int HEIGHT = 630;
- public static final Font main = new Font("Bebas Neue Regular", Font.PLAIN, 28 );
- private Thread game;
- private boolean runnig;
- private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB );
- private GameBoard board;
- private long starTime;
- private long elapsed;
- private boolean set;
- public Game(){
- setFocusable(true);
- setPreferredSize(new Dimension(WIDTH, HEIGHT - GameBoard.BOARD_HEIGHT));
- addKeyListener(this);
- board = new GameBoard(WIDTH/2 - GameBoard.BOARD_WIDTH/2, HEIGHT);
- }
- private void update(){
- board.update();
- Keyboard.update();
- }
- private void render(){
- Graphics2D g = (Graphics2D) image.getGraphics();
- g.setColor(Color.white);
- g.fillRect(0, 0, WIDTH, HEIGHT);
- //render board
- board.render(g);
- g.dispose();
- Graphics2D g2d = (Graphics2D) getGraphics();
- g2d.drawImage(createImage(WIDTH, HEIGHT), 0, 0, null);
- g2d.dispose();
- }
- @Override
- public void run() {
- boolean shouldRender = false;
- int fps = 0, updates = 0;
- long fpsTimer = System.currentTimeMillis();
- double nsPerUpdate = 1000000000.0/60;
- //last update time in nanoseconds
- double then = System.nanoTime();
- double unprocessed = 0;
- while (runnig){
- double now = System.nanoTime();
- unprocessed += (now - then)/nsPerUpdate;
- then = now;
- }
- //update queue
- while (unprocessed >= 1){
- updates++;
- update();
- unprocessed--;
- shouldRender = true;
- }
- //render
- if (shouldRender) {
- fps++;
- render();
- shouldRender = false;
- }
- else{
- try {
- Thread.sleep(1);
- }catch (Exception e){
- e.printStackTrace();
- }
- }
- //fps timer
- if (System.currentTimeMillis() - fpsTimer > 1000){
- System.out.printf("%d fps %d updates", fps, updates);
- System.out.println();
- fps = 0;
- updates = 0;
- fpsTimer += 1000;
- }
- }
- public synchronized void start (){
- if(runnig) return;
- runnig = true;
- game = new Thread(this, "game");
- game.start();
- }
- public synchronized void stop(){
- if(!runnig) return;
- runnig = false;
- System.exit(0);
- }
- @Override
- public void keyTyped(KeyEvent e) {
- }
- @Override
- public void keyPressed(KeyEvent e) {
- Keyboard.keyPressed(e);
- }
- @Override
- public void keyReleased(KeyEvent e) {
- Keyboard.keyReleased(e);
- }
- }
Add Comment
Please, Sign In to add comment