13-Jun-15 Animation. 2 Moving pictures Animation—making objects that appear to move on the screen—is done by displaying a series of still pictures, one.

Slides:



Advertisements
Similar presentations
Made with love, by Zachary Langley Applets The Graphics Presentation.
Advertisements

Java Applets- Using SwingWorker Dave Price and Chris Loftus Computer Science Department University of Wales, Aberystwyth.
Unit 141 Threads What is a Thread? Multithreading Creating Threads – Subclassing java.lang.Thread Example 1 Creating Threads – Implementing java.lang.Runnable.
(c) 2006 E.S.Boese All Rights Reserved. Threads and Timers Chapter 19 - Student.
13-Jun-15 Model-View-Controller. 2 Design Patterns The hard problem in O-O programming is deciding what objects to have, and what their responsibilities.
Event Handling Events and Listeners Timers and Animation.
22-Jun-15 Threads and Multithreading. 2 Multiprocessing Modern operating systems are multiprocessing Appear to do more than one thing at a time Three.
24-Jun-15 Simple Animation. 2 The bouncing ball The “bouncing ball” is to animation what “Hello world” is to programming With a single Thread, we can.
Model-View-Controller. Design Patterns The hard problem in O-O programming is deciding what objects to have, and what their responsibilities are Design.
Concurrency Java Threads. Fundamentals Concurrency defines parallel activity Synchronization is necessary in order for parallel activities to share results.
29-Jun-15 Threads and Multithreading. 2 Multiprocessing Modern operating systems are multiprocessing Appear to do more than one thing at a time Three.
29-Jun-15 Model-View-Controller. 2 Design Patterns The hard problem in O-O programming is deciding what objects to have, and what their responsibilities.
14-Jul-15 Model-View-Controller. 2 Design Patterns The hard problem in O-O programming is deciding what objects to have, and what their responsibilities.
Threads II. Review A thread is a single flow of control through a program Java is multithreaded—several threads may be executing “simultaneously” If you.
Java: Animation Chris North cs3724: HCI. Animation? Changing graphics over time Examples: cartoons Clippy, agents/assistants Hour glass Save dohicky Progress.
Io package as Java’s basic I/O system continue’d.
Java Programming: Advanced Topics
(c) 2008 E.S.Boese All Rights Reserved. Threads and Media Chapter 8 - Lecture Slides 1.
CS12420 – Swing and threads Lynda Thomas
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
Chapter 19 Designing the GUI front-end: the Model-View-Controller pattern.
The Model-View Approach in Teaching Java Maria Litvin Phillips Academy, Andover, MA
Week 3, Day 1: Processes & Threads Processes Threads SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder 1.
CSC 205 – Java Programming II Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are stand-alone.
GUIs for Video Games. The Plan ● Layout suggestions ● Observer/Observable ● Steps to writing Observer/Observables ● Sample Code ● Practice.
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.
CSC 313 – Advanced Programming Topics. Observer Pattern in Java  Java ♥ Observer Pattern & uses everywhere  Find pattern in JButton & ActionListener.
1 Java Model-View-Controller - see programming.html#concepts.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
1 G53SRP: Clocks and Time in Java Chris Greenhalgh School of Computer Science.
Threads. Story so far  Our programs have consisted of single flows of control Flow of control started in the first statement of method main() and worked.
Threads II IS Outline  Quiz  Thread review  Stopping a thread  java.util.Timer  Swing threads javax.swing.Timer  ProgressMonitor.
Model-View-Controller design pattern. Design Patterns “Making abstractions which are powerful and deep is an art. It requires tremendous ability to go.
7-Dec-15 Animation. HW 10 Last HW (I messed up some of the scheduling) Extension until Monday night 11:30 pm Late penalty will be strictly enforced for.
Java Model-View-Controller. Design Patterns The hard problem in O-O programming is deciding what objects to have, and what their responsibilities are.
(1) Introduction to Java GUIs Philip Johnson Collaborative Software Development Laboratory Information and Computer Sciences University of Hawaii Honolulu.
6-Jan-16 Model-View-Controller. 2 Design Patterns The hard problem in O-O programming is deciding what objects to have, and what their responsibilities.
Computer Science 209 GUIs Model/View/Controller Layouts.
1 CSE 331 Model/View Separation and Observer Pattern slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia.
Applets Java code is compiled into byte code instead of machine language –Languages like C, C++, Pascal and others are compiled into machine language so.
Threads and Multithreading. Multiprocessing Modern operating systems are multiprocessing Appear to do more than one thing at a time Three general approaches:
L10: Model-View-Controller General application structure. User Interface: Role, Requirements, Problems Design patterns: Model – View – Controller, Observer/Observable.
Conway’s Game Of Live 1 Fall 2014 CS7020: Game Design and Development.
Chapter 11: Threaded Programs Situations where the program is following multiple execution paths (how to stop one?) Thread: a line of execution Thread.
21-Jun-16 Swing Basics and Model-View- Controller.
Threads.
Threads and Multithreading
GUIs for Video Games.
Animation 12-Sep-18.
Animation 6-Nov-18.
Threads II IS
Model-View-Controller
Animation 15-Nov-18.
Animation 18-Nov-18.
Threads and Multithreading
Threads and Multithreading
Threads and Multithreading
Threads and Multithreading
Threads and Multithreading
Animation 25-Feb-19.
Threads and Multithreading
Threads and Multithreading
Threads and Multithreading
Threads and Multithreading
Threads.
Threads.
Animation 17-Sep-19.
Mobile Computing With Android ACST 4550 Android Animation
Presentation transcript:

13-Jun-15 Animation

2 Moving pictures Animation—making objects that appear to move on the screen—is done by displaying a series of still pictures, one after the other, in rapid succession Generally you should try for at least 20 pictures/second 20 pictures/second is repainting every 50 milliseconds

3 The bouncing ball “Bouncing ball” is the “Hello World” of animation We will develop this program using: Model-View-Controller Observer-Observable Threads Timers

4 Review of MVC MVC stands for Model-View-Controller The Model is the actual internal representation It should be independent of the other classes It’s handy if this class can extend Observable The View (or a View) is a way of looking at or displaying the model It’s handy if this class implements Observer The Controller provides for user input and modification These three components are usually implemented as separate classes (or sets of classes)

5 Review of Observer and Observable java.util.Observable is a class When it does something that should be observed, it says: setChanged(); notifyObservers(); /* or */ notifyObservers( arg ); java.util.Observer is an interface It has to register itself with (subscribe to) an Observable: myObservable.addObserver( myObserver ); It has to implement: public void update(Observable obs, Object arg ) This method is automatically called when observers are notified obs is the object being observed If the Observable did notifyObservers(), arg is null

6 Review of Thread s You can extend the Thread class: class Animation extends Thread {…} Limiting, since you can only extend one class You must override public void run( ) To make it “go”: Animation anim = new Animation( ); anim.start( ); Or you can implement the Runnable interface: class Animation implements Runnable {…} You must implement public void run( ) To make it “go”: Animation anim = new Animation( ); Thread myThread = new Thread(anim); myThread.start( );

7 Timer s A java.util.Timer is used to schedule code for future execution A Timer may: Schedule a one-time execution, or Schedule repeated executions at regular intervals Timer constructors: Timer() Timer(boolean isDaemon ) Timer(String name ) Timer(String name, boolean isDaemon ) A Timer can keep an application from terminating, unless it is specified as a daemon thread A daemon thread dies if there are no no-daemon threads running Create daemon Timer threads with new Timer(true) or new Timer( name, true)

8 Using a Timer for animation public void schedule(TimerTask task, long delay, long period ) Schedules the specified task for repeated fixed-delay execution, beginning after the specified delay (which may be zero) Subsequent executions take place at approximately regular intervals separated by the specified period Times are specified in milliseconds (1/1000s of a second) Notice that schedule requires a TimerTask as an argument TimerTask is an abstract class you must extend and provide a public void run() method TimerTask provides an (implemented) public boolean cancel() method Returns false if there were no scheduled executions to cancel

9 The Model class, I class Model extends Observable { public final int BALL_SIZE = 20; private int xPosition = 0; private int yPosition = 0; private int xLimit, yLimit; private int xDelta = 6; private int yDelta = 4; // methods (on next slide) }

10 The Model class, II public void setLimits(int xLimit, int yLimit) { this.xLimit = xLimit - BALL_SIZE; this.yLimit = yLimit - BALL_SIZE; } public int getX() { return xPosition; } public int getY() { return yPosition; } public void makeOneStep() { // code for making one step (on next slide) }

11 The Model class, III public void makeOneStep() { // Do the work xPosition += xDelta; if (xPosition = xLimit) { xDelta = -xDelta; xPosition += xDelta; } yPosition += yDelta; if (yPosition = yLimit) { yDelta = -yDelta; yPosition += yDelta; } // Notify observers setChanged(); notifyObservers(); }

12 The View class import java.awt.*; import java.util.*; class View extends Panel implements Observer { Model model; View(Model model) { this.model = model; } public void paint(Graphics g) { g.setColor(Color.red); g.fillOval(model.getX(), model.getY(), model.BALL_SIZE, model.BALL_SIZE); } public void update(Observable obs, Object arg) { repaint(); } }

13 The Controller class, I import java.applet.*; import java.awt.*; import java.awt.event.*; import java.util.Timer; import java.util.TimerTask; import javax.swing.*; public class Controller extends JApplet { JPanel buttonPanel = new JPanel(); JButton runButton = new JButton("Run"); JButton stopButton = new JButton("Stop"); Timer timer; Model model = new Model(); View view = new View(model); // View must know about Model

14 The Controller class, II public void init() { layOutComponents(); attachListenersToComponents(); // Connect model and view model.addObserver(view); } private void layOutComponents() { setLayout(new BorderLayout()); this.add(BorderLayout.SOUTH, buttonPanel); buttonPanel.add(runButton); buttonPanel.add(stopButton); stopButton.setEnabled(false); this.add(BorderLayout.CENTER, view); }

15 The Controller class, III private void attachListenersToComponents() { runButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { runButton.setEnabled(false); stopButton.setEnabled(true); timer = new Timer(true); timer.schedule(new Strobe(), 0, 40); // 25 times a second } }); stopButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { runButton.setEnabled(true); stopButton.setEnabled(false); timer.cancel(); } }); }

16 The Controller class, IV private class Strobe extends TimerTask { public void run() { model.setLimits(view.getWidth(), view.getHeight()); model.makeOneStep(); } }

17 Summary In this program I used: Model-View-Controller This is a good design pattern for many uses; it separates the “business logic” (the Model) from the classes that are basically I/O Observer-Observable This is a good design pattern for helping to isolate the Model from the View Threads If you want to have a controllable animation, Threads are essential The animation runs in one Thread, the controls in another Timers Timers are a convenient way to schedule regularly repeating tasks With a slightly different design, you could use Thread.sleep( ms ) instead

18 The End