My Base Java Window Code
“Base code and personal code libraries are allowed, but should be declared and shared with the community prior to beginning your entry. To do this, make a blog post.” – LD Rules
The rules told my to do that.
My Basic Java Gamewindow (which i would use for every Game):
Main.java:
public class Main {
public static void main(String[] args) {
Game.initiate();
}
}
Game.java:
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
public class Game extends Canvas implements Runnable{
private static final long serialVersionUID = 1L;
private JFrame jframe;
private boolean paused = false;
private boolean running;
private int FPS = 0;
private int TICKS = 0;
public static final int WIDTH = 800;
public static final int HEIGHT = WIDTH/16*9;
public static final int SCALE = 1;
public static final String NAME = “Game Name”;
public static final String VERSION = “0.0_1 pre-alpha”;
public static Game gameInstance;
public static Game getGame(){
return gameInstance;
}
public static Game initiate(){
gameInstance = new Game();
gameInstance.start();
return gameInstance;
}
public Game(){
Canvas canvas = this;
canvas.setMinimumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
canvas.setMaximumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
canvas.setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
//canvas.addKeyListener(inputListener);
//canvas.addMouseListener(inputListener);
//canvas.addMouseMotionListener(inputListener);
//canvas.addMouseWheelListener(mainMouseListener);
this.jframe = new JFrame(NAME + ” ” + VERSION);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setLayout(new BorderLayout());
jframe.add(canvas, BorderLayout.CENTER);
//jframe.setUndecorated(true);
//jframe.setExtendedState(JFrame.MAXIMIZED_BOTH);
jframe.setResizable(false);
jframe.setVisible(true);
jframe.pack();
jframe.setLocationRelativeTo(null);
}
public int tickCount = 0;
private boolean sysStats = true;
public boolean isSysStats() {
return sysStats;
}
public void setSysStats(boolean sysStats) {
this.sysStats = sysStats;
}
public synchronized void start(){
this.running = true;
new Thread(this).start();
}
public synchronized void stop(){
this.running = false;
}
@Override
public void run() {
long lastTime = System.nanoTime();
double nsPerTick = 1000000000D/60; // 60 = MaxFrames
int ticks = 0;
int frames = 0;
long lastTimer = System.currentTimeMillis();
double delta= 0;
init();
while(this.running){
long now = System.nanoTime();
delta += (now – lastTime) / nsPerTick;
lastTime = now;
boolean shouldRender = false;
while (delta >= 1){
if(!paused){
ticks++;
tick();
}
delta -= 1;
shouldRender = true;
}
try {
Thread.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(shouldRender){
frames++;
render();
}
if(System.currentTimeMillis() – lastTimer >= 1000){
lastTimer += 1000;
this.FPS = frames;
this.TICKS = ticks;
frames = 0;
ticks = 0;
}
}
System.exit(0);
}
private void init() {
//GamePlayer player = new GamePlayer(“Powerman3540”);
//Handlers.getHandler().playerHandler.setActivePlayer(player);
//Handlers.getHandler().ingamePlayerTeamHandler.registerPlayerTeam(new PlayerTeam(1, player, “Black Label”));
}
public void render(){
BufferStrategy bs = getBufferStrategy();
if(bs == null){
createBufferStrategy(3);
return;
}
Graphics2D g = (Graphics2D) bs.getDrawGraphics();
g.setColor(Color.BLACK);
g.fillRect(0, 0, getWidth(), getHeight());
//float scale = (getWidth()*getHeight())/(2052000F/2);
g.scale(1, 1);
draw(g);
if(this.sysStats)drawSysStats(g);
g.dispose();
bs.show();
}
private void drawSysStats(Graphics2D g){
g.setColor(Color.BLACK);
g.drawString(“FPS: ” + this.FPS + ” | TICK/PS ” + this.TICKS, 11, 11);
g.setColor(Color.WHITE);
g.drawString(“FPS: ” + this.FPS + ” | TICK/PS ” + this.TICKS, 10, 10);
}
public void tick(){
this.tickCount ++;
}
private void draw(Graphics2D g) {
//Background
g.setColor(Color.DARK_GRAY);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
}
public BufferedImage getImage(Class classx, String path){
BufferedImage img;
try {
img = ImageIO.read(new File(path));
} catch (IOException e) {
img = null;
System.out.println(“‘” + classx.getName() + “‘ : ” + path + ” – Do not exist!”);
}
return img;
}
public Rectangle getScreenRect(){
return new Rectangle(0, 0, WIDTH, HEIGHT);
}
}

