I would like to create a JPanel
and draw things on it one by one. I would like to see them added after each other. The problem is, I always have to wait until everything is finished by paintComponent
method. Is there a way to achieve what I desire? Thanks in advance!
package javapaintui;
import java.awt.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
class JavaPaintUI extends JFrame {
private JPanel jPanel2;
public JavaPaintUI() {
initComponents();
}
private void initComponents() {
jPanel2 = new Panel2();
this.setContentPane(jPanel2);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
}
class Panel2 extends JPanel {
Panel2() {
setPreferredSize(new Dimension(420, 420));
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawString("BLAH", 20, 20);
try {
Thread.sleep(5000);
} catch (InterruptedException ex) {
Logger.getLogger(JavaPaintUI.class.getName()).log(Level.SEVERE, null, ex);
}
g.drawRect(200, 200, 200, 200);
}
}
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new JavaPaintUI().setVisible(true);
}
});
}
}