I have the following code that I use in an attempt to create a JFrame:
public class Window {
private JFrame frame;
private Canvas canvas;
private BufferedImage image;
private Graphics g;
private BufferStrategy bs;
public Window(GameContainer gc) {
image = new BufferedImage(gc.getWidth(), gc.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
canvas = new Canvas();
Dimension s = new Dimension((int)(gc.getWidth() * gc.getScale()), (int)(gc.getHeight() * gc.getScale()));
//Why two of same lines?
canvas.setPreferredSize(s);
canvas.setMaximumSize(s);
canvas.setPreferredSize(s);
frame = new JFrame(gc.getTitle());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(canvas, BorderLayout.CENTER);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
canvas.createBufferStrategy(1);
bs = canvas.getBufferStrategy();
g = bs.getDrawGraphics();
}
public void update() {
g.drawImage(image, 0, 0, canvas.getWidth(), canvas.getHeight(), null);
bs.show();
}
}
Whenever I run my project in eclipse (my main method is in another class; I didn't forget one), the JFrame shows up then closes almost immediately. I am hoping one of ya'll can direct me to my error. I also have another class which is essentially a game loop that calls window.update()
every frame, if that would be causing any issues. If the issue cannot be found here I can attach code from some other related classes, however at the time being I don't think they are the issue and I don't want to bog down the post with unnecessary code.