1 / 19

Session 33

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.

rleija
Download Presentation

Session 33

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Session 33 Introduction to PinBallGame, MouseEvents and Vector

  2. Questions?

  3. Pinball Game Construction Kit Version 1

  4. 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.

  5. 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.

  6. 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:

  7. 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.

  8. 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??

  9. 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.

  10. Mouse Events in the Pin Ball Game In the PinBallGame class, we have the following class relationship: MouseListener implements MouseAdapter extends MouseKeeper

  11. 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.

  12. 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?

  13. 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.

  14. 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

  15. 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.

  16. 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

  17. 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.

  18. 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); }

  19. 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.

More Related