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.

Slides:



Advertisements
Similar presentations
Rectangles moving and responding to the mouse. We want a window with a pile of rectangles in it When we click a rectangle it changes from filled to unfilled.
Advertisements

Web Design & Development Lecture 19. Java Graphics 2.
Inheritance // A simple class hierarchy. // A class for two-dimensional objects. class TwoDShape { double width; double height; void showDim() { System.out.println("Width.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
Problem Solving 6 GUIs and Event Handling ICS-201 Introduction to Computing II Semester 071.
Multithreading : animation. slide 5.2 Animation Animation shows different objects moving or changing as time progresses. Thread programming is useful.
Bar Graph Design. Left-side/Right-side mechanical processing creative, abstract reasoning.
CompSci Today’s topics Java Recursion in Graphics Writing a Class Simple Animation Upcoming Simulation Reading Great Ideas, Chapters 5.
Event-Driven Programming You might have realized in creating a GUI and clicking on the JButtons that nothing happens Clicking on a JButton causes the JVM.
Lecture 10.2 Different Types of Loops, including for and do.
 2002 Prentice Hall, Inc. All rights reserved. Chapter 14 – Exception Handling Outline 14.1 Introduction 14.2 When Exception Handling Should Be Used 14.3.
© 2006 Pearson Addison-Wesley. All rights reserved Inheritance When one class inherits another class, the new class becomes a specialized version.
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
Lecture 19 Graphics User Interfaces (GUIs)
Object Oriented Programming Java 1 GUI example taken from “Computing Concepts with Java 2” by Cay Horstmann GUI Programming.
1 Class 8. 2 Chapter Objectives Use Swing components to build the GUI for a Swing program Implement an ActionListener to handle events Add interface components.
Io package as Java’s basic I/O system continue’d.
Object-Oriented Programming in Java. Object-Oriented Programming Objects are the basic building blocks in an O-O program. – A program consists of one.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 20.1 Test-Driving the Screen Saver Application.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
© 2006 Pearson Addison-Wesley. All rights reserved Inheritance Systems Merchandise ElectronicsClothing TelevisionCamcorderShirtDressShoe DigitalAnalog.
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Inheritance Like father like son Image from:
CS 11 java track: lecture 4 This week: arrays interfaces listener classes inner classes GUI callbacks.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, this reading: self-checks: #13-17 exercises:
© A+ Computer Science - Chicken yeller = new Chicken();
Lecture 8.3 The Use of JComponent. © 2006 Pearson Addison-Wesley. All rights reserved More About the Standard Drawing Classes java.awt.Container.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
CS/ENGRD 2110 SPRING 2012 Lecture 2: Objects and classes in Java 1.
Swing GUI Components You can create graphics components to place on your applet using classes available in the Swing package ( javax.swing ) Class names.
EVENTS Wiring together objects, code, and actions.
Java Graphics Graphical Components as objects. Graphics A Component is ◦A rectangular region of a computer screen ◦A graphical entity ◦Can sometimes contains.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
Anonymous Classes An anonymous class is a local class that does not have a name. An anonymous class allows an object to be created using an expression.
1 GUI programming Graphical user interface-based programming Chapter G1 (pages )
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 24.1 Test-Driving the Enhanced Car Payment.
4.3.1 Non-void Methods Parameters are largely one-way communication.  Shared instances variables is one way to accomplish this. calling codemethod parameter.
Practical Session 10: Animation. Animation kinds There are three basic kinds of animation: 1. Moving object 2. Changing pictures 3. Combination of 1 and.
More GUI Components We finish off our examination of GUI components today by looking at a few that we haven’t seen yet –we start off with the JSlider the.
Lecture 3.1 Using Graphics with JFrame. © 2006 Pearson Addison-Wesley. All rights reserved javax.swing.JFrame - int x - int y - int width - int.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 27.1 Test-Driving the Drawing Shapes Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 4 – Completing the Inventory Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 5.1 Test-Driving the Enhanced Inventory Application.
© 2006 Pearson Addison-Wesley. All rights reserved Non-void Methods Parameters are largely one-way communication.  Shared instances variables is.
Week 12 - Wednesday.  What did we talk about last time?  Hunters and prey.
Week 12 - Monday.  What did we talk about last time?  Defining classes  Class practice  Lab 11.
Lecture 8.2 Aggregation and Inheritance. © 2006 Pearson Addison-Wesley. All rights reserved Designing with Aggregation and Inheritance Software.
CS/ENGRD 2110 SPRING 2016 Lecture 2: Objects and classes in Java 1.
© 2006 Pearson Addison-Wesley. All rights reserved Design by Prototype Programmers often approach a problem by creating a series of prototypes. Each.
KeyListener and Keyboard Events Just as we can implement listeners to handle mouse events, we can do the same for keyboard events (keypresses) –to implement.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 17.1 Test-Driving the Student Grades Application.
Lecture 4.1 More About Methods - Using a Prototyping Approach.
© 2006 Pearson Addison-Wesley. All rights reserved Arrays of Greater Dimension One-dimensional arrays are linear containers. Multi-dimensional Arrays.
MIT AITI 2004 Swing Event Model Lecture 17. The Java Event Model In the last lecture, we learned how to construct a GUI to present information to the.
Lecture 8.5 Animating with EventTimer. © 2006 Pearson Addison-Wesley. All rights reserved A Crash Course in the Use of Timed Events What kinds of.
Event Driven (Asynchronous) Programming. Event handling in Unity Subclass a class that contains event handling methods, and then override those methods.
Introducing, the JFrame Gives us a work area beside System.out.println.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 7.1 Test-Driving the Dental Payment Application.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
Lecture 15.1 Event Delegation.
Aggregation and Inheritance
Animating with EventTimer
Lecture 9.5 Event Delegation.
public class Doubler extends Base {
CS/ENGRD 2110 Spring 2018 Lecture 2: Objects and classes in Java
Introduction to Event Handling
Class Hierarchies and Type Conformance
Chapter 5 Processing Input with Applets
Presentation transcript:

Lecture 8.5 Animating with EventTimer

© 2006 Pearson Addison-Wesley. All rights reserved 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 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 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(); }

© 2006 Pearson Addison-Wesley. All rights reserved 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 )

© 2006 Pearson Addison-Wesley. All rights reserved 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.

© 2006 Pearson Addison-Wesley. All rights reserved 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()

© 2006 Pearson Addison-Wesley. All rights reserved 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

© 2006 Pearson Addison-Wesley. All rights reserved 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

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