多线程不能用Java绘制数字

时间:2018-04-10 15:35:39

标签: java multithreading jpanel runnable

我想创建2个帖子。 一个带有移动矩形的线程,另一个带有第二个矩形的线程。 这是我想要实现的图片。 enter image description here enter image description here

如何将我的类“MovingRectangle”添加到线程? 或者我可以绘制一个可以在线程中移动的图形(如矩形)吗? 我在Runnable的public void run()中发现我无法在这里绘图(也许我错了_)... 对不起那个问题,几天前我已经启动了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类(或者只是想添加代码来移动图)但我不知道该怎么做。

public class New_Thread extends MovingRectangle implements Runnable {
    @Override

    public void run()
    {

    }
}

1 个答案:

答案 0 :(得分:1)

  

我发现在Runnable的public void run()中,我无法在此处绘图。

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

为两个矩形设置动画的最佳方法可能是设置一个定期激发的 Swing Timer (例如,每秒30x或每秒60x),然后进行绘制来自计时器对象调用的处理程序方法。