我查看了可能已经回答的其他一些问题,但我没有看到任何与我相关的内容,或者至少我理解的内容。
我正在尝试创建一个交通信号灯,我遇到的问题是当我单击一个按钮时实际绘制的红色绿色和黄色圆圈。一个快速的答复将不胜感激,谢谢。
public class TrafficLight extends JApplet implements ActionListener {
private Image Hayden;
JButton btn1;
JButton btn2;
JButton btn3;
int x;
public void init() {
setLayout(new FlowLayout());
btn1 = new JButton("Stop");
btn2 = new JButton("Wait");
btn3 = new JButton("Go");
Boolean Answer;
add(btn1);
btn1.addActionListener(this);
add(btn2);
btn2.addActionListener(this);
add(btn3);
btn3.addActionListener(this);
Hayden = getImage(getDocumentBase(), "49.jpg");
}
public void actionPerformed(ActionEvent event){
if (event.getSource()==btn1){
boolean one = true;
}
if (event.getSource()==btn2){
boolean two = true;
}
if (event.getSource()==btn3){
boolean three = true;
}
repaint();
}
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.black);
g.drawRect(0, 400, 700, 200);//creating the rectangle
g.fillRect(0, 400, 700, 200);
g.setColor(Color.black);
g.drawRect(645, 0, 55, 155);//creating the rectangle
g.fillRect(645, 0, 55, 155);
g.setColor(Color.white);
g.drawOval(650, 5, 45, 45);//creating the oval
g.fillOval(650, 5, 45, 45);
g.setColor(Color.white);
g.drawOval(650, 55, 45, 45);//creating the oval
g.fillOval(650, 55, 45, 45);
g.setColor(Color.white);
g.drawOval(650, 105, 45, 45);//creating the oval
g.fillOval(650, 105, 45, 45);
if (one == true){
g.setColor(Color.red);
g.drawOval(650,5,45,45);
}
else if (two == true){
g.setColor(Color.red);
g.drawOval(650,55,45,45);
}
else if (three == true){
g.setColor(Color.red);
g.drawOval(650,105,45,45);
}
g.drawImage(Hayden, 0, 500, 150, 100, this);//create the image
}
}