Practical Session 10: Animation. Animation kinds There are three basic kinds of animation: 1. Moving object 2. Changing pictures 3. Combination of 1 and.

Slides:



Advertisements
Similar presentations
Web Design & Development Lecture 19. Java Graphics 2.
Advertisements

Mouse Listeners We continue our examination of GUIs by looking at how to interact with the mouse –Just as Java creates Events when the user interacts with.
Chapter 4 (Horstmann’s Book) Interface Types and Polymorphism: Graphics, Timer, Animation Hwajung Lee.
Multithreading : animation. slide 5.2 Animation Animation shows different objects moving or changing as time progresses. Thread programming is useful.
More Java Drawing in 2D Animations with Timer. Drawing Review A simple two-dimensional coordinate system exists for each graphics context or drawing surface.
JFrame JComponent JFrame JComponent JFrame JComponent.
Events CSC 171 FALL 2001 LECTURE 9. History: the ABC John Vincent Atanasoff, with John Berry, developed the machine we now call the ABC -- the.
Mouse Events and Keyboard Events
Event Handling Events and Listeners Timers and Animation.
1 Lecturte 14Lecture 7 Applications of Graphics Overview  Conversions Between Applications and Applets  Handling Mouse Events  Handling Keyboard Events.
Applet Class Hierarchy. Applet Methods called by the system public void init() When the applet is first loaded. Do initialization here. public void start()
Graphical User Interfaces
Animation To animate our programs, that is, to have real time interactions where objects move on the screen, we want to call repaint( ) every few milliseconds.
Mouse Events. Handling Mouse Events Java provides two listener interfaces to handle mouse events: MouseListener;  MouseListener;  MouseMotionListener.
Object-Oriented Programming (Java), Unit 19 Kirk Scott 1.
CS12420 – Swing and threads Lynda Thomas
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.
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
KeyListener and Keyboard Events Another type of listener listens for keyboard entry – the KeyListener which generates KeyEvents –to implement KeyListener,
GUI Components and Design Here we add one more component to our programs, JButtons –JButtons can only be inserted into JPanels (or JApplets) –Clicking.
Java Event Handling CSIS 3701: Advanced Object Oriented Programming.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Object-Oriented Programming (Java), Unit 19 Kirk Scott 1.
Previous programs used a JLabel for OUTPUT. Another Swing component that can be used for both user input and output is the JTextfield. Suppose we want.
1 Block1 – unit 2 (The Case study in Budd 5-6).  create a small application that uses the Abstract Windowing Toolkit (AWT)  Swing packages to simulate.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
Object-Oriented Programming (Java), Unit 18 Kirk Scott 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.
COMP 321 Week 2. Outline Event-Driven Programming Events, Event Sources, Event Listeners Button and Timer Events Mouse Events, Adapters.
An Introduction to Programming and Object Oriented Design using Java 3 rd Edition. Dec 2007 Jaime Niño Frederick Hosch Chapter 18 Integrating user interface.
 Definition: An event is an object thrown in response to a user of programmatic action  Definition: A listener is a series of methods that executes in.
Lec 16 Adding Mouse and KeyEvent handlers to an Applet Class.
Java Classes, Objects, and Events: A Preview JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria.
CS324e - Elements of Graphics and Visualization Java GUIs - Event Handling.
Animation To animate our programs, that is, to have real time interactions where objects move on the screen, we want to call repaint( ) every few milliseconds.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 10: Event Handling 1 Event Handling.
Java Dynamic Graphics.
עקרונות תכנות מונחה עצמים תרגול 6 - GUI. Outline  Game of Life  Painting.
1 Applications & Applets Standalone applications & Java applets Peter Mozelius DSV/UCSC.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 10: Event Handling 1 Chapter 10 Event Handling.
Mouse Listeners Moving the mouse will also generate events like the Timer –To have your program respond, you must implement either or both of MouseListener.
Computer Science 209 GUIs Model/View/Controller Layouts.
Graphical User Interfaces A Graphical User Interface (GUI) in Java is created with at least three kinds of objects: –components, events, and listeners.
Chapter 4 Interface Types and Polymorphism: Graphics, Timer, Animation.
עקרונות תכנות מונחה עצמים תרגול 7: אנימציה. בשבוע שעבר  paint  Graphics  repaint.
Nested Classes and Event Handling Chapter 10. Overview We explain how to write Java code that responds to events. The Timer class can be used to respond.
1/18H212Mouse and Timer Events H212 Introduction to Software Systems Honors Lecture #16: Mouse and Timer Events October 26, 2015.
GUI Tutorial 2. What did we do last time?  Basic flow  instance variables, set up in ctor, close operation, size, visible  JFrame  Event-driven programming.
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.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Sep 181 Example Program DemoTranslateEnglishGUI.java.
Java Threads 1 1 Threading and Concurrent Programming in Java Threads and Swing D.W. Denbo.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
Swing Timers A Swing timer fires one or more action events after a specified delay. You can use Swing timers in two ways:  To perform a task once, after.
Event Driven (Asynchronous) Programming. Event handling in Unity Subclass a class that contains event handling methods, and then override those methods.
Chapter 4 Interface Types and Polymorphism Part 2.
Modular Event Handling
Lecture 10 Graphics D&D 3.14, 4:10, 5.13, 6.15, 8.16, Date.
Lecture 9.5 Event Delegation.
Multimedia in Java Multimedia combines graphics, animation and sound
Chapter 4 Interface Types and Polymorphism Part 2
Miscellaneous Topics #6: Polygons GUI Components and Event Handlers
MVC Paradigm The MVC paradigm breaks applications or interfaces into three parts: the model, the view, and the controller. A --> 25 % B --> 60 % C -->
עקרונות תכנות מונחה עצמים
עקרונות תכנות מונחה עצמים תרגול 7: אנימציה
Drawing in Java.
Chapter 16 Event-Driven Programming
Events, Event Handlers, and Threads
Constructors, GUI’s(Using Swing) and ActionListner
עקרונות תכנות מונחה עצמים תרגול 7: אנימציה
Presentation transcript:

Practical Session 10: Animation

Animation kinds There are three basic kinds of animation: 1. Moving object 2. Changing pictures 3. Combination of 1 and 2.

Timer In order to get animation effect we need to update an image several times per second. We use javax.swing.Timer. A Timer periodically creates events of type ActionPerformed. For each event, the image can be changed or moved. Constructor syntax: Timer t = new Timer (delay, actionListener)

public class Ball extends JFrame implements ActionListener{ private Timer timer; private int x, y; private final int delay = 200; public Ball(){ x = 0; y = 0; timer = new Timer(delay,this); timer.start(); } public void paint(Graphics g){ super.paint(g); g.fillOval(x,y, 10, 10); } public void actionPerformed(ActionEvent e){ if (e.getSource() == timer){ x = x + 5; y = y + 5; repaint(); } }

Changing Pictures 1. Create an array of pictures. im = new ImageIcon[3]; im[0] = new ImageIcon("car1.gif"); im[1] = new ImageIcon("car2.gif"); im[2] = new ImageIcon("car3.gif"); 2. Define a field that represents the current image number. 3. The paint method draws the appropriate image. private int frame = 0; im[frame].paintIcon(this, g, x, y); 4. ActionPerformed, which is invoked by a timer, changes the current image. frame = (frame + 1) % 3;

public class Car extends JPanel implements ActionListener{ private int frame; private ImageIcon[] im; private Timer timer; private int x, y, dx, dy; public Car(){ frame = 0; x = 30; y = 30; dx = 15; dy = 15; im = new ImageIcon[3]; //... (create three images and assign to the array) timer = new Timer(200,this); timer.start(); } public void actionPerformed(ActionEvent e){ if (e.getSource() == timer){ frame = (frame + 1) % 3; x = (x + dx) % getSize().width; y = (y + dy) % getSize().height; repaint(); } } public void paint(Graphics g){ super.paint(g); im[frame].paintIcon(this, g, x, y); }

Responding to key press When a key is pressed a KeyEvent is initiated. Listening to KeyEvents is performed with KeyListener. public class Frame extends JFrame implements KeyListener{... public void keyPressed(KeyEvent e) { int code; code = e.getKeyCode());... } public static void main(String args []){ Frame frame = new Frame(); frame.addKeyListener(frame); }

Changing the Car example to respond to key press public class Car extends JPanel implements ActionListener, KeyListener{... public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_LEFT){ dx = -30; dy = 0; } if (e.getKeyCode() == KeyEvent.VK_RIGHT){ dx = 30; dy = 0; } if (e.getKeyCode() == KeyEvent.VK_UP){ dx = 0; dy = -30; } if (e.getKeyCode() == KeyEvent.VK_DOWN){ dy = 30; dx = 0; } }

Double Buffering Technique

The double buffering technique allows to overcome the flickering problem. Flickering appears because a drawing is deleted before it is repainted, and repaint takes time. Solution: Maintain an additional panel. Draw a new image on the additional panel. Replace the old image only once the new one is ready.

public void paint(Graphics g){ super.paint(g); for (int i = 0; i < matrix.size(); i++) for (int j =0; j < matrix.get(i).size(); j++) if (matrix.get(i).get(j).intValue() == 1) g.fillOval(i,j,5,5); g.fillOval(x,y,20,20); } Fixing the flickering problem in the Drawing-Canvas example Reminder: The original paint method of DrawingCanvas

public void paint(Graphics g){ //super.paint(g); Image offIm = createImage(getSize().width, getSize().height); Graphics offGr = offIm.getGraphics(); for (int i = 0; i < matrix.size(); i++) for (int j =0; j < matrix.get(i).size(); j++) if (matrix.get(i).get(j).intValue() == 1) offGr.fillOval(i, j, 5, 5 ); offGr.fillOval(x,y, 20, 20); g.drawImage(offIm, 0,0, this); } Fixing the flickering problem in the Drawing-Canvas example The corrected paint method