Download Lab 6: Threads: Bouncing Ball

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Lab 6: Threads: Bouncing Ball
1
Lab 6: Threads: Bouncing Ball
CS 284
Monday, 2-22-16
Revision 1.3
Preliminary material
• Drawing in a Java applet; methods paint(), repaint(). Example threads/Bounce.java (2
Classes and an Interface)
• Double buffering; drawing on Images. Example threads/Bounce2.java
• Concept of multithreaded application. Critical section, race conditions, blocking; deadlock.
• Java synchronized keyword: synchronized instance or class method;
synchronized (obj-or-array) statement
• Java monitors: synchronized, wait(), notify().
• Threads examples threads/Bounce.java, threads/Bounce2.java
Laboratory exercises
1. Create a subdirectory lab6, and make a copy of Bounce.java in your new subdirectory.
Rename this file MyBounce.java; get it working under the new name, and create a git commit
for the correct code.
2. Modify MyBounce.java to draw an animated bouncing ball using double buffering. Use
threads/Bounce2.java as a model, but implement the changes in the class MyBounce rather
than creating a subclass. Handle Graphics objects as a local variable within Paint, rather
than creating a new state variable imageG as in Bounce2.)
Also, move the initialization of the TimerThread timer from the state-variable definition line
into an init() method for MyBounce. (There is no need for anything else to go into init()
at this time.)
Get these changes running and create a commit of the working changes.
3. Add a class Ball to MyBounce.java that represents the bouncing ball. Informal spec for
Ball:
• Six state variables: color, radius, x position, y position, and the direction values dx, dy.
• A constructor with six arguments for initializing those state variables.
• One method draw(Graphics g) for drawing the ball.
Modify MyBounce.java to use a single state variable of type Ball instead of several state
variables x, y, etc. Initialize your Ball in init(). Convert paint() and doStep() to use
your Ball state variable.
Test and correct the resulting program, then record it a commit.
4. Now modify Ball by adding a method move with no arguments that performs the Ballrelated code in the applet’s method doStep. Simplify doStep to call your Ball object’s move
method, then to call repaint.
Test your changes, correct any errors, then create a commit containing the working version.
Lab 6: Threads: Bouncing Ball
2
5. Edit MyBounce to add a new green ball with its own radius, trajectory and starting position.
Test, debug, then commit the results.
Deliverables
Submit the commits in your lab6 subdirectory using pull/push, as usual. This lab should be
completed by Monday, 2-22-16 .