hi,
ich habe hier meine Ampelsteuerung implementiert:
und hier meine public class Ampel:
in der Klasse Ampeltest sollen wir nun die Ampeln ausgeben lassen mit dem timer:
meine Frage ist jetzt, wie ich der Ampelsteuerung bescheid geben kann, das der Timer gestartet ist und er alle Ampeln im Array umschalten soll ?
Ampelsteuerung, Ampel und Ampeltest sind eigene public classes.
Danke für eure Antworten
ich habe hier meine Ampelsteuerung implementiert:
Java:
public class Ampelsteuerung implements ActionListener{
protected Ampel[] ampeln;
public void actionPerformed(ActionEvent e){
if (e.getActionCommand().equals("NEXT_PHASE")){
for (Ampel ampel : this.ampeln) {
ampel.toNextPhase();
}
}
}
}
Java:
public class Ampel implements Drawable {
protected int x;
protected int y;
protected String phase;
public Ampel(int x, int y, String phase){
this.x = x;
this.y = y;
this.phase = phase;
}
public Ampel(int x, int y){
this.phase = Ampelphase.rot;
this.x = x;
this.y = y;
}
public Ampel(Ampel a){
this.x = a.x;
this.y = a.y;
this.phase = a.phase;
}
public String toString(){
return String.format("[X = %03d Y = %03d Phase = %03s]", x, y, phase);
}
public void toNextPhase(){
if (phase == "rot"){
phase = Ampelphase.rotGelb;
}
if (phase == "rotGelb"){
phase = Ampelphase.gruen;
}
if (phase == "gelb"){
phase = Ampelphase.rot;
}
if (phase == "grün"){
phase = Ampelphase.gelb;
}
}
public void draw(Graphics g){
g.setColor(Color.GRAY);
g.fillRect(x, y, 100, 200);
if (phase == "rot"){
g.setColor(Color.RED);
g.fillOval(x + 25 , y + 15, 50, 50);
g.setColor(Color.LIGHT_GRAY);
g.fillOval(x + 25, y + 70, 50, 50);
g.setColor(Color.LIGHT_GRAY);
g.fillOval(x + 25, y + 125, 50, 50);
}
if (phase == "gelb"){
g.setColor(Color.LIGHT_GRAY);
g.fillOval(x + 25 , y + 15, 50, 50);
g.setColor(Color.YELLOW);
g.fillOval(x + 25, y + 70, 50, 50);
g.setColor(Color.LIGHT_GRAY);
g.fillOval(x + 25, y + 125, 50, 50);
}
if (phase == "grün"){
g.setColor(Color.LIGHT_GRAY);
g.fillOval(x + 25 , y + 15, 50, 50);
g.setColor(Color.LIGHT_GRAY);
g.fillOval(x + 25, y + 70, 50, 50);
g.setColor(Color.GREEN);
g.fillOval(x + 25, y + 125, 50, 50);
}
}
}
Java:
public class Ampeltest {
public static void main(String[] args) {
DirtyPainter painter = new DirtyPainter();
Ampel a = new Ampel(3, 3);
Ampelsteuerung steuerung = new Ampelsteuerung();
Timer timer = new Timer(1500, steuerung);
timer.setActionCommand("NEXT_PHASE");
timer.addActionListener(steuerung);
timer.start();
painter.add(a);
painter.showDrawing();
}
}
meine Frage ist jetzt, wie ich der Ampelsteuerung bescheid geben kann, das der Timer gestartet ist und er alle Ampeln im Array umschalten soll ?
Ampelsteuerung, Ampel und Ampeltest sind eigene public classes.
Danke für eure Antworten