Download presentation
Presentation is loading. Please wait.
1
Animating with EventTimer
Lecture 8.5 Animating with EventTimer
2
A Crash Course in the Use of Timed Events
What kinds of occurrences produce events? How does the event handling mechanism work? © 2006 Pearson Addison-Wesley. All rights reserved
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) © 2006 Pearson Addison-Wesley. All rights reserved
4
public class Clock extends EventTimer {
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 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(); © 2006 Pearson Addison-Wesley. All rights reserved
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.