0

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.

Will Fisher
  • 413
  • 5
  • 18
  • 2
    1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Nov 08 '16 at 01:19
  • 1
    3) As an aside, I suspect the problem is related to multiple frames and `EXIT_ON_CLOSE`, but for more than suspicions, add an MCVE/SSCCE as an [edit] to the question. – Andrew Thompson Nov 08 '16 at 01:21

0 Answers0