// Here's my code:
Main Class:
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
public class Display extends Canvas implements Runnable{
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension dim = toolkit.getScreenSize();
public static int WIDTH;
public static int HEIGHT;
public static final String title = "First Person Game";
public static Thread thread;
public static Screen screen;
public static BufferedImage img;
public static boolean running = false;
public static int[] pixels;
public static Render render;
public Display(){
WIDTH = dim.width;
HEIGHT = dim.height;
screen = new Screen(WIDTH, HEIGHT);
img = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
pixels = ((DataBufferInt)img.getRaster().getDataBuffer()).getData();
}
private void start(){
if(running){
return;
}else{
running = true;
thread = new Thread(this);
thread.start();
}
}
private void stop(){
if(!running){
return;
}else{
running = false;
try{
thread.join();
}catch(Exception x){
System.exit(0);
}
}
}
public void run(){
while(running){
render();
}
}
public void render(){
BufferStrategy bs = this.getBufferStrategy();
if(bs == null){
createBufferStrategy(3);
return;
}
screen.render();
for(int i = 0; i < WIDTH * HEIGHT; i++){
pixels[i] = screen.pixels[i];
}
Graphics g = bs.getDrawGraphics();
g.drawImage(img, 0, 0, WIDTH, HEIGHT, null);
g.dispose();
bs.show();
}
public static void main(String args[]){
JFrame frame = new JFrame();
BufferedImage cursorImg = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
Cursor blankCursor = Toolkit.getDefaultToolkit().createCustomCursor(cursorImg, new Point(0, 0), "blank cursor");
frame.getContentPane().setCursor(blankCursor);
Display game = new Display();
frame.setUndecorated(true);
frame.add(game);
frame.setTitle(title);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(WIDTH, HEIGHT);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
fps f = new fps();
game.start();
}
}
The class I need to call:
public class fps{
public void fps(){
System.out.println("Test 1 successful.");
int frames = 0;
double unprocessedSeconds = 0;
long previousTime = System.nanoTime();
double secondsPerTick = 1 / 60.0;
int tickCount = 0;
boolean ticked = false;
Display c = new Display();
System.out.println("Test 2 successful.");
c.render();
long currentTime = System.nanoTime();
long passedTime = currentTime - previousTime;
previousTime = currentTime;
unprocessedSeconds += passedTime / 1000000000.0;
while(unprocessedSeconds > secondsPerTick){
System.out.println("Test 3 successful.");
tick();
unprocessedSeconds -= secondsPerTick;
ticked = true;
tickCount++;
if(tickCount % 60 == 0){
System.out.println(frames + " fps");
previousTime += 1000;
frames = 0;
}
}
if(ticked){
c.render();
frames++;
}
c.render();
frames++;
}
public void tick(){}
}
/* I don't know how to do this, I've been trying all sorts of things. I basically need *to make sure fps is printing into the console while display is running. I can't seem to so */ this. I tried in method run() but it just wouldn't call it.
I beleive you need to create an instance of the fps class in your main class, and then call the method through it.
fps nameOfClassInstance = new fps(//don't forget anything you need to send for constructor);
fps.run();
At least this is how it is in C# which I know is very similar to java.
if this doesn't help,then I beleive it is a threading issue, and you need to make sure you are handling the threads properly. ( I can't help with this issue, I am still green to java threading.)
It is a little strange to have a method with the same name as your class. Is it meant to be a constructor, like this?
public class fps{
//Note that the void is removed here
public fps(){
System.out.println("Test 1 successful.");
int frames = 0;
double unprocessedSeconds = 0;
long previousTime = System.nanoTime();
double secondsPerTick = 1 / 60.0;
int tickCount = 0;
boolean ticked = false;
Display c = new Display();
System.out.println("Test 2 successful.");
c.render();
long currentTime = System.nanoTime();
long passedTime = currentTime - previousTime;
previousTime = currentTime;
unprocessedSeconds += passedTime / 1000000000.0;
while(unprocessedSeconds > secondsPerTick){
System.out.println("Test 3 successful.");
tick();
unprocessedSeconds -= secondsPerTick;
ticked = true;
tickCount++;
if(tickCount % 60 == 0){
System.out.println(frames + " fps");
previousTime += 1000;
frames = 0;
}
}
if(ticked){
c.render();
frames++;
}
c.render();
frames++;
}
public void tick(){}
}
I would also suggest that all that code is not really appropriate for a constructor and should be moved to its own method. Then, as mentioned by others, you can call your method after first creating an fps object.
There's an error in your code:
private void start(){
if(running){
return;
}else{
running = true;
thread = new Thread(this);
thread.start();
}
}
Threads are started by calling the start method. What you've done is to change the start() method. You should use the run method instead of the start method, as Thread.start() is meant for starting a thread while run() is where you are supposed to put the execution code.
As from the JavaDoc of the Java API, http://docs.oracle.com/javase/6/docs/api/java/lang/Thread.html#start%28%29
public void start()
Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.
Well, you can do one of two things. You can either declare an fps object:
fps myfps = new fps();
myfps.fps();
or you can make it all static:
public static void fps(){
...
}
public static void tick(){}
//elsewhere
fps.fps()
Related
I have this code from this tutorial https://youtu.be/Nn8LH6T3xuc
and I can't understand why is running so slow.
There is a thread that handles the game loop and renders the graphics on screen.
Before the implementation of the render() method it runs nice..
There is a thread that handles the game loop and renders the graphics on screen.
public class Game extends Canvas implements Runnable {
public static final int WIDTH = 420;
public static final int HEIGHT = WIDTH / 12 * 9;
public static final int SCALE = 2;
private boolean running = false;
private Thread thread;
private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
#Override
public void run() {
long lastTime = System.nanoTime();
final double amountOfTicks = 60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
int updates = 0;
int frames = 0;
long timer = System.currentTimeMillis();
while(running) {
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
if(delta >= 1) {
tick();
updates++;
delta--;
}
render();
frames++;
if(System.currentTimeMillis() - timer > 1000) {
timer += 1000;
System.out.println(updates + " Ticks, FPS "+ frames);
updates = 0;
}
}
stop();
}
private void tick() { }
private void render() {
BufferStrategy bs = this.getBufferStrategy();
if(bs == null) {
createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
//////////////////////////////////
g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
//////////////////////////////////
g.dispose();
bs.show();
}
public static void main(String[] args) {
//jframe creation etc...
game.start();
}
public synchronized void start() {
if(running) return;
running = true;
thread = new Thread(this);
thread.start();
}
public synchronized void stop() {
if(!running) return;
running = false;
try {
thread.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.exit(1);
}
}
My problem is, I have a game with a random generated map and it has only 30-40 fps because of the number of blocks.(You can imagine my game like a 2d minecraft).
First I search for the first tile what is in screen. Then start a loop render tile next tile... until I reach the last tile what you can see.
(I don't use any of the Java classes like graphics/graphics2d I use my own code what is an int[] with the rows of teh screen in it and when I render a tile I change the int[x+y*width] position of the screen to the correct pixel of the block)
I think logically this is the best way to render my map and i don't understend why is the low fps. I am wrong or I need to search for some other problem in my code? Or there is any better rendering method?
If I skip the rendering of the world, there is stabile 120 fps what is capped there. What can be the problem?
I know you dont use the Gaphics functions and choose to manipulate a pixel-array instead.
Try to change your game to use the Graphics object since there is no (easy and efficient) way around it. If you still choose not to do so, try to add
System.setProperty("sun.java2d.opengl", "true");
at the very begining of your code just after
public static void main(String[] args) {
I tried to do it the same way as you back when i first made simple games but you later come to realize that the built-in Graphics functions are vastly superior in performance and ease of use.
EDIT:
A short explanaition on how a basic game might use the Graphics object:
Suppose you created a JFrame. If you then add a class that extends from Canvas and add that to it. If you want to use a menu, you might even create a JPanel first and add the Canvas into the Jpanel so you can hide it more easily.
Here you have an example how we create a usable canvas:
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
public class Game extends Canvas implements Runnable{
private static final long serialVersionUID = 1L;
public static final int WIDTH = 1920;
public static final int HEIGHT = WIDTH * 9 / 16;
public static final String TITLE = "YOUR GAMES NAME";
public static final int TICKSPERS = 120;
public static final boolean ISFRAMECAPPED = false;
public static JFrame frame;
private Thread thread;
private boolean running = false;
public int frames;
public int lastFrames;
public int ticks;
public Game(){
Dimension size = new Dimension(WIDTH, HEIGHT);
setPreferredSize(size);
setMaximumSize(size);
setMinimumSize(size);
}
public void render(){
frames++;
BufferStrategy bs = getBufferStrategy();
if (bs == null){
createBufferStrategy(2);
return;
}
Graphics g = bs.getDrawGraphics();
g.setColor(new Color(79,194,232));
g.fillRect(0, 0, getWidth(), getHeight());
//Call your render funtions from here
g.setColor(Color.BLACK);
g.fillRect(120,70,35,90);
g.dispose();
bs.show();
}
public void tick(){
}
public synchronized void start(){
if(running) return;
running = true;
thread = new Thread(this, "Thread");
thread.start();
}
public synchronized void stop(){
if(!running) return;
running = false;
try {
System.exit(1);
frame.dispose();
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void init(){
}
public void run() {
init();
//Tick counter variable
long lastTime = System.nanoTime();
//Nanoseconds per Tick
double nsPerTick = 1000000000D/TICKSPERS;
frames = 0;
ticks = 0;
long fpsTimer = System.currentTimeMillis();
double delta = 0;
boolean shouldRender;
while(running){
shouldRender = !ISFRAMECAPPED;
long now = System.nanoTime();
delta += (now - lastTime) / nsPerTick;
lastTime = now;
//if it should tick it does this
while(delta >= 1 ){
ticks++;
tick();
delta -= 1;
shouldRender = true;
}
if (shouldRender){
render();
}
if (fpsTimer < System.currentTimeMillis() - 1000){
System.out.println(ticks +" ticks, "+ frames+ " frames");
ticks = 0;
lastFrames = frames;
frames = 0;
fpsTimer = System.currentTimeMillis();
}
}
}
public static void main(String[] args){
Game game = new Game();
frame = new JFrame(TITLE);
frame.add(game);
frame.pack();
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
game.start();
}
}
This class can be your base. Its tick method is called 120 times a second and its render method can render stuff onto the screen.
If you are not familiar with the functions of the Graphics Object i suggest reading a little about them.
You can't reach the Graphics object from the outside. You need to call the render functions from inside the games render function before the Graphics object gets disposed. Try to seperate your game logic from the render functions.
I use a slightly different architecture than what's stated above. I actually create a separate Renderer object which is basically a deferred renderer.
It's structured like this
public class App {
JFrame window;
Renderer renderer;
Engine engine; //implementation is a nested class within App
Dimension window_dimension; //stored for later use
public App() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
window = new JFrame("MyGame");
boolean full_screen = true;
window_dimension = initializeWindow(window, full_screen);
renderer = new Renderer(window_dimension);
window.add(renderer);
engine = new Engine(renderer);
engine.start();
}
});
}
}
Renderer.java :
import java.awt.*;
import java.util.concurrent.CopyOnWriteArrayList;
public class Renderer extends JPanel {
Dimension dim;
private CopyOnWriteArrayList<Drawable> drawables = new CopyOnWriteArrayList<Drawable>();
Renderer(Dimension dim) {
this.dim = dim;
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.clearRect(0, 0, getWidth(), getHeight());
for (Drawable drawable : drawables) {
drawable.paint(g2d);
}
g2d.dispose();
drawables.clear();
}
public synchronized void render() {
repaint();
}
public synchronized void submit(Drawable drawable) {
drawables.add(drawable);
}
public synchronized void submitBackground(Drawable drawable) {
drawables.add(0,drawable);
}
}
Drawable.java :
import java.awt.*;
abstract class Drawable {
protected Stroke stroke;
protected Color color, stroke_color;
public Dimension size;
public float sub_pixel_x;
public float sub_pixel_y;
public Drawable(Color color) {
setColor(color);
setStrokeColor(new Color(0));
sub_pixel_x = 0.0f;
sub_pixel_y = 0.0f;
size = new Dimension(10, 10);
}
public void setStroke(float width) {
stroke = new BasicStroke(width);
}
public void noStroke() {
stroke = null;
}
public void setColor(Color color) {
this.color = color;
}
public void setStrokeColor(Color color) {
this.stroke_color = color;
}
public void setLocation(float x, float y) {
sub_pixel_x = x;
sub_pixel_y = y;
}
protected abstract void paint(Graphics2D g2d);
}
AbstractEngine.java :
import java.awt.*;
abstract class AbstractEngine implements Runnable {
Renderer renderer;
Dimension dimension;
boolean running;
Thread thread;
public AbstractEngine(Renderer renderer) {
this.renderer = renderer;
dimension = renderer.dim;
}
public void start() {
if (running) return;
running = true;
thread = new Thread(this, "Tread");
thread.start();
}
public void stop() {
if(!running) return;
running = false;
try {
System.exit(1);
thread.join();
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
public abstract void handleInput();
public abstract void update();
public abstract void render();
#Override
public void run() {
final int UPS = 120;
final int FPS = 60;
long initialTime = System.nanoTime();
final double timeU = 1000000000 / UPS;
final double timeF = 1000000000 / FPS;
double deltaU = 0, deltaF = 0;
int frames = 0, ticks = 0;
long timer = System.currentTimeMillis();
while (running) {
long currentTime = System.nanoTime();
deltaU += (currentTime - initialTime) / timeU;
deltaF += (currentTime - initialTime) / timeF;
initialTime = currentTime;
if (deltaU >= 1) {
handleInput();
update();
ticks++;
deltaU--;
}
if (deltaF >= 1) {
render();
renderer.render();
frames++;
deltaF--;
}
if (System.currentTimeMillis() - timer > 1000) {
frames = 0;
ticks = 0;
timer += 1000;
}
}
}
}
Input is handled in the extended Engine class. I have a MouseState object that holds mouse positions and button state. I then create a MouseAdapter that updates the date in MouseState and attach it to the renderer. It's a bit weird, I know, but works nicely.
I have this code for a game called Wave, and normally when I run it, it should be a black window with white squares in it. But the window is white, with a very thin black stripe on the left of the window. I can barely see it.
Does anybody have any idea on why would this happen?
package wave.myFirstGame;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.util.Random;
public class Game extends Canvas implements Runnable {
private static final long serialVersionUID = 3580879553502102315L;
public static final int WITDH = 640, HEIGHT = WITDH / 12 * 9;
private Thread thread;
private boolean running = false;
private Random r;
public Handler handler;
public Game() {
new Window(WITDH, HEIGHT, "Wave", this);
handler = new Handler();
r = new Random();
for(int i = 0; i < 50; i++){
handler.addObject(new Player(r.nextInt(WIDTH), r.nextInt(HEIGHT), ID.Player));
}
handler.addObject(new Player(200, 200, ID.Player));
}
public synchronized void start() {// initializing the thread
thread = new Thread(this);
thread.start();
running = true;
}
public synchronized void stop() {
try {
thread.join();
running = false;
} catch (Exception e) {
e.printStackTrace();
}
}
// GAME LOOP
public void run() {
long lastTime = System.nanoTime();
double amountOfTicks = 60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
long timer = System.currentTimeMillis();
int frames = 0;
while (running) {
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
while (delta >= 1) {
tick();
delta--;
}
if (running)
render();
frames++;
if (System.currentTimeMillis() - timer > 1000) {
timer += 1000;
System.out.println("FPS " + frames);
frames = 0;
}
}
stop();
}
private void tick(){
handler.tick();
}
private void render(){
BufferStrategy bs = this.getBufferStrategy();
if(bs == null){
this.createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.setColor(Color.black);
g.fillRect(0, 0, WIDTH, HEIGHT);
handler.render(g);
g.dispose();
bs.show();
}
public static void main(String[] args) {
new Game();
}
}
Have a look into the API of Canvas. There you will find:
Fields inherited from interface java.awt.image.ImageObserver
[...] HEIGHT, [...], WIDTH
So since you extend your class from Canvas class you already have WIDTH and HEIGHT constants and for some reason WIDTH seams to have value 1.
So simply rename your constants and it will display as expected.
I have designed a code for a game. The problem is that the background won't change to any of my selected colors I have picked from graphics color library.
I need someone to figure this out with the code i have provided (please don't make a whole new code). idk why java/ eclipse won't display it??? am i missing something?? The program here should display a GUI with a background color blue. instead i get white.
public class MainApp extends Canvas implements Runnable {
private static final long serialVersionUID = 8928635543572788908L;
private static final int WIDTH= 648, HEIGHT= WIDTH/ 12 * 9;
private Thread thread;
private boolean running= false;
public MainApp()
{
new Window(WIDTH, HEIGHT, "App", this);
}
public synchronized void start()
{
thread= new Thread(this);
thread.start();
running= true;
}
public void run()
{
long lastTime= System.nanoTime();
double amountOfTicks= 60.0;
double ns= 1000000000 / amountOfTicks;
double delta= 0;
long timer= System.currentTimeMillis();
int frames= 0;
while(running){
long now= System.nanoTime();
delta += (now- lastTime) / ns;
lastTime= now;
while(delta >= 1){
tick();
delta--;
}
if(running)
render();
frames++;
if(System.currentTimeMillis() - timer > 1000)
{
timer += 1000;
System.out.print("FPS: " + frames);
frames= 0;
}
}
stop();
}
public synchronized void stop()
{
try
{
thread.join();
running= false;
}catch(Exception e){e.printStackTrace();}
}
public void tick()
{
}
public void render()
{
BufferStrategy bs= this.getBufferStrategy();
if(bs== null)
{
this.createBufferStrategy(3);
return;
}
Graphics g= bs.getDrawGraphics();
g.setColor(Color.BLUE);
g.fillRect(0, 0, WIDTH, HEIGHT);
g.dispose();
bs.show();
}
public static void main(String[] args) {
new MainApp();
}
}
Your codes a little messed up, you shouldn't be making a new instance of Window from MainApp, the Window should be creating it (IMHO).
Also, you should be overriding the getPreferredSize method the the MainApp, as this is what should be controlling the viewable size of the window, this way, when you use pack on the JFrame, it will ensure that the window is larger then the preferredSize of it's contents, allowing the frame decorations to wrap around the outside of it.
BUT, the main problem you have, is adding the MainApp to the JFrame AFTER it's already been made visible
The following works for me...
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
public class MainApp extends Canvas implements Runnable {
private static final long serialVersionUID = 8928635543572788908L;
private static final int WIDTH = 648, HEIGHT = WIDTH / 12 * 9;
private Thread thread;
private boolean running = false;
public MainApp() {
new Window("App", this);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(WIDTH, HEIGHT);
}
public synchronized void start() {
thread = new Thread(this);
thread.start();
running = true;
}
public void run() {
long lastTime = System.nanoTime();
double amountOfTicks = 60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
long timer = System.currentTimeMillis();
int frames = 0;
while (running) {
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
while (delta >= 1) {
tick();
delta--;
}
if (running) {
render();
}
frames++;
if (System.currentTimeMillis() - timer > 1000) {
timer += 1000;
System.out.print("FPS: " + frames);
frames = 0;
}
}
stop();
}
public synchronized void stop() {
try {
thread.join();
running = false;
} catch (Exception e) {
e.printStackTrace();
}
}
public void tick() {
}
public void render() {
BufferStrategy bs = this.getBufferStrategy();
if (bs == null) {
this.createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.setColor(Color.BLUE);
g.fillRect(0, 0, WIDTH, HEIGHT);
g.dispose();
bs.show();
}
public static void main(String[] args) {
new MainApp();
}
public static class Window {
private Window(String title, MainApp app) {
JFrame frame = new JFrame(title);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(app);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
app.start();
}
}
}
If you run the program, you can see that it prints "Run() method is called", when the run gets called. But the System.out.println() inside the if statement does not get called nor the render() method gets called.
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
public class Game extends Canvas implements Runnable {
public static int WIDTH = 300;
public static int HEIGHT = WIDTH / 16*9;
public static int SCALE = 3;
public static String TITLE = "";
private Thread thread;
private boolean running = false;
private JFrame frame;
public void start() {
if(running) return;
thread = new Thread(this);
thread.start();
}
public void stop() {
if(!running) return;
try{
thread.join();
}catch(InterruptedException e) {
e.printStackTrace();
}
}
public void run() {
System.out.println("Run() has been called");
long lastTime = System.nanoTime();
long timer = System.currentTimeMillis();
double ns = 1000000000.0 / 60.0;
double delta = 0;
int ticks = 0;
int fps = 0;
while(running) {
long now = System.nanoTime();
delta += (now-lastTime) / ns;
lastTime = now;
while(delta >= 1) {
tick();
ticks++;
delta--;
}
render();
fps++;
if(System.currentTimeMillis() - timer > 1000) {
timer += 1000;
System.out.println("Fps: " + fps + " Ticks: " + ticks);
fps = 0;
ticks = 0;
}
}
stop();
}
public void tick() {
}
public void render() {
BufferStrategy bs = getBufferStrategy();
if(bs==null) {
createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.fillRect(36, 25, 25, 25);
g.dispose();
bs.show();
}
public Game() {
setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
frame = new JFrame();
}
public static void main(String[] args) {
Game game = new Game();
game.frame.setResizable(false);
game.frame.setTitle("SPACE ADAPT PRE-ALPHA 0.001");
game.frame.add(game);
game.frame.pack();
game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game.frame.setLocationRelativeTo(null);
game.frame.setVisible(true);
game.start();
}
}
You never set running to true, then it's false. As a side note not related with this question but most of swing components methods are not thread-safe so calling in another thread that is not the Event Dispatch Thread would not work as you expected.
Read more Concurrency in Swing