1 / 10

Ereignissteuerung (Events)

Ereignissteuerung (Events). Learning By Doing. Unterprogrammaufruf an fest codierter Stelle. Event kann irgendwo auftreten. ereignisgesteuert. prozedural. Wann immer der Event auftritt, führe möglichst bald aus. TimerEvent (UML-Diagramm) im Delegations-Eventmodell. Learning By Doing.

hansel
Télécharger la présentation

Ereignissteuerung (Events)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Ereignissteuerung (Events) Learning By Doing Unterprogrammaufrufan fest codierter Stelle Event kann irgendwo auftreten ereignisgesteuert prozedural Wann immer der Event auftritt, führe möglichst bald aus ...

  2. TimerEvent (UML-Diagramm)im Delegations-Eventmodell Learning By Doing Garantieerklärung: timeElapsed() existiert! Implementierung derCallbackmethode Zur Registrierung derCallbackmethode Event! Aufruf von timerListener.timeElapsed() Registrierung derCallbackmethode Registrierung: Übergeben einer Referenz der Klasse, welche die Callbackmethode enthält, an den Eventhandler, der die Callbackmethode bei jedem Event aufruft.

  3. DigitalWatch Learning By Doing // DigitalWatch.java import ch.aplu.util.*; import java.awt.Font; public class DigitalWatch implements TimerListener { private int seconds = 0; private boolean isBell = true; private LoResAlarmTimer timer = new LoResAlarmTimer(1000000L); private GPanel panel = new GPanel(); public DigitalWatch() { panel.font(new Font( "Arial", Font.PLAIN, 200)); timer.addTimerListener(this); while (true) { if (isBell) { isBell = false; panel.clear(); panel.text(0.3, 0.4, Integer.toString(seconds)); seconds++; } } } while (true) { if (isBell) { isBell = false; panel.clear(); panel.text(0.3, 0.4, Integer.toString(seconds)); seconds++; } } Implementierung "DigitalWatch implementiert einen TimerListener" Registrierung"Dieser TimerListener wird beim Eventhandler registriert" Callbackmethode Eventloop public boolean timeElapsed() { isBell = true; return true; } public static void main(String[] args) { new DigitalWatch(); } }

  4. MouseEvents Learning By Doing // WbzEx11.java import java.awt.event.*; import java.awt.Color; import ch.aplu.util.*; public class WbzEx11 { class MyMouseAdapter extends MouseAdapter { public void mousePressed(MouseEvent evt) { Color c = new Color((float)Math.random(), (float)Math.random(), (float)Math.random()); Stern stern = new Stern(p, 50, c); stern.moveAt((int)p.toWindowX(evt.getX()), (int)p.toWindowY(evt.getY())); stern.showMe(); } } private GPanel p = new GPanel(0, 500, 0, 500); public WbzEx11() { p.addMouseListener(new MyMouseAdapter()); } public static void main(String[] args) { new WbzEx11(); } } Innere Klasse hat Zugriff Callbackmethode Registrie-rung Mausevents

  5. Anonyme Klassen Learning By Doing // WbzEx11a.java import java.awt.event.*; import java.awt.Color; import ch.aplu.util.*; public class WbzEx11a { private GPanel p = new GPanel(0, 500, 0, 500); public WbzEx11a() { p.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent evt) { Color c = new Color((float)Math.random(), (float)Math.random(), (float)Math.random()); Stern stern = new Stern(p, 50, c); stern.moveAt((int)p.toWindowX(evt.getX()), (int)p.toWindowY(evt.getY())); stern.showMe(); } }); } public static void main(String[] args) { new WbzEx11a(); } } Registrierung Anonyme Klasseabgeleitet von MouseAdapter Anonyme Klassen

  6. ButtonEvents Learning By Doing // WbzEx12.java import java.awt.event.*; import javax.swing.*; import ch.aplu.turtle.*; public class WbzEx12 { private class ButtonActionAdapter implements ActionListener { public void actionPerformed(ActionEvent evt) { Object source = evt.getSource(); if (source == runButton) state = States.RUNNING; if (source == stopButton) state = States.STOPPED; if (source == quitButton) state = States.QUITTING; } } private enum States {STOPPED, RUNNING, QUITTING}; private States state = States.STOPPED; private JButton runButton = new JButton("Run"); private JButton stopButton = new JButton("Stop"); private JButton quitButton = new JButton("Quit"); Innere Klasse, implementiertActionListener State switch Zustände (Aufzählungstyp) aktueller Zustand Instanzvariablen Buttonevents

  7. ButtonEvents public WbzEx12() { Turtle t = new Turtle(); JPanel jp = t.getPlayground(); jp.add(runButton); jp.add(stopButton); jp.add(quitButton); jp.validate(); ButtonActionAdapter adapter = new ButtonActionAdapter(); runButton.addActionListener(adapter); stopButton.addActionListener(adapter); quitButton.addActionListener(adapter); while (state != States.QUITTING) { switch(state) { case STOPPED: Thread.yield(); break; case RUNNING: t.forward(10).left(10); break; } } System.exit(0); } public static void main(String[] args) { new WbzEx12(); } } Learning By Doing Buttons in der Komponente anzeigen Registrierung Prozessor freiwillig anderen abgeben Event loop Buttonevents

  8. ButtonEvents // WbzEx12a.java import java.awt.event.*; import javax.swing.*; import ch.aplu.turtle.*; public class WbzEx12a { interface State { int QUITTING = 0; int STOPPED = 1; int RUNNING = 2; } private class ButtonActionAdapter implements ActionListener { public void actionPerformed(ActionEvent evt) { Object source = evt.getSource(); if (source == runButton) state = State.RUNNING; if (source == stopButton) state = State.STOPPED; if (source == quitButton) state = State.QUITTING; } } private int state = State.STOPPED; private JButton runButton = new JButton("Run"); private JButton stopButton = new JButton("Stop"); private JButton quitButton = new JButton("Quit"); Learning By Doing Simulation des Enum-Typs Verwendung durch Vorstellendes Interface-Bezeichners state-Variable als "dekorierter" int Buttonevents (J2SE 1.4) Schon viel besser als state = 2

  9. ButtonEvents Learning By Doing Verwendung durch Vorstellendes Interface-Bezeichners public WbzEx12a() { Turtle t = new Turtle(); JPanel jp = t.getPlayground(); jp.add(runButton); jp.add(stopButton); jp.add(quitButton); jp.validate(); ButtonActionAdapter adapter = new ButtonActionAdapter(); runButton.addActionListener(adapter); stopButton.addActionListener(adapter); quitButton.addActionListener(adapter); while (state != State.QUITTING) { switch(state) { case State.STOPPED: Thread.yield(); break; case State.RUNNING: t.forward(10).left(10); break; } } System.exit(0); } public static void main(String[] args) { new WbzEx12a(); } } Buttonevents (J2SE 1.4)

  10. GUI Muster Learning By Doing JPanel als Grafik-Fenster Position und Grösse des JFrames JFrame als umfassendes Fenster Aktion beim Klicken des Close-Buttons Füge das JPanel in das JFrame Zeige das JFrame mit Inhalt paintComponent() wird bei jedemSystem-Trigger aufgerufen Aufruf überschr. Methode aus JPanel Zeichnen mit dem Graphics-Context Linie als Objekt Text als Grafik Neue Linieneigenschaft (Dicke) Zeichne Grafikobjket Hole Graphics2D-Context Indikator für Aufruf von paintComponent // WbzEx13.java import java.awt.*; import javax.swing.*; import ch.aplu.util.*; public class WbzEx13 extends JPanel { private int count = 0; public WbzEx13() { JFrame f = new JFrame("Frame Window"); f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); f.getContentPane().add(this); f.setBounds(50, 50, 550, 550); f.setVisible(true); } public void paintComponent(Graphics g) { super.paintComponent(g); g.drawOval(0, 0, 500, 500); g.drawString("WBZ-Kurs \"Java im Unterricht\"", 180, 250); Graphics2D g2D = (Graphics2D)g; g2D.setStroke(new BasicStroke(8)); Line2D.Double line = new Line2D.Double(20, 300, 480, 300); g2D.draw(line); System.out.println(count++); } public static void main(String[] args) { Console.init(); new WbzEx13(); } } GUI-Muster

More Related