0

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);
    }
}

nsj247
  • 1
  • 1
  • As long as you don't sleep (or do anything else overtly long) in the EDT, it doesn't matter. repaint() queues up a paint request to run on the EDT. It will run when it's its turn. – WJS Oct 03 '20 at 17:53
  • Read the Swing tutorial on [Concurrency](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html). You need to understand the difference from code executing on the `Event Dispatch Thread (EDT)` and code executing on a separate Thread. The Thread.sleep() will only affect the repainting of a component if the sleeping is done on the EDT. – camickr Oct 04 '20 at 02:35

0 Answers0