📄 test3.java
字号:
import java.awt.*;
import java.applet.Applet;
import java.awt.Color;
import javax.swing.JOptionPane;
public class Test3 extends Applet
{
东西向 td;
南北向 td2;
交通信号灯 a=new 交通信号灯();
String xhd="东西灯";
Graphics g;
String input;
int time;
int x1=0, x2=0, x3=0;
int y1=80,y2=80,y3=0;
public void init(){setForeground(Color.DARK_GRAY);}
public void start()
{
input=JOptionPane.showInputDialog("请输入红灯须停多少秒?");
time=Integer.parseInt(input);
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.white);
g.drawString("南北行",100-x2,y2-15);
g.drawString("南北行",100,y1);
g.drawString("南北行",160,310-y1);
g.drawString("南北行",160+x2,325-y2);
g.drawString("东西行",265-x3,115-y3);
g.drawString("东西行",265-x1,130);
g.drawString("东西行",x1,180);
g.drawString("东西行",x3,195+y3);
if(xhd=="东西灯")a.切换信号灯(100,85,"红","绿",g);
if(xhd=="南北灯")a.切换信号灯(100,85,"绿","红",g);
if(xhd=="黄灯")a.切换信号灯(100,85,"黄","黄",g);
}
class 交通信号灯{
int x,y;
String 颜色1,颜色2,d;
public void 切换信号灯(int x1,int y1,String c1,String c2,Graphics g)
{
x=x1;y=y1;颜色1=c1;颜色2=c2;
int[] sx3={171,177,177,183,177,177,171};
int[] sy3={205,205,201,207,213,209,209};
int[] sx2={162,168,164,164,160,160,156};
int[] sy2={201,207,207,213,213,207,207};
int[] nx1={116,122,122,128,128,122,122};
int[] ny1={92,86,90,90,94,94,98};
int[] nx2={135,139,139,143,137,131,135};
int[] ny2={86,86,92,92,98,92,92};
int[] wx1={90,94,94,98,92,86,90};
int[] wy1={171,171,177,177,183,177,177};
int[] wx2={86,92,92,98,92,92,86};
int[] wy2={160,160,156,162,168,164,164};
int[] ex1={201,207,213,209,209,205,205};
int[] ey1={122,116,122,122,128,128,122};
int[] ex2={201,207,207,213,213,207,207};
int[] ey2={137,131,135,135,139,139,143};
if(颜色1=="黄")
{
g.setColor(Color.yellow);
g.fillPolygon(sx3,sy3,7);
g.fillPolygon(sx2,sy2,7);
g.fillPolygon(nx1,ny1,7);
g.fillPolygon(nx2,ny2,7);
}
if(颜色1=="红")
{
g.setColor(Color.red);
g.fillPolygon(sx3,sy3,7);
g.fillPolygon(sx2,sy2,7);
g.fillPolygon(nx1,ny1,7);
g.fillPolygon(nx2,ny2,7);
}
if(颜色1=="绿")
{
g.setColor(Color.green);
g.fillPolygon(sx3,sy3,7);
g.fillPolygon(sx2,sy2,7);
g.fillPolygon(nx1,ny1,7);
g.fillPolygon(nx2,ny2,7);
}
g.fillOval(x,y,15,15);
g.fillOval(x+85,y+115,15,15);
if(颜色2=="黄")
{
g.setColor(Color.yellow);
g.fillPolygon(wx1,wy1,7);
g.fillPolygon(wx2,wy2,7);
g.fillPolygon(ex1,ey1,7);
g.fillPolygon(ex2,ey2,7);
}
if(颜色2=="红")
{
g.setColor(Color.red);
g.fillPolygon(wx1,wy1,7);
g.fillPolygon(wx2,wy2,7);
g.fillPolygon(ex1,ey1,7);
g.fillPolygon(ex2,ey2,7);
}
if(颜色2=="绿")
{
g.setColor(Color.green);
g.fillPolygon(wx1,wy1,7);
g.fillPolygon(wx2,wy2,7);
g.fillPolygon(ex1,ey1,7);
g.fillPolygon(ex2,ey2,7);
}
g.fillOval(x+100,y+15,15,15);
g.fillOval(x-15,y+100,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<=(time*15);i++)
{
if(x1<100)
{x1+=5;x3+=5; repaint();}
else
{
x1+=5;y3+=3; repaint();
}
if(x1==285) {x1=0;x3=0;y3=0;}
try {Thread.sleep(50);}
catch (InterruptedException e) {}
}
xhd="黄灯";
repaint();
try{Thread.sleep(400);}
catch(InterruptedException e){}
xhd="南北灯";x1=50;y3=0;x3=50;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<(time*15);i++)
{
if(y1<145)
{y1+=5;y2+=5; repaint();}
else
{
y1+=5;x2+=3; repaint();
}
if(y1==285) {y1=0;x2=0;y2=0;}
try{Thread.sleep(50);}
catch(InterruptedException e){}
}
xhd="黄灯";
repaint();
try {Thread.sleep(400);}
catch(InterruptedException e){}
xhd="东西灯";y1=80;x2=0;y2=80;repaint();
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -