多线程无法在 Java 中绘制图形

Multi-Threading Can't Drawing Figures in Java

我想创建 2 个线程。 一个带有移动矩形的线程,另一个线程带有第二个矩形跟随第一个矩形。 这是我想要实现的图片。

如何将我的 class "MovingRectangle" 添加到主题中? 或者我可以画一个可以在线程中移动的图形(如矩形)吗? 我发现在 Runnable 中的 public void 运行() 中我无法在此处绘制(也许我错了_)... 对于这个问题,我很抱歉,几天前我已经开始 Java,但我找不到这个问题的答案。 这是主要的:

import javax.swing.JFrame;

public class Main extends JFrame
{
            public Main()
            { }

        public static void main (String[] args) throws InterruptedException {

            JFrame f = new JFrame();

            New_Thread x = new New_Thread();
            f.add(x);
            f.setTitle("Window");
            f.setVisible(true);
            f.setSize(800,800);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            Thread t1 = new Thread (new New_Thread());
            t1.start();
            Thread t2 = new Thread (new New_Thread());
            t2.start();

        }
}

这里是移动这个矩形的代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Rectangle2D;

public class MovingRectangle extends square implements ActionListener {

    Timer t = new Timer (50,this);
    double x = 50;
    double y= 700;
    double velX= 10;
    double velY=10;

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);

        Graphics2D g2 = (Graphics2D) g;
        g2.setColor(Color.gray);
        Rectangle2D rectangle = new Rectangle2D.Double(x,y,40,40);
        g2.fill(rectangle);
        t.start();
    }

    @Override
    public void actionPerformed(ActionEvent e) {

       y = y - velY;
       if (y==90)
       {
           velY=0;
           x=x+velX;
       }

       if (x==600)
       {
           velY=10;
           velY=-velY;
       }

       if (x==600 && y == 700)
       {
           velY=0;
       }

        repaint();
    }


}

这里是小方块:

import javax.swing.*;
import java.awt.*;
import java.awt.geom.Rectangle2D;

public class square extends JPanel
{

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);

        Graphics2D g2 = (Graphics2D) g;
        g2.setColor(Color.PINK);
        Rectangle2D towar1 = new Rectangle2D.Double(20,500,30,30);
        Rectangle2D towar2 = new Rectangle2D.Double(20,300,30,30);
        Rectangle2D towar3 = new Rectangle2D.Double(20,100,30,30);
        Rectangle2D towar4 = new Rectangle2D.Double(150,60,30,30);
        Rectangle2D towar5 = new Rectangle2D.Double(350,60,30,30);
        Rectangle2D towar6 = new Rectangle2D.Double(550,60,30,30);
        Rectangle2D towar7 = new Rectangle2D.Double(640,500,30,30);
        Rectangle2D towar8 = new Rectangle2D.Double(640,300,30,30);
        Rectangle2D towar9 = new Rectangle2D.Double(640,100,30,30);
        g2.fill(towar1);
        g2.fill(towar2);
        g2.fill(towar3);
        g2.fill(towar4);
        g2.fill(towar5);
        g2.fill(towar6);
        g2.fill(towar7);
        g2.fill(towar8);
        g2.fill(towar9);
    }
}

这里是新线程,我想添加 MovingRectangle class(或者只是想添加代码来移动图形),但我不知道该怎么做。

public class New_Thread extends MovingRectangle implements Runnable {
    @Override

    public void run()
    {

    }
}

I found that in public void run() in Runnable i can't drawing here.

你没有看错。 java.awtjavax.swing 中的大多数方法只能从 event dispatch thread 中调用。也就是说,它们只能从一种或另一种事件处理程序中调用。

可能使两个矩形动画化的最佳方法是设置一个 Swing Timer 定期触发(例如,每秒 30 次或每秒 60 次) ,并从计时器对象调用的处理程序方法中进行绘制。