The Bouncing Ball Problem (Independent Threads)

Slides:



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

Multithreading : animation. slide 5.2 Animation Animation shows different objects moving or changing as time progresses. Thread programming is useful.
Using Eclipse. Getting Started There are three ways to create a Java project: 1:Select File > New > Project, 2 Select the arrow of the button in the upper.
Java Threads Part II. Lecture Objectives To understand the concepts of multithreading in Java To be able to develop simple multithreaded applications.
Slides prepared by Rose Williams, Binghamton University Chapter 20 Java Threads Part I.
Slides prepared by Rose Williams, Binghamton University Chapter 20 Java Never Ends.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
Cosc 4755 Phone programming: GUI Concepts & Threads.
Slides prepared by Rose Williams, Binghamton University ICS201 Lectures 18 : Threads King Fahd University of Petroleum & Minerals College of Computer Science.
1 CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.
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.
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.
50.003: Elements of Software Construction Week 5 Basics of Threads.
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.
Chapter 15 Multithreading F Threads Concept  Creating Threads by Extending the Thread class  Creating Threads by Implementing the Runnable Interface.
© 2006 Lawrenceville Press Slide 1 Chapter 3 Visual Basic Interface.
Using Thread in leJOS. Introduction to thread A thread is a thread of execution in a program The Java Virtual Machine allows an application to have multiple.
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
CS12420 – Swing and threads Lynda Thomas
CSE 219 Computer Science III Graphical User Interface.
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
Chapter One An Introduction to Visual Basic 2010 Programming with Microsoft Visual Basic th Edition.
Practical OOP using Java Basis Faqueer Tanvir Ahmed, 08 Jan 2012.
Session 11 Border Layout, using Panels, Introduction to PinBallGame.
1 Web Based Programming Section 8 James King 12 August 2003.
Field Trip #19 Animations with Java By Keith Lynn.
CSC 205 – Java Programming II Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are stand-alone.
Using Microsoft Visual Studio C++ Express 2005 Name: Dr Ju Wang Ashwin Belle Course Resource:
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.
Session 13 Pinball Game Construction Kit (Version 3):
Chapter 8 (Horstmann’s Book) Frameworks Hwajung Lee.
Multi-Threading in Java
C OMP 401 U SER -I NTERFACE VS. M AIN T HREADS Instructor: Prasun Dewan.
THREAD Subject:T0934 / Multimedia Programming Foundation Session:3 Tahun:2009 Versi:1/0.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 15 Event-Driven Programming and.
CPRG 215 Introduction to Object-Oriented Programming with Java Module 1-Introduction to Java Topic 1.3 Write Your First Java Program Produced by Harvey.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 15 Event-Driven Programming and.
Multithreading The objectives of this chapter are: To understand the purpose of multithreading To describe Java's multithreading mechanism.
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.
L6: Threads “the future is parallel” COMP206, Geoff Holmes and Bernhard Pfahringer.
Concurrency (Threads) Threads allow you to do tasks in parallel. In an unthreaded program, you code is executed procedurally from start to finish. In a.
Java Threads 1 1 Threading and Concurrent Programming in Java Threads and Swing D.W. Denbo.
Threads and Swing Multithreading. Contents I. Simulation on Inserting and Removing Items in a Combo Box II. Event Dispatch Thread III. Rules for Running.
Sending . Contents A)Sending Mail Using Command Line B)Sending Mail Using GUI.
The Echo Server Problem. Contents  Basic Networking Concepts  The Echo Server Problem.
The Echo Server Problem. Contents  Basic Networking Concepts  The Echo Server Problem.
Synchronization (Threads Accessing Shared Data). Contents I.The Bank Transfer Problem II.Doing a Simulation on the Bank Transfer Problem III.New Requirement:
Regular Expressions.
Internationalization The Number Format Problem
Animate objects and threads
Multithreaded applets
Chapter 13: Multithreading
More About Threads.
Chapter 19 Java Never Ends
Chap 7. Building Java Graphical User Interfaces
Graphical User Interfaces -- Introduction
ITEC324 Principle of CS III
Threads II IS
Android Topics UI Thread and Limited processing resources
Android Topics Threads and the MessageQueue
Unit 1 Lab14 & Lab15.
Border Layout, using Panels, Introduction to PinBallGame
Chapter 15 Multithreading
NETWORK PROGRAMMING CNET 441
Building Java Programs
CMSC 202 Threads.
Presentation transcript:

The Bouncing Ball Problem (Independent Threads)

Contents Problem Description A Single-Threading Solution A Multithreading Solution New Requirement: Bouncing an Image

I. Problem Description Develop a GUI program that animates a bouncing ball by continually moving the ball 1,000 times, each time 1 pixel in both x and y directions As soon as you click the Start button, the program launches a new ball from the upper- left corner of the screen and the ball begins bouncing (see the next slide) The delay between two moves is 3 ms

II. A Single-Threading Solution The Main Class Developing the View Adding Listeners to the Buttons The Shortcoming of This Solution

1. The Main Class

2. Developing the View

2.1. Add ballPanel to CENTER

2.2. Add buttonPanel to SOUTH

3. Adding Listeners to the Buttons 3.1. Adding Listener to the Close Button 3.2. Adding Listener to the Start Button

3.1. Adding Listener to the Close Button

3.2. Adding Listener to the Start Button 3.2.1. Bouncing a Ball 3.2.2. Taking Care of Multiple Balls

3.2.1. Bouncing a Ball

Make BallPanel as a Separate Class

Develop the Ball Class

Refactor the Position & Gradient Classes

Develop bounceBall()

Move the Ball

Sleep for 3 ms

Fit the Ball to the Panel

3.2.3. Bouncing Multiple Balls

4. The Shortcoming of This Solution The bouncing of a ball completely takes over the application We cannot interact with the program until the ball has finished bouncing

III. A Mutithreading Solution A simple procedure for running a task in a separate thread: Place the code for the task into the run method of a class that implements the Runnable interface public interface Runnable { void run(); } … class MyRunnable implements Runnable { public void run() { task code } }

Construct an object of your class: Runnable r = new MyRunnable(); Construct a Thread object from the Runnable: Thread t = new Thread(r); Start the thread: t.start();

III. A Mutithreading Solution Run the bouncing code in a separate thread

Execute the application and click Start button many times to get ConcurrentModificationException

Replace paint() by repaint()

Let the Balls Run Forever

IV. New Requirement: Bouncing an Image

Solution Look for the place where we can replace the ball by the image

Modify the listener of the Start Button

Modify bounceBall() by introducing an inheritance structure The main difference between BlackBall and ImageBall is show()

Execute the application to make sure that BlackBall works

Reference Core Java, Volume I – Fundamentals, Eighth Edition, Chapter 14. Cay S. Horstmann and Gary Cornell. Prentice Hall, 2008