Java 尝试在任意方向上移动图像并使字符串以漩涡状颜色显示。使用线程不起作用
这是我的代码。我在applet图形中使用了多线程概念。我试图在任意方向移动图像,并使字符串以漩涡状的颜色显示。它在单独的类中工作,但在多线程中不工作。我猜问题出在run方法中。请检查代码![]一只忙碌的猫]Java 尝试在任意方向上移动图像并使字符串以漩涡状颜色显示。使用线程不起作用,java,multithreading,applet,Java,Multithreading,Applet,这是我的代码。我在applet图形中使用了多线程概念。我试图在任意方向移动图像,并使字符串以漩涡状的颜色显示。它在单独的类中工作,但在多线程中不工作。我猜问题出在run方法中。请检查代码![]一只忙碌的猫] import java.applet.Applet; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.a
import java.applet.Applet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
//guess problem is in this class...!!
public class threadone extends Applet implements Runnable
{
int x;
int ux1,ux2;
private Image img;
boolean running=true;
Font f=new Font("TimesRoman", Font.BOLD + Font.ITALIC, 40);
Color colors[]=new Color[50];
private Thread thread1,thread2;
public void start()
{
img=getImage(getCodeBase(),"c232.gif");
Runnable job=new threadtwo(this);
Runnable job2=new threadthree(this);
thread1=new Thread(job);
thread2=new Thread(job2);
thread1.start();
thread2.start();
}
public void destroy()
{
running=false;
}
public void stop()
{
running=false;
}
public void paint(Graphics g)
{
g.setFont(f);
int i = 0;
setForeground(colors[i]);
g.drawString("flames",750,50);
g.drawImage(img, x,250, this);
ux1=ux2=0;
}
public void run()
{
}
}`
//this is second class in multithread have tried to move an image//
public class threadtwo implements Runnable
{
threadone parent;
public threadtwo(threadone parent)
{
this.parent=parent;
}
public void run()
{
while(true)
{
int x;
int ux1 = 0,ux2 = 0;
for (x = 5; x <= 505; x+=4)
{
ux2 = x + 90;
parent. repaint();
try {
Thread.sleep(100);
}
catch (InterruptedException e)
{
if (ux1 == 0) ux1 = x;
}
for (x = 505; x > 5; x -=4)
{
ux1 = x;
parent. repaint();
try {
Thread.sleep(100);
}
catch (InterruptedException e)
{
}
if (ux2 == 0) ux2 = x + 90;
}
}
}
}
}
// this is third class in multithread have tried to implement swirly colors//
import java.awt.Color;
public class threadthree implements Runnable{
threadone parent;
Color colors[]=new Color[50];
public threadthree(threadone parent)
{
this.parent=parent;
}
public void run() {
// TODO Auto-generated method stub
float c = 0;
for( int i=0;i<colors.length;i++)
{
colors[i]=Color.getHSBColor(c,(float)1.0,(float)1.0);
c+=0.02;
}
int i = 0;
while (true)
{
parent.repaint();
i++;
try {
Thread.sleep(50);
}
catch (InterruptedException e)
{
}
if (i == colors.length )
i = 0;
}
}
}
import java.applet.applet;
导入java.awt.Color;
导入java.awt.Font;
导入java.awt.Graphics;
导入java.awt.Image;
//我猜问题就在这节课上。。。!!
公共类threadone扩展小程序实现可运行
{
int x;
int-ux1,ux2;
私有图像img;
布尔运行=真;
Font f=新字体(“TimeRoman”,Font.BOLD+Font.ITALIC,40);
颜色[]=新颜色[50];
私有线程thread1、thread2;
公开作废开始()
{
img=getImage(getCodeBase(),“c232.gif”);
Runnable作业=新线程二(此);
Runnable job2=新线程三(此);
线程1=新线程(作业);
螺纹2=新螺纹(job2);
thread1.start();
thread2.start();
}
公共空间销毁()
{
运行=错误;
}
公共停车场()
{
运行=错误;
}
公共空间涂料(图g)
{
g、 setFont(f);
int i=0;
设置前景(颜色[i]);
g、 抽绳(“火焰”,750,50);
g、 绘图图像(img,x,250,本);
ux1=ux2=0;
}
公开募捐
{
}
}`
//这是第二类在多线程中尝试移动图像//
公共类threadtwo实现可运行
{
单亲;
公共线程二(线程一父级)
{
这个。父=父;
}
公开募捐
{
while(true)
{
int x;
intux1=0,ux2=0;
对于(x=5;x=5;x-=4)
{
ux1=x;
parent.repaint();
试一试{
睡眠(100);
}
捕捉(中断异常e)
{
}
如果(ux2==0)ux2=x+90;
}
}
}
}
}
//这是第三个在多线程中尝试实现漩涡颜色的类//
导入java.awt.Color;
公共类ThreadThreadThree实现可运行{
单亲;
颜色[]=新颜色[50];
公共线程三(线程一父级)
{
这个。父=父;
}
公开募捐{
//TODO自动生成的方法存根
浮点数c=0;
对于(int i=0;i什么不起作用?我们需要从某个地方开始,而不仅仅是试图挖掘所有代码。1)为什么要编写一个小程序?如果是由于老师的规范,请参考他们。2)为什么要使用AWT而不是Swing?请参阅我的答案,了解放弃使用AWT组件的许多好理由。3)为什么要破坏cat映像链接?问题在于主类的run方法!!我们最初需要调用start函数吗?我发现在小程序中工作很容易而不是摇摆,因为我是一个beginner@Thompson“我发现在小程序中工作比摇摆更容易”你很困惑。1)小程序可以是Swing或AWT。2)小程序是一个完整的PITA,如果你读了那篇链接文章,我会详细介绍这件事。3)人们更好地帮助Swing,因为他们忘记了AWT。--所以,你有代码问题,来这里寻求帮助,是时候开始使用Swing了。然后离开小程序他完全撇开不谈。