Java window closing problem

Started by
2 comments, last by ms75214 1 year, 2 months ago

I created a Java program to open a window and do double buffering:

import java.awt.*;
import java.awt.image.BufferedImage;

public class DoubleBufferingExample extends Frame {
    private BufferedImage buffer;
    private Graphics bufferGraphics;

    public DoubleBufferingExample() {
        super("Double Buffering Example");
        setSize(300, 300);
        setVisible(true);
        buffer = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
        bufferGraphics = buffer.getGraphics();
    }

    public void update(Graphics g) {
        paint(bufferGraphics);
        g.drawImage(buffer, 0, 0, null);
    }

    public void paint(Graphics g) {
        // draw your image or animation here
        // using bufferGraphics instead of g
    }

    public static void main(String[] args) {
        new DoubleBufferingExample();
    }
}

However, when I try to close the window, it doesn't close. Anyone know how to fix this?

Advertisement

Google not working for you ?

Close frame.

I don't know why you use bare AWT Frame instead of more simple and convenient JFrame from Swing, but I suppose you have your reasons. In order to close awt window you have to add window listener in class constructor like this:

this.addWindowListener(new WindowAdapter() {

        public void windowClosing(WindowEvent evt) {
            System.exit(0);
        }

}); 

You can see this StackOverflow thread for more, but I think you should consider using swing in further development.

(You will need awt anyways, but parts that could be done by swing should be done with it in my opinion)

Do you like bad guys? Because I'm really bad. At everything.

@Maggistrator Okay. I decided to write an example using Swing:

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;

public class DoubleBufferingExample extends JPanel {
    private BufferedImage buffer;
    private Graphics2D bufferGraphics;
    
    public DoubleBufferingExample() {
        // Set up the JPanel
        setPreferredSize(new Dimension(400, 400));
        
        // Set up the double buffer
        buffer = new BufferedImage(400, 400, BufferedImage.TYPE_INT_RGB);
        bufferGraphics = buffer.createGraphics();
        
        // Draw something to the buffer
        bufferGraphics.setColor(Color.WHITE);
        bufferGraphics.fillRect(0, 0, 400, 400);
        bufferGraphics.setColor(Color.RED);
        bufferGraphics.fillOval(50, 50, 300, 300);
    }
    
    @Override
    protected void paintComponent(Graphics g) {
        // Copy the buffer to the screen
        g.drawImage(buffer, 0, 0, null);
    }
    
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame("Double Buffering Example");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(new DoubleBufferingExample());
            frame.pack();
            frame.setVisible(true);
        });
    }
}

Thanks for the tip.

This topic is closed to new replies.

Advertisement