67,513
社区成员
发帖
与我相关
我的任务
分享
package Test6;
import java.awt.*;
import java.awt.event.*;
public class BridgeThroughOut {
BridgePanel winframe;
public BridgeThroughOut(){}
public BridgeThroughOut(String wintitle){
winframe=new BridgePanel(wintitle);
}//过桥类,其中包括桥面板类,过桥人的类
public static class BridgePanel extends Frame{
Graphics g;
public BridgePanel(){}
public BridgePanel(String wintitle){
super(wintitle);
setSize(500,500);
setLayout(new FlowLayout(FlowLayout.CENTER,10,10));
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
}
});
this.setVisible(true);
}
public class Person extends Thread{
int x,y;
Color color;
int sleeptime;//人过桥的速度
String direction;//方向
BridgePanel BP;
public Person(){}
public Person(BridgePanel BP,int x,int y,int v,String direct,Color color){
this.BP=BP;
this.x=x;
this.y=y;
this.color=color;
sleeptime=v;
direction=direct;
}//创建人的构造函数
public void run(){
if(direction=="North")
while(y>50){
y--;
try{
Thread.sleep(sleeptime);
}catch(Exception e){
System.out.println(e);
}
repaint();
}
else if(direction=="South")
while(y<450){
y++;
try{
Thread.sleep(sleeptime);
}catch(Exception e){
System.out.println(e);
}
repaint();
}
}
public void setGraphics(BridgePanel BP){
}
public void paint(Graphics g){
Graphics g1=BP.getGraphics();
g1.setColor(color);
g1.fillOval(x, y, 20, 20);
}
}//过桥人的类,把人当做质点,以颜色的不同区分南北桥上的人
public void addPerson(BridgePanel BP,int x,int y,int v,String direct,Color color){
new Person(BP,x,y, v,direct,color).start();
//new Thread(new Person(x,y, v,direct,color).start();
}//添加过桥人的方法
}// 过桥面板的创建
public static void main(String[] args){
//添加人的参数分别是所选面板,横坐标、纵坐标、方向(South或者North)、描绘颜色
BridgePanel BP=new BridgePanel("过桥问题");
BP.addPerson(BP,250,450,10,"North",Color.red);
BP.addPerson(BP,250,50,100,"South",Color.green);
}
}