Java: Animation Chris North cs3724: HCI. Animation? Changing graphics over time Examples: cartoons Clippy, agents/assistants Hour glass Save dohicky Progress.

Slides:



Advertisements
Similar presentations
Using Thread for Animation. Threads When we are doing multiple things at once we say we are multitasking Computers multitask when they run several programs.
Advertisements

13/04/2015Client-server Programming1 Block 6: Threads 1 Jin Sa.
Algorithm Programming Concurrent Programming in Java Bar-Ilan University תשס"ח Moshe Fresko.
Jim Priest, Peter Lundgren, Russell Bennett. Background info  A sequence of fames  You have some experience already  Used for some GUI’s and Games.
Albert Johnson Molly Richardson Andrew Kruth.  Threads Runnable interface sleep()  GUIs repaint() paintComponent()
(c) 2006 E.S.Boese All Rights Reserved. Threads and Timers Chapter 19 - Student.
Graphics Programming. In this class, we will cover: The difference between AWT and Swing Creating a frame Frame positioning Displaying information in.
Slides prepared by Rose Williams, Binghamton University Chapter 20 Java Never Ends.
22-Jun-15 Threads and Multithreading. 2 Multiprocessing Modern operating systems are multiprocessing Appear to do more than one thing at a time Three.
Carnegie Mellon University, MISM1 Java GUI programming and Java Threads GUI example taken from “Computing Concepts with Java 2” by Cay Horstmann Thread.
Object Oriented Programming Java 1 GUI example taken from “Computing Concepts with Java 2” by Cay Horstmann GUI Programming.
Java ThreadsGraphics Programming Graphics Programming: Java Threads.
Definitions Process – An executing program
29-Jun-15 Threads and Multithreading. 2 Multiprocessing Modern operating systems are multiprocessing Appear to do more than one thing at a time Three.
Lecture 17: Animation Yoni Fridman 7/27/01 7/27/01.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
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.
Events in Java Swing Chris North cs3724: HCI. Typical command line program Non-interactive Linear execution program: main() { code; }
Threading in Java – a Tutorial QMUL IEEE SB. Why Threading When we need to run two tasks concurrently So multiple parts (>=2) of a program can run simultaneously.
Web Design & Development Lecture 18. Java Graphics.
1 Advanced Computer Programming Concurrency Multithreaded Programs Copyright © Texas Education Agency, 2013.
Chapter 15 Multithreading F Threads Concept  Creating Threads by Extending the Thread class  Creating Threads by Implementing the Runnable Interface.
CS12420 – Swing and threads Lynda Thomas
Java Programming, Second Edition Chapter Seventeen Multithreading and Animation.
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
Adding Graphics to a Frame Application Applets: Can generate drawings by overriding paint Frame: Do not draw directly on a frame. Draw graphics on a JPanel.
Dr. R R DOCSIT, Dr BAMU. Basic Java : Multi Threading 2 Objectives of This Session State what is Multithreading. Describe the life cycle of Thread.
Object-Oriented Programming (Java), Unit 29 Kirk Scott 1.
1 Web Based Programming Section 8 James King 12 August 2003.
Java Graphics. Review 3 kinds of elements in components API? Layout managers Events Extend vs. Implement.
Concurrent Programming and Threads Threads Blocking a User Interface.
Object-Oriented Programming (Java), Unit 29 Kirk Scott 1.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
OPERATING SYSTEMS Frans Sanen.  Recap of threads in Java  Learn to think about synchronization problems in Java  Solve synchronization problems in.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
Threading and Concurrency COM379T John Murray –
15.1 Threads and Multi- threading Understanding threads and multi-threading In general, modern computers perform one task at a time It is often.
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
SurfaceView.
Java Dynamic Graphics.
Concurrent Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
© Wang Bin 2004 Java Threads. © Wang Bin 2004 In this lesson, you will learn to: u Define the concepts of threads and multithreading  Identify the functions.
Multi-Threading in Java
THREAD Subject:T0934 / Multimedia Programming Foundation Session:3 Tahun:2009 Versi:1/0.
Parallel Processing (CS526) Spring 2012(Week 8).  Shared Memory Architecture  Shared Memory Programming & PLs  Java Threads  Preparing the Environment.
Threads and Multithreading. Multiprocessing Modern operating systems are multiprocessing Appear to do more than one thing at a time Three general approaches:
Java Graphics Chris North cs3724: HCI. Presentations peter hou Vote: UI Hall of Fame/Shame?
Java Applets Adding Animation. Import Files You still need to include the same files: –import java.applet.*; –import java.awt.*;
Chapter 13: Multithreading The Thread class The Thread class The Runnable Interface The Runnable Interface Thread States Thread States Thread Priority.
CSC CSC 143 Threads. CSC Introducing Threads  A thread is a flow of control within a program  A piece of code that runs on its own. The.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
Multithreaded applets
Chapter 13: Multithreading
Java Graphics.
Threads and Multithreading
GUI AND GRAPHICS.
Threads and Multithreading
Threads and Multithreading
Threads and Multithreading
Threads and Multithreading
Threads and Multithreading
Chapter 15 Multithreading
Threads and Multithreading
Threads and Multithreading
Multithreading in java.
Threads and Multithreading
Threads and Multithreading
Threads.
CMSC 202 Threads.
Presentation transcript:

Java: Animation Chris North cs3724: HCI

Animation? Changing graphics over time Examples: cartoons Clippy, agents/assistants Hour glass Save dohicky Progress bar Copy file Amit’s pulsating plot

Purvi Algorithm Animation

Java Graphics Review Extend JPanel Override paintComponent( ) method public class MyPanel extends JPanel { public void paintComponent(Graphics g){ super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; // my graphics here: g2.drawString("Hello World", 10, 10); }

Ticker

Animating the ticker text Keep repainting graphics using different x public class MyPanel extends JPanel { int x; public void paintComponent(Graphics g){ super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; // my graphics here: g2.drawString("Hello World", x, 10); }

Animation Loop Update x location (member variable) Repaint While(true){//infinite loop x = x – 1; repaint(); //calls paintComponent() Thread.sleep(10); //slow down animation } Problem?

Multi-Threading Loop prevents other parts of code from executing User events pile up Multiple threads let multiple methods execute simultaneously (multi-tasking) Threads = lightweight processes, shared memory Main thread User interaction Anim thread Update graphics

Java Threads implement Runnable interface Override run( ) method Put animation loop here new Thread(runnable) Thread.start( ) Calls runnable.run( ) in new thread Runnable run( ) Thread start( ) My Panel

Simplifying MyPanel implements Runnable Runnable run( ) Thread start( ) My Panel Thread start( ) My Panel Runnable run( )

Threaded Ticker public class MyPanel extends JPanel implements Runnable { int x = 100; Thread mythread; public MyPanel(){ // constructor mythread = new Thread(this); mythread.start(); } public void run(){ while(true){// animation loop x = x – 5; repaint(); Thread.sleep(100); } public void paintComponent(Graphics g){ super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.drawString("Hello World", x, 10); // my graphics } or in button code that starts animation

Stopping a Thread Stop animation loop with a flag mythread = null; will cleanup thread object Boolean runMyAnimation; While(runMyAnimation){//flag stops loop x = x – 5; repaint(); //calls paintComponent() Thread.sleep(100); //slow down animation }

Controlling Animation Speed While(true){ x = x – 5;//animation step size repaint(); Thread.sleep(100); //delay between steps } Milliseconds 1000 = 1 second

Animation Summary In a secondary thread: 1.Make changes to global state 2.Repaint 3.Sleep delay 4.Repeat Use double buffering as needed

JBuilder Example