003
30.04.2004, 15:58 Uhr
Spacelord
Hoffnungsloser Fall
|
Hi, hier ist ne Java Lösung.
Code: |
import java.awt.*; import java.awt.event.*; import java.util.*;
///////////////////////Event Klasse////////////////////////////// class AmpelEvent extends EventObject { public AmpelEvent(Object source) { super(source); } } /////////////////Ampel Listener Schnittstelle //////////////////// interface AmpelListener extends EventListener { public void gruen(AmpelEvent e); public void gelb(AmpelEvent e); public void rot(AmpelEvent e); public void rot_gelb(AmpelEvent e); }
//Hauptklasse public class Ampelsteuerung extends Frame { private int schaltung;//speichert den Status und wird in der //paint Methode zum neuzeichnen benoetigt
/////////////////////Ampelklasse/////////////////////////////// class Ampel extends Canvas implements AmpelListener {
/////////////Implementierung von AmpelListener////////////////// public void gruen(AmpelEvent e) { schaltung=0; paint(getGraphics()); } public void gelb(AmpelEvent e) { schaltung=1; paint(getGraphics()); } public void rot(AmpelEvent e) { schaltung=2; paint(getGraphics()); } public void rot_gelb(AmpelEvent e) { schaltung=3; paint(getGraphics()); } //////////////////////////////////////////////////////////////////
Ampel() { schaltung= -1;//Ampel aus this.setSize(150,280); } public void ampelAus(Graphics g) { g.setColor(Color.gray); g.drawRoundRect(50,20,50,50,90,90); g.fillRoundRect(50,20,50,50,90,90); g.drawRoundRect(50,75,50,50,90,90); g.fillRoundRect(50,75,50,50,90,90); g.drawRoundRect(50,130,50,50,90,90); g.fillRoundRect(50,130,50,50,90,90); } //Zum Neuzeichnen noetig public void paint(Graphics g) { switch(schaltung) { case -1: ampelAus(g); break; case 0: //Ampel gruen ampelAus(g); g.setColor(Color.green); g.fillRoundRect(50,130,50,50,90,90); break; case 1: //Ampel gelb ampelAus(g); g.setColor(Color.yellow); g.fillRoundRect(50,75,50,50,90,90); break; case 2: //Ampel rot ampelAus(g); g.setColor(Color.red); g.fillRoundRect(50,20,50,50,90,90); break; case 3 : //Ampel gelb-rot ampelAus(g); g.setColor(Color.red); g.fillRoundRect(50,20,50,50,90,90); g.setColor(Color.yellow); g.fillRoundRect(50,75,50,50,90,90); break; default:{} } } }
///////////////////////Ampelzustandsklasse/////////////////////// class Ampelzustand extends Button { private Vector myAmpelListeners; private int state=1; private int nextState() { state=(state+1)%4; switch (state) { case 0: fireRot(new AmpelEvent(this)); break; case 1: fireRot_Gelb(new AmpelEvent(this)); break; case 2: fireGruen(new AmpelEvent(this)); break; case 3: fireGelb(new AmpelEvent(this)); break; default:{} } return state; } public synchronized void addAmpelListener(AmpelListener l) { Vector v=myAmpelListeners==null?new Vector(2): (Vector)myAmpelListeners.clone(); if(!v.contains(l)) { v.addElement(l); myAmpelListeners=v; } } public synchronized void removeAmpelListener(AmpelListener l) { if(myAmpelListeners!=null&&myAmpelListeners.contains(l)) { Vector v=(Vector)myAmpelListeners.clone(); v.removeElement(l); myAmpelListeners=v; } }
protected void fireRot(AmpelEvent e) { if(myAmpelListeners!=null) { Vector listeners =myAmpelListeners; int count=listeners.size(); for(int i=0;i<count;i++) { ((AmpelListener)listeners.elementAt(i)).rot(e); } } }
protected void fireRot_Gelb(AmpelEvent e) { if(myAmpelListeners!=null) { Vector listeners =myAmpelListeners; int count=listeners.size(); for(int i=0;i<count;i++) { ((AmpelListener)listeners.elementAt(i)).rot_gelb(e); } } } protected void fireGruen(AmpelEvent e) { if(myAmpelListeners!=null) { Vector listeners =myAmpelListeners; int count=listeners.size(); for(int i=0;i<count;i++) { ((AmpelListener)listeners.elementAt(i)).gruen(e); } } } protected void fireGelb(AmpelEvent e) { if(myAmpelListeners!=null) { Vector listeners =myAmpelListeners; int count=listeners.size(); for(int i=0;i<count;i++) { ((AmpelListener)listeners.elementAt(i)).gelb(e); } } }
Ampelzustand() { super("Weiterschalten"); } } ////////////////////////////////////////////////////////////////// private Ampel ampel; private Ampelzustand zustand; private Panel amPanel; private Panel zuPanel;
Ampelsteuerung() { super("Ampel"); amPanel=new Panel(new FlowLayout()); zuPanel=new Panel(new FlowLayout()); ampel = new Ampel(); amPanel.add(ampel); zustand= new Ampelzustand(); zuPanel.add(zustand); zustand.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Ampelzustand temp=(Ampelzustand)e.getSource(); temp.state=temp.nextState(); } }); zustand.addAmpelListener(ampel); this.setSize(150,300); this.setLayout(new BorderLayout()); this.add(amPanel,BorderLayout.CENTER); this.add(zuPanel,BorderLayout.SOUTH); }
//////////////////////////Main////////////////////////////////// public static void main(String[] argv) { Ampelsteuerung steuer=new Ampelsteuerung(); steuer.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); steuer.setVisible(true); } }
|
Musst du nur noch schnell nach C++ portieren.
MfG Spacelord -- .....Ich mach jetzt nämlich mein Jodeldiplom.Dann hab ich endlich was Eigenes. |