Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 8.5 Animating with EventTimer. © 2006 Pearson Addison-Wesley. All rights reserved 8.5.2 A Crash Course in the Use of Timed Events What kinds of.

Similar presentations


Presentation on theme: "Lecture 8.5 Animating with EventTimer. © 2006 Pearson Addison-Wesley. All rights reserved 8.5.2 A Crash Course in the Use of Timed Events What kinds of."— Presentation transcript:

1 Lecture 8.5 Animating with EventTimer

2 © 2006 Pearson Addison-Wesley. All rights reserved 8.5.2 A Crash Course in the Use of Timed Events What kinds of occurrences produce events? How does the event handling mechanism work?

3 © 2006 Pearson Addison-Wesley. All rights reserved 8.5.3 EventTimer for building computer “alarm clocks” javax.swing.Timer EventTimer «constructor» + EventTimer(int) «update» + start() + stop() «event handler» + void actionPerformed( java.awt.event.ActionEvent ) time between consecutive events (in milliseconds)

4 © 2006 Pearson Addison-Wesley. All rights reserved 8.5.4 import javax.swing.JFrame; public class Driver { private Clock animator; private Oval dot; private JFrame win; public Driver() { win = new JFrame(); win.setBounds(0,0,200,200); win.setLayout(null); win.setVisible(true); dot = new Oval(0, 0, 30, 30); win.add(dot, 0); win.repaint(); animator = new Clock(this, 50); animator.start(); } public void clockHasTicked() { if (dot.getX() > 195) dot.setLocation(0, 0); else dot.setLocation(dot.getX()+1, 0); dot.repaint(); } import javax.swing.JFrame; public class Driver { private Clock animator; private Oval dot; private JFrame win; public Driver() { win = new JFrame(); win.setBounds(0,0,200,200); win.setLayout(null); win.setVisible(true); dot = new Oval(0, 0, 30, 30); win.add(dot, 0); win.repaint(); animator = new Clock(this, 50); animator.start(); } public void clockHasTicked() { if (dot.getX() > 195) dot.setLocation(0, 0); else dot.setLocation(dot.getX()+1, 0); dot.repaint(); } import java.awt.event.*; public class Clock extends EventTimer { private Driver theDriver; /** post: theDriver == d */ public Clock( Driver d, int t ) { super( t ); theDriver = d; } public void actionPerformed( ActionEvent e ) { theDriver.clockHasTicked(); } import java.awt.event.*; public class Clock extends EventTimer { private Driver theDriver; /** post: theDriver == d */ public Clock( Driver d, int t ) { super( t ); theDriver = d; } public void actionPerformed( ActionEvent e ) { theDriver.clockHasTicked(); }

5 © 2006 Pearson Addison-Wesley. All rights reserved 8.6.5 EventTextField Crash Course EventTextField «constructor» + EventTextField( int, int, int, int ) «query» + int getX() + int getY() + int getWidth() + int getHeight() + Color getParent() + String getText()... «update» + void repaint() + void setBounds( int, int, int, int ) + void setSize( int, int ) + void setLocation( int, int ) + void setText( String )... «event handler» + void actionPerformed( actionEvent )

6 © 2006 Pearson Addison-Wesley. All rights reserved 8.6.6 EventTextField Behavior import java.awt.event.ActionEvent; import java.util.Scanner; public class ReportingField extends EventTextField { public ReportingField() { super(); setBounds(10, 50, 180, 25); } public void actionPerformed (ActionEvent e) { String fieldString; double fieldDouble; boolean fieldBool; Scanner scanner; scanner = new Scanner( getText() ); if (scanner.hasNextDouble()) { fieldDouble = scanner.nextDouble(); } else if (scanner.hasNextBoolean()) { fieldBool = scanner.nextBoolean(); } else { fieldString = getText(); } // the code continues… } import java.awt.event.ActionEvent; import java.util.Scanner; public class ReportingField extends EventTextField { public ReportingField() { super(); setBounds(10, 50, 180, 25); } public void actionPerformed (ActionEvent e) { String fieldString; double fieldDouble; boolean fieldBool; Scanner scanner; scanner = new Scanner( getText() ); if (scanner.hasNextDouble()) { fieldDouble = scanner.nextDouble(); } else if (scanner.hasNextBoolean()) { fieldBool = scanner.nextBoolean(); } else { fieldString = getText(); } // the code continues… } An event occurs every time that the return key is struck within an EventTextField. With or without an event EventTextField content can be accessed by getText.

7 © 2006 Pearson Addison-Wesley. All rights reserved 8.6.7 EventSlider Crash Course EventSlider + static final int HORIZONTAL + static final int VERTICAL «constructor» + EventSlider( int, int, int, int ) «query» + int getX() + int getY() + int getWidth() + int getHeight() + int getMaximum() + int getMinimum() + Container getParent() + int getValue()... «update» + void repaint() + void setBounds( int, int, int, int ) + void setSize( int, int ) + void setLocation( int, int ) + void setMinimum( int ) + void setMaximum( int ) + void setValue( int )... «event handler» + void stateChanged( javax.swing.event.ChangeEvent ) knob getMinimum()getMaximum() getValue() called when user adjusts the knob or arrow buttons assigns new values to HORIZONTAL or VERTICAL getMinimum(), getMaximum() & getValue()

8 © 2006 Pearson Addison-Wesley. All rights reserved 8.6.8 import javax.swing.event.ChangeEvent; public class PrintingSlider extends EventSlider { private Driver driver; /** post: this slider object is positioned at (newX, newY) * and getWidth() == 150 and getHeight() == 30 * and driver == d */ public PrintingSlider( int newX, int newY, int mn, int mx, Driver d ) { super(HORIZONTAL, mn, mx, mn); setBounds(newX, newY, 150, 30); driver = d; } public void stateChanged( ChangeEvent e ) { driver.printSlider( getValue() ); } import javax.swing.event.ChangeEvent; public class PrintingSlider extends EventSlider { private Driver driver; /** post: this slider object is positioned at (newX, newY) * and getWidth() == 150 and getHeight() == 30 * and driver == d */ public PrintingSlider( int newX, int newY, int mn, int mx, Driver d ) { super(HORIZONTAL, mn, mx, mn); setBounds(newX, newY, 150, 30); driver = d; } public void stateChanged( ChangeEvent e ) { driver.printSlider( getValue() ); } EventSlider Subclass

9 © 2006 Pearson Addison-Wesley. All rights reserved 8.6.9 import javax.swing.JFrame; public class Driver { private JFrame theWindow; private PrintingSlider upperSlider, lowerSlider; public Driver() { theWindow = new JFrame(“Slider Test”); theWindow.setBounds(10, 10, 600, 400); theWindow.setLayout(null); theWindow.setVisible(true); upperSlider = new PrintingSlider( 100, 50, 0, 33, this ); theWindow.add( upperSlider, 0 ); lowerSlider = new PrintingSlider( 200, 90, -10, 10, this ); theWindow.add( lowerSlider, 0 ); theWindow.repaint(); } public void printSlider( int v ) { System.out.println( "Slider value: "+ v ); // Could do more with the slider’s value here. } import javax.swing.JFrame; public class Driver { private JFrame theWindow; private PrintingSlider upperSlider, lowerSlider; public Driver() { theWindow = new JFrame(“Slider Test”); theWindow.setBounds(10, 10, 600, 400); theWindow.setLayout(null); theWindow.setVisible(true); upperSlider = new PrintingSlider( 100, 50, 0, 33, this ); theWindow.add( upperSlider, 0 ); lowerSlider = new PrintingSlider( 200, 90, -10, 10, this ); theWindow.add( lowerSlider, 0 ); theWindow.repaint(); } public void printSlider( int v ) { System.out.println( "Slider value: "+ v ); // Could do more with the slider’s value here. }... sample Driver

10 © 2006 Pearson Addison-Wesley. All rights reserved 8.6.10 The Object diagram : Driver upperSlider : PrintingSliderlowerSlider: PrintingSlider driver


Download ppt "Lecture 8.5 Animating with EventTimer. © 2006 Pearson Addison-Wesley. All rights reserved 8.5.2 A Crash Course in the Use of Timed Events What kinds of."

Similar presentations


Ads by Google