PDA

View Full Version : help position the light



hhv
23-02-2003, 13:57
private int xPosition = 105;
private int yPosition = 30;
private int nLights = 0;
private SetOfLights[] lights = new SetOfLights[4];
private static final int lightsWidth = 150;
private static final int lightsLength = 200;
.......
lights[nLights] = new SetOfLights(area, xPosition, yPosition);
lights[nLights].start();

" if (nLights == 0)
xPosition += lightsWidth;
if (nLights ==1)
xPosition +=lightsWidth;
yPosition +=lightsLength;
...................."

nLights++;
if (nLights == 4)
newSetButton.setEnabled(false);

Cac cao thu xem the nao position cho em 4 cai lights nay vao 4 vi tri tai 4 di?nh cua mot hinh vuong cai, em position mai ma ko duoc, hic hic
duoi day la full class

import java.awt.*;

class SetOfLights extends Thread {

private Canvas area;
private int x;
private int red = 0;
private int light;
private int y;
boolean alive = true; // in textbook is private

// Those are public
boolean walk = false;
int time[] = {2000, 2000, 2000, 2000};
boolean walkOn = false;

public SetOfLights(Canvas c, int x,int y) {
area = c;
this.x = x;
this.y = y;
}

public void run() {
while (alive)
for (int light = 0; light < 3; light++)
if ((light == red) & walk) {
walkOn = false;
for (int i=0; i<11; i++) {
draw(light);
try {
sleep(time[3]);
}
catch (InterruptedException e) {
System.out.println("Exception in Traffic Light Thread.");
}
walkOn = !walkOn;
}
walk = false;
}
else {
draw(light);
try {
sleep(time[light]);
}
catch (InterruptedException e) {
System.out.println("Exception in Traffic Light Thread.");
};
}
}

void draw(int light) {
Graphics g = area.getGraphics();
g.setColor(Color.black);
g.drawOval(x-8, y, 30, 80);
g.setColor(Color.cyan);
g.fillRect(x-90, y, 70, 1000);
g.setColor(Color.black);
g.drawString("" + time[0], x-70, y + 18);
g.drawString("" + time[2], x-70, y + 38);
g.drawString("" + time[1], x-70, y + 58);
g.drawString("" + time[3], x-70, y + 78);

switch (light) {
case 0:
g.setColor(Color.red);
g.fillOval(x, y + 10, 15, 15);
g.setColor(Color.lightGray);
g.fillOval(x, y + 30, 15, 15);
g.fillOval(x, y + 50, 15, 15);
break;
case 1:
g.setColor(Color.green);
g.fillOval(x, y + 50, 15, 15);
g.setColor(Color.lightGray);
g.fillOval(x, y + 10, 15, 15);
g.fillOval(x, y + 30, 15, 15);
break;
case 2:
g.setColor(Color.yellow);
g.fillOval(x, y + 30, 15, 15);
g.setColor(Color.lightGray);
g.fillOval(x, y + 10, 15, 15);
g.fillOval(x, y + 50, 15, 15);
break;
}

if ((light == red) & walk) {
if (walkOn)
g.setColor(Color.green);
else
g.setColor(Color.white);
g.fillOval(x+1, y + 95, 14, 14);
}
else {
g.setColor(Color.black);
g.drawOval(x, y + 95, 15, 15);
}
}
}


import java.awt.*; // for Frame
import java.awt.event.*; // for Action/ItemListener

public class Traffic4 extends Frame
implements ActionListener, ItemListener {

private Canvas area;
private Button newSetButton;
private Button walkButton;
private Button closeButton;
private Choice colours;
private Choice pickinglight;
private int light;
private TextField duration;

private int xPosition = 105;
private int yPosition = 30;
private int nLights = 0;
private SetOfLights[] lights = new SetOfLights[4];
private static final int lightsWidth = 150;
private static final int lightsLength = 200;
private int setWanted = 0;

public Traffic4() {
setTitle("Traffic Lights");
add("North",
new Label("Ft. Myers Traffic Light Simulator", Label.CENTER));
area = new Canvas();
area.addMouseListener(new MouseEvtHandler());
add("Center", area);

Panel buttons = new Panel();

newSetButton = new Button("New Set");
newSetButton.addActionListener(this);
buttons.add(newSetButton);

colours = new Choice();
colours.addItem("Red Duration");
colours.addItem("Green Duration");
colours.addItem("Yellow Duration");
colours.addItem("Walk Duration");
colours.addItemListener(this);
light = 0;
buttons.add(colours);

duration = new TextField("", 4);
duration.addActionListener(this);
buttons.add(duration);

pickinglight = new Choice();
pickinglight.addItem("Northeast and Southwest light");
pickinglight.addItem("Northwest and Southeast light");

pickinglight.addItemListener(this);
buttons.add(pickinglight);


walkButton = new Button("Walk");
walkButton.addActionListener(this);
buttons.add(walkButton);

closeButton = new Button("Close");
closeButton.addActionListener(this);
buttons.add(closeButton);

add("South", buttons);

// Set up the frame
setSize(800, 800);
setVisible(true);

addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}


public void itemStateChanged(ItemEvent e) {
String s = (String)e.getItem();
if (s == "Red Duration")
light = 0;
else if (s == "Green Duration")
light = 1;
else if (s == "Yellow Duration")
light = 2;
else if (s == "Walk Duration")
light = 3;
String h = (String)e.getItem();
if (h == "Northeast and Southwest light")
nLights = 1 & 3;

else if ( h == "Northwest and Southeast light")
nLights = 0 & 2;

}

public void actionPerformed(ActionEvent e) {
Object event = e.getSource();

if (event == newSetButton) {
lights[nLights] = new SetOfLights(area, xPosition, yPosition);
lights[nLights].start();

xPosition += lightsWidth;

yPosition +=lightsLength;


nLights++;
if (nLights == 4)
newSetButton.setEnabled(false);
}

else if (event == walkButton)
lights[setWanted].walk = true;

else if (event == duration)
// setWanted = nLights;
lights[setWanted].time[light] =
Integer.parseInt(duration.getText());


else if (event == closeButton) {
for (int i=0; i < nLights; i++)
lights[i].alive = false;
setVisible(false);
dispose();
System.exit(0);
}
}

class MouseEvtHandler extends MouseAdapter {
public void mousePressed(MouseEvent e) {
int n = e.getX() / lightsWidth;
if (n < nLights)
setWanted = n;
}
}

public static void main(String args[]) {
new Traffic4();
}
}