maclib
Class DrawingStuff

java.lang.Object
  extended by maclib.DrawingStuff
All Implemented Interfaces:
java.awt.event.KeyListener, java.awt.event.MouseListener, java.awt.event.MouseMotionListener, java.util.EventListener

public final class DrawingStuff
extends java.lang.Object
implements java.awt.event.MouseListener, java.awt.event.MouseMotionListener, java.awt.event.KeyListener

A DrawingStuff object implements an off-screen image for backing store and provides automatic repaint for a DrawingApplet panel. The drawings are to be done on the off-screen image, using the graphic context that is obtained by a call to the getGraphics() method. New drawings have to be explicitely exposed by invoking the doExpose method. When the DrawingApplet panel needs to be repaint, its paint method calls the DrawingStuff's one.

The DrawingStuff also implements listeners for handling keyboard and mouse events that concern the DrawingApplet.

Version:
25 feb 2005
Author:
Philippe Chassignet, Ecole Polytechnique
See Also:
MacLib, DrawingFrame, DrawingApplet

Field Summary
static int CONTROL
          Used to encode the control modifier in the button state.
static int DEFAULT_SCREEN_HEIGHT
          The default final height for an off-screen image.
static int DEFAULT_SCREEN_WIDTH
          The default final width for an off-screen image.
static int META
          Used to encode the meta modifier in the button state.
static int NONE
          Used to specify no modifier in the button state.
static int SHIFT
          Used to encode the shift modifier in the button state.
 
Constructor Summary
DrawingStuff(DrawingApplet panel, int width, int height, java.lang.String debug)
          Constructs and initializes a DrawingStuff attached to the specified DrawingApplet.
 
Method Summary
 void dispose()
          Disposes of this DrawingStuff and releases its resources.
 void doExpose(int left, int top, int right, int bottom)
          Paints the specified rectangular area onto the visible panel with the content of the associated off-screen image.
 void finalize()
          Called by the garbage collector when there are no more reference to this DrawingStuff.
 boolean getButton()
          Returns the state of the mouse button.
 int getButtonState()
          Returns an integer value that describes the modifier keys state at the time of the last mouse pressed event.
 java.awt.Graphics getGraphics()
          Returns a graphic context for drawing onto the off-screen image that is associated to this DrawingStuff.
 char getKey()
          Returns the char corresponding to the last keyboard event.
 java.awt.Point getMouse()
          Returns the current mouse location (relative to the corresponding drawing panel) as a java.awt.Point.
 void keyPressed(java.awt.event.KeyEvent e)
          Invoked by the event handler when a key has been pressed.
 void keyReleased(java.awt.event.KeyEvent e)
          Invoked by the event handler when a key has been released.
 void keyTyped(java.awt.event.KeyEvent e)
          Invoked by the event handler when a key has been typed.
 void mouseClicked(java.awt.event.MouseEvent e)
          Invoked by the event handler when the mouse has been clicked on the drawing panel.
 void mouseDragged(java.awt.event.MouseEvent e)
          Invoked by the event handler when a mouse button is pressed on the drawing panel and then dragged.
 void mouseEntered(java.awt.event.MouseEvent e)
          Invoked by the event handler when the mouse enters the drawing panel.
 void mouseExited(java.awt.event.MouseEvent e)
          Invoked by the event handler when the mouse exits the drawing panel.
 void mouseMoved(java.awt.event.MouseEvent e)
          Invoked by the event handler when the mouse has been moved on the drawing panel (with no button down).
 void mousePressed(java.awt.event.MouseEvent e)
          Invoked by the event handler when a mouse button has been pressed on the drawing panel.
 void mouseReleased(java.awt.event.MouseEvent e)
          Invoked by the event handler when a mouse button has been released on the drawing panel.
 void paint(java.awt.Graphics g)
          Invoked by the DrawingApplet.paint(Graphics) method when an exposed area needs to be repaint.
 char peekKey()
          Returns the char corresponding to the last keyboard event, if any occurs and the char is still kept available.
 void reset()
          Resets the drawing and event context of this DrawingStuff.
 java.lang.String toString()
          Returns a string representation of the state of this DrawingStuff.
 boolean waitClick()
          Waits until the mouse button is pressed or released, then returns the state of the mouse button.
 java.awt.Point waitMouse()
          Waits until the mouse button is released, after being pressed, or until the mouse location is dragged.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

NONE

public static final int NONE
Used to specify no modifier in the button state.

See Also:
getButtonState(), Constant Field Values

SHIFT

public static final int SHIFT
Used to encode the shift modifier in the button state.

See Also:
getButtonState(), Constant Field Values

CONTROL

public static final int CONTROL
Used to encode the control modifier in the button state.

See Also:
getButtonState(), Constant Field Values

META

public static final int META
Used to encode the meta modifier in the button state.

See Also:
getButtonState(), Constant Field Values

DEFAULT_SCREEN_WIDTH

public static final int DEFAULT_SCREEN_WIDTH
The default final width for an off-screen image.

See Also:
DrawingStuff(maclib.DrawingApplet, int, int, java.lang.String), Constant Field Values

DEFAULT_SCREEN_HEIGHT

public static final int DEFAULT_SCREEN_HEIGHT
The default final height for an off-screen image.

See Also:
DrawingStuff(maclib.DrawingApplet, int, int, java.lang.String), Constant Field Values
Constructor Detail

DrawingStuff

public DrawingStuff(DrawingApplet panel,
                    int width,
                    int height,
                    java.lang.String debug)
Constructs and initializes a DrawingStuff attached to the specified DrawingApplet.

Parameters:
panel - the DrawingApplet
width - the final width of the off-screen image
height - the final height of the off-screen image
debug - a string indicating the debug level
Method Detail

dispose

public void dispose()
Disposes of this DrawingStuff and releases its resources. This method is called by the dispose() method for DrawingApplet.


finalize

public void finalize()
Called by the garbage collector when there are no more reference to this DrawingStuff.

Overrides:
finalize in class java.lang.Object

getGraphics

public java.awt.Graphics getGraphics()
Returns a graphic context for drawing onto the off-screen image that is associated to this DrawingStuff. This context should be explicitely disposed when finished.

Returns:
the Graphics object.

doExpose

public void doExpose(int left,
                     int top,
                     int right,
                     int bottom)
Paints the specified rectangular area onto the visible panel with the content of the associated off-screen image. Coordinates are relative to both the off-screen image and the drawing panel.

Parameters:
left - the left coordinate of the filled area.
top - the top coordinate of the filled area.
right - the right coordinate of the filled area.
bottom - the bottom coordinate of the filled area.

paint

public void paint(java.awt.Graphics g)
Invoked by the DrawingApplet.paint(Graphics) method when an exposed area needs to be repaint. It results in copying the content of the associated off-screen image onto the panel.


reset

public void reset()
Resets the drawing and event context of this DrawingStuff.


mouseClicked

public void mouseClicked(java.awt.event.MouseEvent e)
Invoked by the event handler when the mouse has been clicked on the drawing panel.

Specified by:
mouseClicked in interface java.awt.event.MouseListener

mousePressed

public void mousePressed(java.awt.event.MouseEvent e)
Invoked by the event handler when a mouse button has been pressed on the drawing panel.

Specified by:
mousePressed in interface java.awt.event.MouseListener

mouseReleased

public void mouseReleased(java.awt.event.MouseEvent e)
Invoked by the event handler when a mouse button has been released on the drawing panel.

Specified by:
mouseReleased in interface java.awt.event.MouseListener

mouseEntered

public void mouseEntered(java.awt.event.MouseEvent e)
Invoked by the event handler when the mouse enters the drawing panel.

Specified by:
mouseEntered in interface java.awt.event.MouseListener

mouseExited

public void mouseExited(java.awt.event.MouseEvent e)
Invoked by the event handler when the mouse exits the drawing panel.

Specified by:
mouseExited in interface java.awt.event.MouseListener

mouseDragged

public void mouseDragged(java.awt.event.MouseEvent e)
Invoked by the event handler when a mouse button is pressed on the drawing panel and then dragged. Mouse drag events will continue to be delivered until the mouse button is released (regardless of whether the mouse position is within the bounds of the drawing panel).

Specified by:
mouseDragged in interface java.awt.event.MouseMotionListener

mouseMoved

public void mouseMoved(java.awt.event.MouseEvent e)
Invoked by the event handler when the mouse has been moved on the drawing panel (with no button down).

Specified by:
mouseMoved in interface java.awt.event.MouseMotionListener

getButton

public boolean getButton()
Returns the state of the mouse button.

Returns:
true if the button is currently down; false otherwise.

getMouse

public java.awt.Point getMouse()
Returns the current mouse location (relative to the corresponding drawing panel) as a java.awt.Point.

Returns:
the java.awt.Point object

waitClick

public boolean waitClick()
Waits until the mouse button is pressed or released, then returns the state of the mouse button.

Returns:
true if the button is currently down; false otherwise.

waitMouse

public java.awt.Point waitMouse()
Waits until the mouse button is released, after being pressed, or until the mouse location is dragged. Returns the issued mouse location (relative to the corresponding drawing panel) as a java.awt.Point.

Returns:
the java.awt.Point object

getButtonState

public int getButtonState()
Returns an integer value that describes the modifier keys state at the time of the last mouse pressed event. The returned value is either NONE, or a combination of the SHIFT, CONTROL and META (for the Alt key) constant flags.

Returns:
the modifier keys state as an int value.
See Also:
NONE, SHIFT, CONTROL, META

keyTyped

public void keyTyped(java.awt.event.KeyEvent e)
Invoked by the event handler when a key has been typed. This event occurs when a key press is followed by a key release.

Specified by:
keyTyped in interface java.awt.event.KeyListener

keyPressed

public void keyPressed(java.awt.event.KeyEvent e)
Invoked by the event handler when a key has been pressed.

Specified by:
keyPressed in interface java.awt.event.KeyListener

keyReleased

public void keyReleased(java.awt.event.KeyEvent e)
Invoked by the event handler when a key has been released.

Specified by:
keyReleased in interface java.awt.event.KeyListener

peekKey

public char peekKey()
Returns the char corresponding to the last keyboard event, if any occurs and the char is still kept available. This char remains available for further calls.

Returns:
the char, if any, 0 otherwise
See Also:
getKey()

getKey

public char getKey()
Returns the char corresponding to the last keyboard event. If no char is available, will block until a new keyboard event occurs. The char is made unavailable for further calls.

Returns:
the char
See Also:
peekKey()

toString

public java.lang.String toString()
Returns a string representation of the state of this DrawingStuff.

Overrides:
toString in class java.lang.Object
Returns:
the string representation of internal parameters