How the User Blocks Pucks

The computer moves its own blocker around, but the user is responsible for moving his blocker using the mouse. To handle the mouse, you'll need to catch mouse press, mouse drag, and mouse release events, which are handled by implementing the methods of the MouseListener and MouseMotionListener interfaces, as you've already seen.

When the user presses the mouse, Slapshot! checks to see if the mouse is over his blocker, and if so, sets a Boolean flag named dragging to true, which means that from then on the user can drag his blocker simply by moving the mouse.

When the user presses the mouse, the mousePressed method is passed a MouseEvent object, and you can use the getX and getY methods of that object to determine where the mouse was when it was pressed. The code also stores the offset at which the user clicked the blocker so that when he starts dragging the blocker, it doesn't simply assume that the mouse pointer is at location (0, 0) in the blocker, which would make the blocker's image jump when the user started dragging:

[View full width]

public class Slapshot extends Frame implements ActionListener, MouseListener, MouseMotionListener, Runnable { . . . public void mousePressed(MouseEvent e) { Rectangle r1 = pucks.elementAt(13).rectangle; if(r1.contains(new Point(e.getX(), e.getY()))){ offsetX = e.getX() - r1.x; offsetY = e.getY() - r1.y; dragging = true; } }

REAL-WORLD SCENARIO: Worker Threads and User-Interface Threads

The Aquarium project used multithreaded sprites, but the Slapshot! game is where multithreading really shines. In the early days of window-oriented programming, you didn't have access to threads, which made programming the user interface and the main program logic separately nearly impossible to do.

In an application such as Slapshot!, that would be a real problem, because this game needs to watch what you're doing with the mouse at the same time as it moves the pucks around. Programmers were often supremely frustrated trying to write visually intensive applications like this because as they were handling the mouse and intercepting mouse events, the rest of the application was on hold, and if things were supposed to be happeningsuch as pucks zinging arounda lot of flicker was caused.

That's obviously a bad problem. Programmers found that while their applications were supposed to be doing some real work behind the scenes, the user interface sufferedor, if the user interface got the time it needed, the crucial background work was neglected.

This is one of the reasons Java was popular from the very beginningsince the JDK 1.0, it has offered programmers the ability to work with threads. You can launch a new thread to handle background work, such as moving pucks around, while letting the main thread deal with the user interface (or the other way around). And programmers soon found that the way threads were implemented in Java made them a lot easier to work with than in other window-oriented languages, where access to the user interface from a background thread was problematic and often caused the entire application to freeze.

Slapshot! is a good example of the need for threads in window-oriented applications. While you're moving around your blocker with the mouse, the application should still be able to move the pucks around and be able to draw them on the screen. That works just as you'd expect in Java.

You can see the methods of the MouseEvent class in Table 2.4.

Table 2.4. The Significant Methods of the java.awt.event.MouseEvent Class


Does This

int getButton()

Returns which mouse button was pressed or released

int getClickCount()

Returns the number of mouse clicks that happened

Point getPoint()

Returns a Point object that holds the X and Y coordinates of the mouse event

int getX()

Returns the X coordinate of the mouse event

int getY()

Returns the Y coordinate of the mouse event

When the user releases the mouse, the dragging flag is set to false:

 public void mouseReleased(MouseEvent e) {         dragging = false; } 

When the user drags the mouse, Java calls the mouseDragged method with the new mouse location. In this method, all you have to do is to pass the new Y location of the blocker to its slide method and repaint the game to update that blocker's location:

 public void mouseDragged(MouseEvent e) {     if(dragging){         int newY = e.getY() - offsetY;         pucks.elementAt(13).slide(newY);         repaint();     } } 

That's it. This gets all the blockers and all the pucks moving. How fast the pucks actually move depends on the speed variable, which the user can set. But how can you read the user's new setting? For that, the application uses a dialog box.

    Java After Hours(c) 10 Projects You'll Never Do at Work
    Java After Hours: 10 Projects Youll Never Do at Work
    ISBN: 0672327473
    EAN: 2147483647
    Year: 2006
    Pages: 128

    Similar book on Amazon © 2008-2017.
    If you may any questions please contact us: