不知道是那出了个错,语法没错,可就是得不到想要的结果,那个大侠指教一下小弟,谢谢了!!
import java.awt.*;
import java.applet.Applet;
import java.awt.Color;
public class Test1 extends Applet
{
东西向 td;南北向 td2;
交通信号灯 a=new 交通信号灯();
String xhd="东西灯";
Graphics g;
int x1=0,y1=220;
public void init(){setForeground(Color.DARK_GRAY);
}
public void start()
{
if(td==null)td=new 东西向(a);td.start();
if(td2==null)td2=new 南北向(a);td2.start();
}
public void stop()
{td=null;td2=null;}
public void update(Graphics g)
{paint(g);}
public void paint(Graphics g)
{
g.fillRect(0,100,300,100);
g.fillRect(100,0,100,300);
g.setColor(Color.red);
g.drawString("南北行",110,300-y1);
g.drawString("南北行",160,y1);
g.drawString("东西行",280-x1,120);
g.drawString("东西行",x1,180);
if(xhd=="东西灯")a.切换信号灯(100,100,"红","绿",g);
if(xhd=="南北灯")a.切换信号灯(100,100,"绿","红",g);
if(xhd=="黄灯")a.切换信号灯(100,100,"黄","黄",g);
}
class 交通信号灯
{
int x,y;String 颜色1,颜色2,d;int 显示时间;
public void 切换信号灯(int x1,int y1,String c1,String c2,Graphics g)
{
x=x1;y=y1;颜色1=c1;颜色2=c2;
for(int i=0;i<3;i++)
{
if(颜色1=="黄")g.setColor(Color.yellow);
if(颜色1=="红")g.setColor(Color.red);
if(颜色1=="绿")g.setColor(Color.green);
g.fillOval(x+i*15,y,15,15);
g.fillOval(x+55+i*15,y+85,15,15);
if(颜色2=="黄")g.setColor(Color.yellow);
if(颜色2=="红")g.setColor(Color.red);
if(颜色2=="绿")g.setColor(Color.green);
g.fillOval(x+85,y+i*15,15,15);
g.fillOval(x,y+55+i*15,15,15);
}
}
}
class 东西向 extends Thread
{
private 交通信号灯 a1;
public 东西向(交通信号灯 a){a1=a;}
public void run()
{
for(int j=0;j<100+j;j++)
{
synchronized(a1){
for(int i=0;i<100;i++)
{
x1+=5;repaint();
if(x1==285)x1=0;
try
{Thread.sleep(100);}
catch(InterruptedException e){}
}
xhd="黄灯";repaint();
try{Thread.sleep(400);}
catch(InterruptedException e){}
xhd="南北灯";x1=70;repaint();
}
}
}
}
class 南北向 extends Thread
{
private 交通信号灯 a1;
public 南北向(交通信号灯 a){a1=a;}
public void run()
{
for(int j=0;j<100+j;j++)
{
synchronized(a1){for(int i=0;i<100;i++)
{
y1+=5;repaint();if(y1==285)y1=0;
try
{
Thread.sleep(50);
}
catch(InterruptedException e){}
}
xhd="黄灯";repaint();try
{Thread.sleep(400);}
catch(InterruptedException e){}
xhd="东西灯";y1=220;repaint();
}
}
}
}
}
[原创] 请问大侠,这是那出了问题