gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 04:21:39 26 4
gpt4 key购买 nike

我是 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com