Reputation: 3
I am following a youtube guide and there are no errors in the code however, when I run the program in the console I have 'running' (I put running loop forever to make sure that the game is working) however no window is popping up for the display. Here is the code
import java.awt.Canvas;
import java.awt.Dimension;
import javax.swing.JFrame;
public class Game extends Canvas implements Runnable {
private static final long serialVersionUID = 1L;
public static int width = 300;
public static int height = width / 16 * 9;
public static int scale = 3;
private Thread thread;
private JFrame frame;
private boolean running = false;
public Game(){
Dimension size = new Dimension(width * scale, height * scale);
setPreferredSize(size);
frame = new JFrame();
}
public synchronized void start() {
} {
running = true;
thread = new Thread(this, "Display");
thread.start();
}
public synchronized void stop () {
running = false;
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void run(){
while(running){
System.out.println("running");
}
}
public static void main(String[] args){
Game game = new Game();
game.frame.setResizable(false);
game.frame.setTitle("Go Home");
game.frame.add(game);
game.frame.pack();
game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game.frame.setLocationRelativeTo(null);
game.setVisible(true);
game.start();
}
}
Upvotes: 0
Views: 105
Reputation: 347314
Okay, so here's a lesson, don't create an instance of JFrame
inside your component classes (unless you component class is actually going to use it)
game.setVisible(true);
is only setting the visibility state of the Canvas
, not the frame
Instead, make the JFrame
inside your main
method instead
import java.awt.Canvas;
import java.awt.Dimension;
import javax.swing.JFrame;
public class Game extends Canvas implements Runnable {
private static final long serialVersionUID = 1L;
public static int width = 300;
public static int height = width / 16 * 9;
public static int scale = 3;
private Thread thread;
private boolean running = false;
public Game() {
Dimension size = new Dimension(width * scale, height * scale);
setPreferredSize(size);
}
public synchronized void start() {
running = true;
thread = new Thread(this, "Display");
thread.start();
}
public synchronized void stop() {
running = false;
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void run() {
while (running) {
System.out.println("running");
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame("Go Home");
Game game = new Game();
frame.setTitle("Go Home");
frame.add(game);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
game.start();
}
});
}
}
Also, your start
method is actually empty...
public synchronized void start() { } /* What's this? */ { running = true; thread = new Thread(this, "Display"); thread.start(); }
and the thread is actually been started through the static initialisation of the class, not the method call
Upvotes: 3
Reputation: 132
The JFrame is the main window that will contain components. It must be set visible. On your JFrame object:
frame.setVisible(true);
Upvotes: 1