Download presentation
Presentation is loading. Please wait.
Published byAugustus Osborne Modified over 8 years ago
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(); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.