Animation in Java Disclaimer Rendering Performance Rendering or Performance issues are not covered here.

Slides:



Advertisements
Similar presentations
Chapter 4 (Horstmann’s Book) Interface Types and Polymorphism: Graphics, Timer, Animation Hwajung Lee.
Advertisements

Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 9: Oct 17-21, 2011 Aditya Mathur Department of Computer Science Purdue.
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
1 Chapter 7 Graphics and Event Handling. 2 Overview The java.awt and javax.swing packages and their subpackages support graphics and event handling. Many.
Java Beans & Serialization CS-328 Dick Steflik. Java Beans Java based component technology –originally developed to provide java with a component technology.
What is an object? Your dog, your desk, your television set, your bicycle. Real-world objects share two characteristics: They all have state and behavior;
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Chapter 6: Graphical User Interface (GUI) and Object-Oriented Design (OOD) J ava P rogramming: Program Design Including Data Structures Program Design.
A.k.a. GUI’s.  If you want to discuss your Lab 2 grade come see me this week. ◦ Office: 436 ERB. One hour prior to class ◦ Open to Appointments MWF 
LEGO NXT Robot Programming Introduction to Programming a Lego NXT robot in Java.
Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams.
Draw Shapes Introduction to simple graphics. What is a Component? A class that resides in the java.awt package Examples include: –Button, java.awt.Button.
Enhancing JavaScript Using Application Programming Interfaces (APIs)
Using the Netbeans GUI Builder. The Netbeans IDE provides a utility called the GUI Builder that assists you with creating Windows applications. The Netbeans.
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.
Chapter 1: Introducing JAVA. 2 Introduction Why JAVA Applets and Server Side Programming Very rich GUI libraries Portability (machine independence) A.
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,
3461A Readings from the Swing Tutorial. 3461A Overview  The follow is the Table of Contents from the trail “Creating a GUI with JFC/Swing” in the “The.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
CS 11 java track: lecture 4 This week: arrays interfaces listener classes inner classes GUI callbacks.
QML Qt Quick with QML and you can use JavaScript for engine along C++ Started to be released since late 2009 (Qt 4.7) Nokia focused on that for the Symbian/Meego.
Review of ICS 102. Lecture Objectives To review the major topics covered in ICS 102 course Refresh the memory and get ready for the new adventure of ICS.
Chapter 12 1 TOPIC 13B l Buttons and Action Listeners Window Interfaces Using Swing Objects.
 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/15) MVC and Swing Joel Adams and Jeremy Frens Calvin College.
2D Graphics: Advanced Topics Chapter 4. Bird’s Eye View  Overview of Computer Graphics  2D Graphics: Basics  2D Graphics: Rendering Details  2D Graphics:
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.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington More Interfaces.
Creating a GUI with JFC/Swing. What are the JFC and Swing? JFC –Java Foundation Classes –a group of features to help people build graphical user interfaces.
UID – Event Handling and Listeners Boriana Koleva
Lab 11: Animation User Interface Lab: GUI Lab Nov. 6 th, 2013.
Session 18 Chapter 8: Understanding Inheritance. Recall Exercise 2 From Tuesday It’s very annoying to move a target from the pallet and drop it in the.
1 CSC 222: Computer Programming II Spring 2005 Java interfaces & polymorphism  Comparable interface  defining an interface  implementing an interface.
Java Beans. Definitions A reusable software component that can be manipulated visually in a ‘builder tool’. (from JavaBean Specification) The JavaBeans.
Threads II IS Outline  Quiz  Thread review  Stopping a thread  java.util.Timer  Swing threads javax.swing.Timer  ProgressMonitor.
CS324e - Elements of Graphics and Visualization Timing Framework.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Layout How multiple components are composed to build interfaces.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 5 Objectives  Learn about basic GUI components.  Explore how the GUI.
Practical Session 10: Animation. Animation kinds There are three basic kinds of animation: 1. Moving object 2. Changing pictures 3. Combination of 1 and.
A cannon game ?. Simple version angle from command line, one shot only Coordinate system is “upside-down”: Use dy(int) method to transform y coordinate:
CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 9: Oct 18-22, 2010 Aditya Mathur Department of Computer Science Purdue.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
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,
Computer Science II 810:062 Section 01 Session 2 - Objects and Responsibilities.
1/18H212Mouse and Timer Events H212 Introduction to Software Systems Honors Lecture #16: Mouse and Timer Events October 26, 2015.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Graphical User Interface (GUI)
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
7/19: Primitives, the for loop Primitive data types –why we mention them Return to counter-controlled repetition.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
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.
Notices Assn 4 posted. Due last day of class. Note that Exercise 10 contains the JavaFX code for three simple GUI programs. Winter 2016CMPE212 - Prof.
Event Driven (Asynchronous) Programming. Event handling in Unity Subclass a class that contains event handling methods, and then override those methods.
ME 120: Arduino PWM Breathing LED Code: Implementation on Arduino ME 120 Mechanical and Materials Engineering Portland State University
 Movieclip symbols are reusable pieces of flash animation  consisting usually of one or more graphic/button symbols  thus they are flash movies within.
CSC 205 Programming II Lecture 5 AWT - I.
Java Programming: From Problem Analysis to Program Design,
Chapter 15 Event-Driven Programming and Animations
Threads II IS
Inner Classes 11/14/ Dec-04 inner_classes.ppt.
Inner Classes 29-Nov-18.
Constructors, GUI’s(Using Swing) and ActionListner
Chapter 5 Processing Input with Applets
Inner Classes 17-Apr-19.
Inner Classes 21-Apr-19.
Inner Classes 11-May-19.
Inner Classes 18-May-19.
Presentation transcript:

Animation in Java

Disclaimer Rendering Performance Rendering or Performance issues are not covered here

Simplest Example

Using Timer

2 Timers in Java World java.util.Timer –for general use calls java.util.TimerTask instance –good for scheduling tasks specifying time for task javax.swing.Timer –GUI specific calls java.awt.event.ActionListener instance –called on EDT

Working Implementation Example1 labelPanel.getObj()

Code & Demo of Example 1 timer = new Timer(10, new ActionListener() { public void actionPerformed(ActionEvent e) { if(xPos < xEndPos) { xPos += xDiff; JComponent obj = labelPanel.getObject(); obj.setLocation( xPos, obj.getY() ); } else { timer.stop(); xPos = 0; } }); timer.start();

Is it enough? What about –Repetition –Duration –On slow machine –Non-linear interpolation –Reverse direction Back and Forth

Time based animation v = f(t)

Key Concepts of Timing Model Cycle –Smallest unit for animation Envelope –container of same cycles TimingController –uses Cycles & Envelopes –Synthesize TimingEvent(s) TimingTarget –Interface we should implements –Modify value based on timing event

Cycle

Envelope

Envelope ( bonus ) Repeat Behavior –FORWARD or REVERSE –at the end of each cycles End Behavior –HOLD or RESET –at the end of the Envelope

Timing Target Interface we should implement callback for timing events where the formula (v=f(t)) resides

Example 2 // duration : 1s, resolution : 10 ms Cycle horizontalCycle = new Cycle( 1000, 10 ); // repeat 5 times, beginning delay : 0 ms Envelope simpleEnv = new Envelope( 5, 0, Envelope.RepeatBehavior.FORWARD, Envelope.EndBehavior.HOLD ); TimingTarget tt = new TimingTarget() { public void begin() {} public void end() {} public void timingEvent(long cycleElapsedTime, long totalElapsedTime, float fraction) { int x = (int)(fraction * 400); JComponent obj = labelPanel.getObject(); obj.setLocation( x, obj.getY() ); } }; TimingController controller = new TimingController( horizontalCycle, simpleEnv ); controller.addTarget( tt ); controller.start();

Demo of Example 2 Duration Resolution Beginning delay Repeat Count Repeat Behavior

Hardcore Animation Non-linear Interpolation Acceleration Deceleration Property Range Object Modifier Trigger Multi-Step Animation

Non-linear Interpolation Real World is non-linear –Gravity, Acceleration, friction… Non-linear movement looks more natural see KeySplines, KeyFrames Ease-inEase-in, Ease-out Quick Start

Acceleration / Deceleration Simple & useful non-linear interpolation Float value between 0 and 1 Fraction of time spent speeding up or slowing down Demo please –modifying Example 2

Property Setter Property is CBD term for field or member variable Animate property of JavaBeans Consists of –PropertyRange property name + value range int, double, float, Dimension, Point, Color, Rectangle –Object modifier implements TimingTarget

Example 3 & Demo Cycle horizontalCycle = new Cycle( 1000, 10 ); Envelope simpleEnv = new Envelope( 1, 0, RepeatBehavior.REVERSE, EndBehavior.HOLD ); Rectangle from = new Rectangle( 0, 0, 0, 0 ); Rectangle to = new Rectangle( 100, 100, 200, 200 ); PropertyRange boundsRange = PropertyRange.createPropertyRangeRectangle("bounds", from, to); TimingTarget tt = new ObjectModifier( labelPanel.getObject(), boundsRange ); TimingController controller = new TimingController( horizontalCycle, simpleEnv ); controller.addTarget( tt ); controller.start();

Trigger Utilities to reduce event listener code Useful for –Hover effect for buttons –Pulsating effect for focus events –Animation on action events ActionTrigger, ButtonStateTrigger, ComponentFocusTrigger, TimingTrigger

Trigger Example from JavaOne2006 TS-1297 Slide

Multi-Step Animations KeyValues, KeyTimes, KeySplines, KeyFrames (Example 4) Cycle horizontalCycle = new Cycle( 1000, 10 ); Envelope simpleEnv = new Envelope( 1, 0, REVERSE, HOLD ); Point from = new Point( 0, 10 ); Point mid = new Point ( 10, 300 ); Point to = new Point( 400, 100 ); // variable length arguments KeyValues values = KeyValues.createKeyValues(from, mid, to); KeyFrames frames = new KeyFrames( values ); PropertyRange boundsRange = new PropertyRange("location", frames ); TimingTarget tt = new ObjectModifier( labelPanel.getObject(), boundsRange ); TimingController controller = new TimingController( horizontalCycle, simpleEnv ); controller.addTarget( tt ); controller.start();

Application Not only for location, size, bounds can apply for everywhere Example 5 – Fading Animation Demo

Reference Timing is Everything – ming.htmlhttp://today.java.net/pub/a/today/20 05/02/15/timing.htmlhttp://today.java.net/pub/a/today/2005/02/15/ti ming.htmlhttp://today.java.net/pub/a/today/20 05/02/15/timing.html Time Again – me-again.htmlhttp://today.java.net/pub/a/today/2006/03/16/ti me-again.html Timing Framework – JavaOne 2006 TS-1297 “Filthy Rich Clients: Animated Effects in Swing Applications”