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.

Slides:



Advertisements
Similar presentations
Java Applets- Using SwingWorker Dave Price and Chris Loftus Computer Science Department University of Wales, Aberystwyth.
Advertisements

1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.
CS 178: Programming with Multimedia Objects Aditya P. Mathur Professor of Computer Sciences Purdue University, West Lafayette Sept 9, 2004 Last update:
(c) 2006 E.S.Boese All Rights Reserved. Threads and Timers Chapter 19 - Student.
Event Handling Events and Listeners Timers and Animation.
Lecture 24 Applets. Introduction to Applets Applets should NOT have main method but rather init, stop, paint etc They should be run through javac compiler.
CSE S. Tanimoto Java Threads 1 Java Threads (Outline) Motivation The class Thread Example program: ThreadRace The Runnable interface Example: Clock.
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.
Starts to load image Delay on network Load some more of the image Time for some word processing Thread 2 Thread 1 Figure 15.1 Two threads sharing the processor.
Slides prepared by Rose Williams, Binghamton University ICS201 Lectures 18 : Threads King Fahd University of Petroleum & Minerals College of Computer Science.
Threads A thread is a program unit that is executed independently of other parts of the program A thread is a program unit that is executed independently.
28-Jun-15 Applets. 2 An applet is a program that is typically embedded in a Web page and can be run from a browser You need special HTML in the Web page.
OOP&M - theory lectures1 OOP&M – and there it was an eighth day “I f you know what I know, what am I doing here? ” - Aristoteles -
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.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Java Threads CS Introduction to Operating Systems.
Java: Animation Chris North cs3724: HCI. Animation? Changing graphics over time Examples: cartoons Clippy, agents/assistants Hour glass Save dohicky Progress.
1 Advanced Computer Programming Concurrency Multithreaded Programs Copyright © Texas Education Agency, 2013.
Code Club Session 2 Dance Party. What will we learn ?  How to change the background  How to create animations  How to make objects talk to each other.
Chapter 15 Multithreading F Threads Concept  Creating Threads by Extending the Thread class  Creating Threads by Implementing the Runnable Interface.
Threads and Multimedia Animation, Images, Sound. Animation nAnimation, displaying a sequence of frames to create the illusion of motion, is a typical.
(c) 2008 E.S.Boese All Rights Reserved. Threads and Media Chapter 8 - Lecture Slides 1.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Java Programming, Second Edition Chapter Seventeen Multithreading and Animation.
Threads. Overview Problem Multiple tasks for computer Draw & display images on screen Check keyboard & mouse input Send & receive data on network Read.
Object Oriented Programming Lecture 4: Refactoring, An Applet Example, Idiom - Animation applets, Introduction to the Laboratorial exercise www2.hh.se/staff/jebe/oop2005/
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.
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.
1 Web Based Programming Section 8 James King 12 August 2003.
Field Trip #19 Animations with Java By Keith Lynn.
Concurrent Programming and Threads Threads Blocking a User Interface.
C20: Threads see also: ThreadedBallWorld, DropTest, Tetris source examples Not covered: advanced stuff like notify/notifyAll.
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
Section 3.2 Notes Conditional Probability. Conditional probability is the probability of an event occurring, given that another event has already occurred.
1 CS161 Introduction to Computer Science Topic #9.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
Threads II IS Outline  Quiz  Thread review  Stopping a thread  java.util.Timer  Swing threads javax.swing.Timer  ProgressMonitor.
Java Thread and Memory Model
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
SurfaceView.
17/3/00SEM107 © Kamin & Reddy Class 13 - Animation 1 Class 13 - Animation r Summary of loops m while-do m for m do-while r while loop examples r for loop.
More Python!. Lists, Variables with more than one value Variables can point to more than one value at a time. The simplest way to do this is with a List.
Multithreaded programming  Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run.
Introduction to Computer Programming - Project 2 Intro to Digital Technology.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Slides prepared by Rose Williams, Binghamton University Chapter 20 Java Never Ends.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
Lesson 28: More on the GUI button, frame and actions.
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.
1 5-High-Performance Embedded Systems using Concurrent Process (cont.)
UQC117S2 Graphics Programming Lecture 2 Event Handling Program as a sequence of instructions Event -driven program Need to detect the event and carry out.
Programming and Problem Solving With Java Copyright 1999, James M. Slack Threads and Animation Threads Animation.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Chapter 11: Threaded Programs Situations where the program is following multiple execution paths (how to stop one?) Thread: a line of execution Thread.
Objective of the lesson Use Blockly to make a dice for Snakes and Ladders All of you will: – Make an image which displays when you press a button Most.
Chapter 13: Multithreading
Lecture 09 Applets.
Chapter 19 Java Never Ends
BBC Microbit.
Threads II IS
Multithreaded Programming
Java Threads (Outline)
Java Threads (Outline)
Chapter 15 Multithreading
Threads and Multithreading
Mobile Computing With Android ACST 4550 Android Animation
Presentation transcript:

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 or operations at one time What is happening is that each program or operation is running on its own THREAD This is so the computer can control each thread giving one more priority than another and stopping or starting separate threads independently

Animation Classic animation is accomplished by rapidly displaying a sequence of images Each image is slightly changed When done correctly this tricks our brain into seeing movement Another way to look at it is if we take an image –display it –erase it –move the image –display it again –Do this over and over

Using Threads for Animation We could probably accomplish some basic animations by putting everything in an infinite while loop We will rather use threads for animation because it give us more control than just a giant while loop would

Threads 1 Let’s look at what is needed in the class header and variable section public class PicMover extends Applet implements Runnable { Thread runner; implement Runnable declare a Thread object

Threads 2a There are three methods used to control threads // Initialize the thread in the start() method public void start() { if (runner==null) { runner=new Thread(this); runner.start(); } The start() method is where the thread is initialized. It checks if the runner is initialized and if it isn’t it initializes it

Threads 2b // The run() method is where you put most of your program public void run() { while (true) { repaint(); //call to redraw the screen // other commands for your program // would go here } This is the method that gets repeated. Therefore this is where you will probably put the bulk of your program

Threads 2c //stop() method used to stop the thread public void stop() { if (runner!=null) { runner.stop(); runner=null; } This is the method that is called when the thread needs to be stopped. It checks to see if there is a thread running then stops it if it is running

Threads 3 You can “pause” or “unpause” your animation by SUSPENDING or RESUMING your thread The two methods used to do this could be called after a button event occurred: public void actionPerformed(ActionEvent event) { buttonLabel=event.getActionCommand(); if (buttonLabel=="Suspend") { runner.suspend(); } else if (buttonLabel=="Resume") { runner.resume(); }