繁体   English   中英

JAVA将不同的paint()方法用于多线程Java

[英]JAVA Use different paint() methods with multiple threads Java

我是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();
        }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM