C OMP 401 A NIMATION L OOPS Instructor: Prasun Dewan.

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
Ade Azurat, Advanced Programming 2004 (Based on LYS Stefanus’s slides) Advanced Programming 2004, Based on LYS Stefanus’s slides Slide 2.1 Multithreading.
Locks (Java 1.5) Only one thread can hold a lock at once –Other threads that try to acquire it block (or become suspended) until lock becomes available.
Unit 141 Threads What is a Thread? Multithreading Creating Threads – Subclassing java.lang.Thread Example 1 Creating Threads – Implementing java.lang.Runnable.
Albert Johnson Molly Richardson Andrew Kruth.  Threads Runnable interface sleep()  GUIs repaint() paintComponent()
C OMP 110 L OOPS Instructor: Jason Carter. 2 L OOPS More loops Off-by-one errors Infinite loops Nested Loops Animations Concurrency Synchronized methods.
Object-Oriented Software Engineering Concurrent Programming.
Slides prepared by Rose Williams, Binghamton University Chapter 20 Java Never Ends.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
28-Jun-15 Producer-Consumer An example of using Threads.
Lecture 17: Animation Yoni Fridman 7/27/01 7/27/01.
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L20 (Chapter 24) Multithreading.
A Bridge to Your First Computer Science Course Prof. H.E. Dunsmore Concurrent Programming Threads Synchronization.
C OMP 110 L OOPS A DVANCED Instructor: Prasun Dewan.
C OMP 110/401 C OMPOSITE A NNOTATIONS Instructor: Prasun Dewan.
Lecture 5 : JAVA Thread Programming Courtesy : MIT Prof. Amarasinghe and Dr. Rabbah’s course note.
1 CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
Programming Fundamentals 2: Interaction/ F II Objectives – –introduce modularization and abstraction – –explain how an object uses other.
C OMP 110/401 A PPENDIX : I NSTALLING AND U SING B ARE B ONES D EVELOPMENT E NVIRONMENT ON U NIX Instructor: Prasun Dewan (FB 150,
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 11 Fall 2010.
Java Threads. What is a Thread? A thread can be loosely defined as a separate stream of execution that takes place simultaneously with and independently.
J McQuillan SE204: 2004/2005: Lecture 4slide 1 The Graphics Class Used when we need to draw to the screen Two graphics classes –Graphics –Graphics2D.
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.
CSC 205 – Java Programming II Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are stand-alone.
Swing GUI Components You can create graphics components to place on your applet using classes available in the Swing package ( javax.swing ) Class names.
Chapter 5: Ball Worlds Features 2 classes, constant data fields, constructors, extension through inheritance, graphics.
Concurrent Programming and Threads Threads Blocking a User Interface.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
1 Building Java Programs Supplement 3G: Graphics These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold,
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
In Java processes are called threads. Additional threads are associated with objects. An application is associated with an initial thread via a static.
Threading and Concurrency COM379T John Murray –
Concurrent Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
File Input and Graphics An extract of slides from set 05_inout.ppt Designed to get you ready for the HW3.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
C OMP 401 A NIMATION : MVC Instructor: Prasun Dewan.
C OMP 401 U SER -I NTERFACE VS. M AIN T HREADS Instructor: Prasun Dewan.
Using classes. One step instantiation, Composition I JFrame myWindow = new JFrame( ); Two step Instantiation, Composition II private JFrame myWindow;
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 More on Thread API.
Multithreaded programming  Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run.
C OMP 401 R EFLECTION AND A CTION O BJECTS Instructor: Prasun Dewan.
Winter 2006CISC121 - Prof. McLeod1 Stuff No stuff today!
CS305j Introduction to Computing Simple Graphics 1 Topic 11 Simple Graphics "What makes the situation worse is that the highest level CS course I've ever.
1 Java Programming Java Programming II Concurrent Programming: Threads ( I)
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Threads b A thread is a flow of control in a program. b The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.
Java Applets Adding Animation. Import Files You still need to include the same files: –import java.applet.*; –import java.awt.*;
Programming and Problem Solving With Java Copyright 1999, James M. Slack Threads and Animation Threads Animation.
C OMP 110/401 D OCUMENTATION : A NNOTATIONS Instructor: Prasun Dewan.
JAVA METHODS (FUNCTIONS). Why are they called methods? Java is a strictly object-oriented programming language Methods are functions inside of objects.
Java Threads 1 1 Threading and Concurrent Programming in Java Threads and Swing D.W. Denbo.
C OMP 401 C OPY : S HALLOW AND D EEP Instructor: Prasun Dewan.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
1 Handling Errors and Exceptions Chapter 6. 2 Objectives You will be able to: 1. Use the try, catch, and finally statements to handle exceptions. 2. Raise.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Sophomore Scholars Java
Lecture 27 Creating Custom GUIs
Threads Chate Patanothai.
Condition Variables and Producer/Consumer
Condition Variables and Producer/Consumer
slides created by Alyssa Harding
Multithreading.
Comp 401 Concluding Remarks
class PrintOnetoTen { public static void main(String args[]) {
Recitation 12 November 18, 2011.
When a function is called...
Review of Previous Lesson
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Presentation transcript:

C OMP 401 A NIMATION L OOPS Instructor: Prasun Dewan

2 P REREQUISITE Loops Advanced

3 S HUTTLE A NIMATION

4 B ASIC A RCHITECTURE APlotted Shuttle APlotted Shuttle AShuttleAnimator main setShuttleX(Y)() Main Class animateFromOrigin()

5 A NIMATE F ROM O RIGIN S EMANTICS Basic Idea: Should animate from origin to current location Should move to origin Should animate first in Y direction to current Y Next should animate in X direction to current X

6 public void animateFromOrigin(PlottedShuttle shuttle, int animationStep, int animationPauseTime) { int originalX = shuttle.getShuttleX(); int originalY = shuttle.getShuttleY(); int curX = 0; int curY = 0; shuttle.setShuttleX(curX); shuttle.setShuttleY(curY); animateYFromOrigin(shuttle, animationStep, animationPauseTime, curY, originalY); animateXFromOrigin(shuttle, animationStep, animationPauseTime, curX, originalX); } A NIMATE FROM O RIGIN

7 A NIMATING TO A D ESTINATION L OCATION 1. Should move some distance (< total_distance) in straight line to destination 2. Go back to 1 if location not reached

8 A NIMATING TO A D ESTINATION L OCATION 1. Should move a constant distance (< total_distance) in straight line to destination 2. Should pause for some time3. Go back to 1 if location not reached

9 P AUSING P ROGRAM * ASSIGNMENT_TIME different on different computers! Unnecessarily using the CPU (Busy Waiting) Need the OS to suspend program for pause time void sleep ( int pauseTime) { int numberOfAssignments = pauseTime; //ASSIGNMENT_TIME; for ( int i = 0; i < numberOfAssignments; i++) { int dummy = 0; // nonsense assignment }

10 P AUSING P ROGRAM : T HREAD S UPPORT.S LEEP () public void sleep( int pauseTime) { try { // OS suspends program for pauseTime Thread.sleep(pauseTime); } catch (Exception e) { // program may be forcibly interrupted while sleeping e.printStackTrace(); }; }

11 A NIMATION IN Y D IRECTION protected void animateYFromOrigin(PlottedShuttle shuttle, int animationStep, int animationPauseTime, int startY, int endY) { // make sure we don’t go past final Y position while (startY < endY) { ThreadSupport.sleep(animationPauseTime); startY += animationStep; shuttle.setShuttleY(startY); } // move to destination Y position shuttle.setShuttleY(endY); }

12 A NIMATION IN X D IRECTION protected void animateXFromOrigin(PlottedShuttle shuttle, int animationStep, int animationPauseTime, int startX, int endX) { while (startX < endX) { ThreadSupport.sleep(animationPauseTime); startX += animationStep; shuttle.setShuttleX(startX); } shuttle.setShuttleX(endX); }

13 A NIMATE F ROM O RIGIN public void animateFromOrigin(PlottedShuttle shuttle, int animationStep, int animationPauseTime) { int originalX = shuttle.getShuttleX(); int originalY = shuttle.getShuttleY(); int curX = 0; int curY = 0; shuttle.setShuttleX(curX); shuttle.setShuttleY(curY); animateYFromOrigin(shuttle, animationStep, animationPauseTime, curY, originalY); animateXFromOrigin(shuttle, animationStep, animationPauseTime, curX, originalX); }

14 M AIN M ETHOD public static void main(String[] args) { PlottedShuttle shuttle = new APlottedShuttle(50, 100); OEFrame oeFrame = ObjectEditor.edit(shuttle); oeFrame.hideMainPanel(); oeFrame.setSize (450, 450); ShuttleAnimator shuttleAnimator = new AShuttleAnimator(); shuttleAnimator.animateFromOrigin(aShuttle, 5, 100); } No animation, nothing happens on the screen Using non observable version of plotted shuttle

15 protected void animateYFromOrigin(PlottedShuttle shuttle, int animationStep, int animationPauseTime, int startY, int endY) { // make sure we don’t go past final Y position while (startY < endY) { ThreadSupport.sleep(animationPauseTime); startY += animationStep; shuttle.setShuttleY(startY); } // move to destination Y position shuttle.setShuttleY(endY); } N O R EFRESH Shuttle being displayed by some GUI code (e.g. ObjectEditor) GUI needs to be told when shuttle coordinates change Even if the method was called by it and it does automatic refresh Automatic refresh done when the method returns, not at indeterminate intermediate points

16 protected void animateYFromOrigin(PlottedShuttle shuttle, int animationStep, int animationPauseTime, int startY, int endY, OEFrame frame) { // make sure we don’t go past final Y position while (startY < endY) { ThreadSupport.sleep(animationPauseTime); startY += animationStep; shuttle.setShuttleY(startY); frame.refresh(); } // move to destination Y position shuttle.setShuttleY(endY); } M ANUAL F ULL R EFRESH

17 M ANUAL F ULL R EFRESH public class ShuttleAnimationDriver { public static void demoShuttleAnimation( ShuttleAnimator aShuttleAnimator, PlottedShuttle aShuttle, OEFrame anOEFrame) { aShuttleAnimator.animateFromOrigin(aShuttle, 5, 100); aShuttleAnimator.animateFromOrigin(aShuttle, 5, 100, anOEFrame); } public static void main(String[] args) { PlottedShuttle shuttle = new APlottedShuttle(50, 100); OEFrame oeFrame = ObjectEditor.edit(shuttle); oeFrame.hideMainPanel(); oeFrame.setSize (450, 450); ShuttleAnimator shuttleAnimator = new AShuttleAnimator(); demoShuttleAnimation(shuttleAnimator, shuttle, oeFrame); } Both animateFromOrigins take the same amount of time The refreshing one changes the display, the other one does not

18 R EFRESHING A RCHITECTURE APlotted Shuttle APlotted Shuttle AShuttleAnimator main setShuttleX(Y)() Main Class Component repaint() paint() animateFromOrigin() animateFromOrigin ObjectEditor refresh()

19 A NIMATION S TEPS Animation consists of one or more animation steps An animation step updates one or more animating graphical properties such as size, location, and icon of one or more graphical objects and then pauses execution

20 P AUSING E XECUTION Execution can be paused using busy waiting or a sleep call provided by the operating system Busy waiting has the problem that it is platform-specific and does not allow other activity to proceed while the animation is paused Therefore using the sleep call is preferable

21 I NCREMENTAL U PDATES After each animation step, all displays of the animation must be updated The refresh operation can be used to do ensure these updates are made.