I know that the paintComponent (Graphics g) method is executed by the AWT-EventQueueue thread. So isn't it possible to execute the paintComponent (Graphics g) method while executing the crossword between the repaint method and Thread.sleep() in the code? Thread has concurrency. But after a few experiments, I found out that it wasn't. The repaint method was executed only in the sleep state. I understand that repaint is executed when I meet Thread.sleep(), but I don't quite understand that it is executed only when I meet Thread.sleep().
Please understand even if there is a grammatical error in the question. I don't know English, and I use a translator.
package draw;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class drawThread extends JPanel {
int y = 6;
ArrayList<Shape> shapeArray = new ArrayList<Shape>();
public drawThread() {
new Thread() {
public void run() {
int i = 1;
while(i<10) {
System.out.println(i + "." + Thread.currentThread().getName() + " 시작");
y = y + 4;
repaint(); //sleep이 걸렸을 때 실행된다.
for(int j=0;j<=10;j++) {
System.out.println(j);
}
i++;
try {
Thread.sleep(3000);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}.start();
}
@Override
protected void paintComponent(Graphics g) {
// TODO Auto-generated method stub
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.BLACK);
g2.setStroke(new BasicStroke(2));
Shape s = new Rectangle2D.Float(10,y,1,1);
g2.draw(s);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame jf = new JFrame();
jf.setTitle("Test");
jf.setSize(300,300);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.add(new drawThread());
jf.setVisible(true);
}
}