Download presentation
Presentation is loading. Please wait.
1
Expanding the PinBallGame
Session 35 Expanding the PinBallGame
2
The next slides are all review from Friday
3
PinBall Contruction Kit Version 2
4
Questions How is the “black box” in the PinBallGame drawn?
What is the difference between the items outside the box of the game window and the items inside the box? What messages can we send to a Peg, and where is each behavior defined? What is the purpose of the PinBallTarget interface? Why can’t we do without it?
5
PinBallTarget Interface
interface PinBallTarget { public boolean intersects (Ball aBall); public void moveTo (int x, int y); public void paint (Graphics g); public void hitBy (Ball aBall); } Why use an interface? we want to process targets uniformly, e.g., check if a ball hit it the interface makes them the same “type” for storage in a Vector
6
Hole target structurally similar to a ball behavioral
round like a ball has a location on the frame like a ball behavioral it must adhere to the interface class Hole extends Ball implements PinBallTarget Inherits moveTo and paint, but supplies intersects and hitBy
7
More on Threads We can think of separate threads as separate programs running concurrently. They don’t literally run at the same time (unless you have a machine with more than one CPU). Instead, one thread gets the CPU for a while, then it gets put on hold while another thread gets the CPU, and so on. When separate threads are running, sometimes we need to worry about two threads taking actions that conflict with one another. We can use the keyword synchronized to have the JVM help maintain order.
8
A Problem Caused by Separate Threads of Control
9
More on Threads Example: The second version of the pin ball game keeps track of the score the user earns for hitting targets in the field of play. It keeps track of the score in an instance variable named score: private int score = 0; When a pin ball strikes a target, the target tells the pin ball game to add its point total to the instance variable by sending an addScore message: public void addScore( int value ) { score = score + value; scoreLabel.setText( "score = " + score ); }
10
A Problem Caused by Separate Threads of Control
11
The solution synchronized public void addScore( int value ) {
score = score + value; scoreLabel.setText( "score = " + score ); } The keyword synchronized is used to ask Java to guarantee that only one thread at a time can be executing the addScore() method.
12
What improvements would you like to make?
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.