JAVA 在多线程中使用不同的paint()方法

标签 java multithreading paint

我是 Java 新手。 是否可以调用不同的paint()方法? 我需要在 4 个单独的线程中创建运行不同的字符串。左-右、右-左、下-上、上-下。

我有一个想法,在每个线程中使用绘画,仅清除已使用的空间并一次又一次地在其上绘画。我希望有更智能的方法......

import java.awt.*;
import java.applet.*;

public class Program extends Applet implements Runnable
{
    Thread t, t1;
    int x = 10, y = 10;

    public void run()
    {
        while(true)
        {
            //this.getGraphics().clearRect(0, 0, 400, 200);
            //paint(this.getGraphics());
            try
            {
                x++;
                t.sleep(10);
            }
            catch (InterruptedException e) {
              }     
        }
    }
    public void init () {
    setSize(400, 200);
    t = new Thread(this);   
    t1 = new Thread(new Runnable() {
        public void run() {
            while(true)
            {
                try
                {
                    t1.sleep(10);
                }
                catch (InterruptedException e) {
                  } 
            }
        }
   });  
    t.start();
   t1.start();

    }
      public void paint(Graphics g) 
      { 
          g.drawString("Test", x, y);       
      } 
}

所以,我正在研究如何在 t2 中创建和使用另一个 Paint()。

最佳答案

 import java.awt.*;
import java.applet.*;
import javax.swing.JLabel;

public class Main extends Applet implements Runnable
{
    private static final long serialVersionUID = 1L;
    Thread thread1, thread2, thread3, thread4;
    int x = 10, y = 10;
    JLabel label1 = new JLabel(" text1 runs from left to right "), label2 = new JLabel("text2 runs in good way here  ");//, label3 = new JLabel("text3"), label4 = new JLabel("text4");
    RotatedLabel label3 = new RotatedLabel("text3 runs in a bad way("), label4  = new RotatedLabel("t4 runs UP-Down all the time  ");
    public void run()
    {
        while(true)
        {
            try
            {
                thread1.sleep(1);
                thread2.sleep(1);
                thread3.sleep(1);
                thread4.sleep(1);
            }
            catch (InterruptedException e) {
              }     
        }
    }
    public void init () {
        setSize(400, 200);
        setLayout(null);
        label1.setBounds(new Rectangle(new Point(1, 1), label1.getPreferredSize()));
        label2.setBounds(new Rectangle(new Point(1, 1), label2.getPreferredSize()));
        label3.setBounds(new Rectangle(new Point(1, 1), label3.getPreferredSize()));
        label4.setDirection(RotatedLabel.Direction.VERTICAL_UP);
        label4.setBounds(new Rectangle(new Point(1, 1), label4.getPreferredSize()));
        label3.setDirection(RotatedLabel.Direction.VERTICAL_DOWN);
        this.add(label1);
        this.add(label2);
        this.add(label3);
        this.add(label4);
        thread1 = new Thread(new Runnable() {
            public void run() {
                while(true)
                {
                    try
                    {
                        label1.setLocation(100, 180);
                        String text = label1.getText();
                        String text2 = text.substring(1)+  text.substring(0,1);
                        label1.setText(text2);
                        thread1.sleep(500);
                    }
                    catch (InterruptedException e) {
                      } 
                }
            }
       });
        thread2 = new Thread(new Runnable() {
            public void run() {
                while(true)
                {
                    try
                    {
                        label2.setLocation(100, 20);
                        String text = label2.getText();
                        String text2 = text.substring(text.length()-1)+  text.substring(0,text.length()-1);
                        label2.setText(text2);
                        thread2.sleep(500);
                    }
                    catch (InterruptedException e) {
                      } 
                }
            }
       });
        thread3 = new Thread(new Runnable() {
            public void run() {
                while(true)
                {
                    try
                    {
                        label3.setLocation(80, 28);
                        String text = label1.getText();
                        String text2 = text.substring(1)+  text.substring(0,1);
                        label3.setText(text2);
                        label3.setSize(10, 180);
                        thread3.sleep(500);
                    }
                    catch (InterruptedException e) {
                      } 
                }
            }
       });
        thread4 = new Thread(new Runnable() {
            public void run() {
                while(true)
                {
                    try
                    {
                        label4.setLocation(268, 25);
                        String text = label4.getText();
                        String text2 = text.substring(1)+  text.substring(0,1);
                        label4.setText(text2);


                        thread4.sleep(500);
                    }
                    catch (InterruptedException e) {
                      } 
                }
            }
       });
        thread1.start();
        thread2.start();
        thread3.start();
        thread4.start();
        }
}

关于JAVA 在多线程中使用不同的paint()方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32703790/

相关文章:

python - 未发现 djutils 队列命令

ios - 如何对依赖于 Objective-C 中的 volatile 变量的方法进行单元测试?

java - 在Java中,构造函数中的final字段赋值涉及到哪些操作?

java - 如何更新不断变化的 GUI 而不会因更新而闪烁(图形 g)?

java - 如何在 GridBagLayout 上放置背景图片

java - 在 Java 中查找总磁盘大小的可移植方法(Java 6 之前)

java - 模态对话框并不总是位于未装饰框架的顶部

java - 如何在同一屏幕上拥有按钮和自定义绘画 View ?安卓

java - PaintComponent 方法混合不同的 JPanel 组件

java - @Transactional 的动态事务隔离级别