package traffic;
import java.util.*;
import java.awt.*;
import java.applet.Applet;
public class Crossroad extends Applet {
Traffic traffic;
int quantity;
Thread MyThread = new Thread();
public void init() {
setLayout(new BorderLayout());
traffic = new Traffic(this);
add("Center", traffic);
quantity = Integer.parseInt(getParameter("quantity"));
for (int k=0; k brgright[k]) && brgflag[k]==1){
if(temp > brgleft[k] || temp < brgright[k])
myCar.x = temp;
} else if ((myCar.x< brgleft[k] && myCar.x > brgright[k]) && brgflag[k] == 0)
if (light.signal==0)
brgflag[k]=1;
else
myCar.x=temp;
else if(temp < brgleft[k] && temp > brgright[k])
brgflag[k]=0;
if (myCar.x < 0) {
myCar.x = d.width-10*Math.random();
ratio.vehicleNumber=ratio.vehicleNumber+1;
} else if (myCar.x > d.width) {
myCar.x = d.width-10*Math.random();
}
if (myCar.x!=temp && myCar.wait==-1){
ratio.vehicleWait+= System.currentTimeMillis()- myCar.wait;
myCar.wait=-1;
}
}
} else{
temp=myCar.y;
myCar.y += Math.max(-10, Math.min(10, myCar.dY));
if ( (myCar.y brgbottom) && brgflag[myCar.road-1]==1){
if(temp> brgtop ||tempbrgbottom)&&brgflag[myCar.road-1]==0)
if (light.signal==1)
brgflag[myCar.road-1]=1;
else
myCar.y=temp;
else if(temp< brgtop &&temp>brgbottom)
brgflag[myCar.road-1]=0;
if (myCar.y > d.height||myCar.y<0) {
myCar.y = 10*Math.random();
ratio.vehicleNumber=ratio.vehicleNumber+1;
}
}
}
repaint();
} //end of relax method
Image offscreen;
Dimension offscreensize;
Graphics offgraphics;
final Color selectColor = Color.pink;
final Color edgeColor = Color.black;
final Color vehicleColor = new Color(250, 135, 250);
public void paintVehicle(Graphics g, Vehicle n) {
int x = (int)n.x;
int y = (int)n.y;
g.setColor(vehicleColor);
int w = n.width;
int h = n.length;
g.fillRoundRect(x - w/2, y - h/2, w, h, w/3,h/3);
g.setColor(Color.black);
g.drawRoundRect(x - w/2, y - h/2, w-1, h-1,w/3,h/3);
int dim;
dim = Math.min (w, h);
g.drawRect(x - dim/3, y - dim/3, dim*2/3, dim*2/3);
}
public void paintRoad(Graphics g){
Dimension d = size();
g.setColor(Color.gray);
for(int k=1; k<2; k++){
g.drawLine(rdleft[k], 0, rdleft[k], rdbottom);
g.drawLine(rdleft[k], rdtop, rdleft[k], d.height);
g.drawLine(rdright[k], 0, rdright[k], rdbottom);
g.drawLine(rdright[k], rdtop, rdright[k], d.height);
g.drawLine(rdright[k-1], rdtop, rdleft[k], rdtop);
g.drawLine(rdright[k-1], rdbottom, rdleft[k], rdbottom);
}
g.drawLine(rdright[1], rdbottom, d.width, rdbottom);
g.drawLine(rdright[1], rdtop, d.width, rdtop);
}
public void paintLights(Graphics g){
int lightwidth=15;
for(int k=1; k<2; k++){
g.setColor(Color.black);
g.fillRect(rdleft[k]-lightwidth - lightwidth, rdtop- 6 * lightwidth, lightwidth-2,3* lightwidth);
g.fillRect(rdleft[k]+ 3 * lightwidth, rdtop+4, 3*lightwidth, lightwidth-2);
if (light.signal==1){
g.setColor(Color.red);
g.fillOval(rdleft[k]- 2* lightwidth, rdtop- 6 * lightwidth, lightwidth-2, lightwidth-2);
g.setColor(Color.black);
g.fillOval(rdleft[k]- 2* lightwidth, rdtop- 4*lightwidth, lightwidth-2, lightwidth-2);
g.fillOval(rdleft[k]+ 3 * lightwidth, rdtop+4, lightwidth-2, lightwidth-2);
g.setColor(Color.green);
g.fillOval(rdleft[k]+ 5 * lightwidth, rdtop+4, lightwidth-2, lightwidth-2);
}
if (light.signal==0){
g.setColor(Color.red);
g.fillOval(rdleft[k]+ 3 * lightwidth, rdtop+4, lightwidth-2, lightwidth-2);
g.setColor(Color.black);
g.fillOval(rdleft[k]+ 5 * lightwidth, rdtop+4, lightwidth-2, lightwidth-2);
g.fillOval(rdleft[k]-2*lightwidth, rdtop- 6 * lightwidth, lightwidth-2, lightwidth-2);
g.setColor(Color.green);
g.fillOval(rdleft[k]-2*lightwidth, rdtop- 4*lightwidth, lightwidth-2, lightwidth-2);
}
if (light.signal==2){
g.setColor(Color.black);
g.fillOval(rdleft[k]+ 3 * lightwidth, rdtop+4, lightwidth-2, lightwidth-2);
g.fillOval(rdleft[k]+ 5 * lightwidth, rdtop+4, lightwidth-2, lightwidth-2);
g.fillOval(rdleft[k]-2*lightwidth, rdtop- 6 * lightwidth, lightwidth-2, lightwidth-2);
g.fillOval(rdleft[k]-2*lightwidth, rdtop- 4*lightwidth, lightwidth-2, lightwidth-2);
g.setColor(Color.yellow);
g.fillOval(rdleft[k]+ 4 * lightwidth, rdtop+4, lightwidth-2, lightwidth-2);
g.fillOval(rdleft[k]-2* lightwidth, rdtop- 5 * lightwidth, lightwidth-2, lightwidth-2);
}
}
}
public synchronized void update(Graphics g) {
Dimension d = size();
if ((offscreen == null) || (d.width != offscreensize.width) || (d.height != offscreensize.height)) {
offscreen = createImage(d.width, d.height);
offscreensize = d;
offgraphics = offscreen.getGraphics();
}
offgraphics.setColor(getBackground());
offgraphics.fillRect(0, 0, d.width, d.height);
paintRoad(offgraphics);
//draw lights
paintLights(offgraphics);
//draw cars
for (int i = 0 ; i < nvehicles ; i++) {
paintVehicle(offgraphics, vehicles[i]);
}
g.drawImage(offscreen, 0, 0, null);
}
public void start() {
relaxer = new Thread(this);
relaxer.start();
}
public void stop() {
relaxer.stop();
}
}
}