190 likes | 203 Views
Session 33. Introduction to PinBallGame, MouseEvents and Vector. Questions?. Pinball Game Construction Kit Version 1. Looks the same as CannonWorld, but it’s not. Replaced the fire button with a mouse event. Control is no longer in the paint method.
E N D
Session 33 Introduction to PinBallGame, MouseEvents and Vector
Looks the same as CannonWorld, but it’s not. • Replaced the fire button with a mouse event. • Control is no longer in the paint method. • Multiple balls can be in the air at once.
Looks the same, but it’s not. • Replaced the fire button with a mouse event. • Control is no longer in the paint method. • Multiple balls can be in the air at once.
Handling Mouse Events The CannonWorld handles only events that are generated by the active components — a button and a slider — that we added to it. More generally, though, we will want to trap and respond to any mouse action, anywhere within the frame. Any listener that wishes to monitor mouse activity must implement the MouseListener interface:
public interface MouseListener { public void mouseClicked ( MouseEvent e ); public void mouseEntered ( MouseEvent e ); public void mouseExited ( MouseEvent e ); public void mousePressed ( MouseEvent e ); public void mouseReleased( MouseEvent e ); } The MouseListener interface is part of the package java.awt.event. It specifies what an object must do to be a mouse listener within the Java Event Model.
How the MouseListener works. Think back to how we implemented Button listeners: private class FireButtonListener implements ActionListener { We can do a similar thing for MouseListeners… private class PinballGameListener implements MouseListener{ However, to implement an interface, the Java language insists that the programmer provide a definition for all operations. But what if I don’t want all 5 mouse events??
MouseAdapter to the rescue • To simplify this process, the Java library provides a simple class named Mouse Adapter. The class MouseAdapter implements the MouseListener interface, but uses an empty method for each method. • Rather than implement a MouseListener, we may choose to extend a MouseAdapter.
Mouse Events in the Pin Ball Game In the PinBallGame class, we have the following class relationship: MouseListener implements MouseAdapter extends MouseKeeper
What does a MouseKeeper do? • If the mouse is pressed in the“shooting area”, then it creates and launches a new pinball. • What does a MouseAdapter do? • Nothing, in response to any sort of mouse event. • Why do you suppose that Java’s creators call it an “adapter”? • They use a common design from the world as an analogy.
Why do you suppose Java’s creators bothered to define the MouseListener interface? Why not have everyone extend MouseAdapter? • Inheritance is not free. Why force programmers who plan to implement most or all of the interface to pay the extra price?
Looks the same, but it’s not. • Replaced the fire button with a mouse event. • Control is no longer in the paint method. • Multiple balls can be in the air at once.
Threads of Execution What? How? Why? The Thread class provides methods to start, run, sleep, and stop an independent path of computation, among other things. The start() method manages the overhead of threads for us; we can simply watch them go! (This is similar to the benefits we get from using Frames...) The pinball game separates these responsibilities into different objects: • painting the frame • controlling the movement/location of balls
Threads of Execution This separation simplifies all of the methods involved, for writing, reading, and modifying. The cost is an increase in the number of methods needed. You may also suffer some separation anxiety. To programmers used to writing the omniscient, omnipotent main(), decentralizing control can cause a sense of fragmentation. This will largely go away as you gain OO and Java experience.
Looks the same, but it’s not. • Replaced the fire button with a mouse event. • Control is no longer in the paint method. • Multiple balls can be in the air at once. • Uses a Vector to contain many balls
Keeping track of multiple objects. • In the past we have kept track of multiple items through an array, e.g., in Multi-ball world. • What are the limitations of using an array? • Instead, this code uses the concept of a Vector.
Using Vector import java.util.Vector; private Vector<PinBall> balls; balls = new Vector<PinBall>( ); balls.addElement(newBall); // add element for ( PinBall aBall : balls) { aBall.paint(g); }
Vector vs. ArrayList • Why use Vector instead of an ArrayList? • They basically behave the same. • Vector is optimized for threaded execution (more on what this means later). • ArrayList is optimized for non-threaded execution.