Class DrawingPanel

java.lang.Object
  extended by java.lang.Thread
      extended by DrawingPanel
All Implemented Interfaces:
java.awt.event.KeyListener, java.awt.event.MouseListener, java.awt.event.MouseMotionListener, java.lang.Runnable, java.util.EventListener, javax.swing.event.MouseInputListener

public class DrawingPanel
extends java.lang.Thread
implements javax.swing.event.MouseInputListener, java.awt.event.KeyListener


Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
 
Field Summary
static int ANY_KEY
           
static int DELETE_KEY
           
static int DOWN_ARROW_KEY
           
static int ENTER_KEY
           
static int ESC_KEY
           
static int F1_KEY
           
static int F10_KEY
           
static int F11_KEY
           
static int F12_KEY
           
static int F2_KEY
           
static int F3_KEY
           
static int F4_KEY
           
static int F5_KEY
           
static int F6_KEY
           
static int F7_KEY
           
static int F8_KEY
           
static int F9_KEY
           
static int HOME_KEY
           
static int INSERT_KEY
           
static int LEFT_ARROW_KEY
           
static int LEFT_BUTTON
           
static int MIDDLE_BUTTON
           
static int NO_KEY_PRESSED
           
static int PAGE_DOWN_KEY
           
static int PAGE_UP_KEY
           
static int RIGHT_ARROW_KEY
           
static int RIGHT_BUTTON
           
static int SHIFT_KEY
           
static int TAB_KEY
           
static int UP_ARROW_KEY
           
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
DrawingPanel(int desiredWidth, int desiredHeight)
          Construct a drawing panel of a given width and height enclosed in a window.
 
Method Summary
 void closeWindow()
          Close the DrawingPanel (actually closes the JFrame)
 void copyGraphicsToScreen()
          Copy the offscreen graphics buffer to the screen.
 java.awt.Graphics2D getGraphics()
          Obtain the Graphics object needed to draw on the DrawingPanel's offscreen graphics buffer.
 int getKeyHitCode()
          Returns a single "key code" for a key that was pressed and released.
 int getMouseButton()
          Get which mouse button was most recently clicked.
 int getMouseClickX(int whichButton)
          Get the x coordinate of the mouse's location when the most recent mouse click of the specified button occurred.
 int getMouseClickY(int whichButton)
          Get the y coordinate of the mouse's location when the most recent mouse click of the specified button occurred.
 int getMouseX()
          Get the current x coordinate of the mouse.
 int getMouseY()
          Get the current y coordinate of the mouse.
 int getRGB(int x, int y)
          Return an integer representation of the color at a specified pixel location.
 boolean isMouseButtonDown(int whichButton)
          Determine if a specific mouse button is down at the time of the method call.
 boolean keyHasBeenHit(int whichKeyCode)
          Has the specified key been hit (pressed and released)?
 boolean keyIsDown(int whichKeyCode)
          Is the specified key down at this moment in time?
 void keyPressed(java.awt.event.KeyEvent e)
          Callback method for "key pressed" events (never call explicitly).
 void keyReleased(java.awt.event.KeyEvent e)
          Callback method for "key release" events (never call explicitly).
 void keyTyped(java.awt.event.KeyEvent e)
          Callback method for "key typed" events (never used - never call explicitly).
static java.awt.image.BufferedImage loadBitmap(java.lang.String filename)
          Load a bitmap image into memory.
 void mouseClicked(java.awt.event.MouseEvent e)
          MouseInputListener Callback (application never calls this method)
 boolean mouseClickHasOccurred(int whichButton)
          Determine if a mouse click has occurred.
 void mouseDragged(java.awt.event.MouseEvent e)
          MouseInputListener Callback (application never calls this method)
 void mouseEntered(java.awt.event.MouseEvent e)
          MouseInputListener Callback (application never calls this method)
 void mouseExited(java.awt.event.MouseEvent e)
          MouseInputListener Callback (application never calls this method)
 void mouseMoved(java.awt.event.MouseEvent e)
          MouseInputListener Callback (application never calls this method)
 void mousePressed(java.awt.event.MouseEvent e)
          MouseInputListener Callback (application never calls this method)
 void mouseReleased(java.awt.event.MouseEvent e)
          MouseInputListener Callback (application never calls this method)
 int numberOfKeysActive()
          Return the number of keys that are currently being pressed on the keyboard.
 void run()
          Run the GUI drawing panel in a separate thread so that the application that is drawing to the window can be paused and restarted (never called by an application).
 void saveGraphics(java.lang.String filename)
          Save the current graphics (in the offscreen buffer) to a file.
 void setBackground(java.awt.Color c)
          Clears the DrawingPanel's graphics window to the specified color.
 void setRGB(int x, int y, int RGB)
          Set the color at a specified pixel location.
 void setWindowTitle(java.lang.String name)
          Set the windows name -- which appears in the window's header.
 void sleep(int millis)
          Make your application "sleep" for the specified number of milliseconds.
 void waitForKeyHit()
          Wait for the user to hit a key on the keyboard -- your application goes to sleep until a key is pressed and released on the keyboard.
 void waitForKeyPressed()
          Wait for the user to press a key on the keyboard -- your application goes to sleep until a key is pressed.
 void waitForMouseClick(int whichButton)
          Wait for a mouse click on a specific mouse button (your application goes to sleep until the specific mouse button has been clicked (pressed and released)).
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

LEFT_BUTTON

public static final int LEFT_BUTTON
See Also:
Constant Field Values

MIDDLE_BUTTON

public static final int MIDDLE_BUTTON
See Also:
Constant Field Values

RIGHT_BUTTON

public static final int RIGHT_BUTTON
See Also:
Constant Field Values

NO_KEY_PRESSED

public static final int NO_KEY_PRESSED
See Also:
Constant Field Values

ANY_KEY

public static final int ANY_KEY
See Also:
Constant Field Values

F1_KEY

public static final int F1_KEY
See Also:
Constant Field Values

F2_KEY

public static final int F2_KEY
See Also:
Constant Field Values

F3_KEY

public static final int F3_KEY
See Also:
Constant Field Values

F4_KEY

public static final int F4_KEY
See Also:
Constant Field Values

F5_KEY

public static final int F5_KEY
See Also:
Constant Field Values

F6_KEY

public static final int F6_KEY
See Also:
Constant Field Values

F7_KEY

public static final int F7_KEY
See Also:
Constant Field Values

F8_KEY

public static final int F8_KEY
See Also:
Constant Field Values

F9_KEY

public static final int F9_KEY
See Also:
Constant Field Values

F10_KEY

public static final int F10_KEY
See Also:
Constant Field Values

F11_KEY

public static final int F11_KEY
See Also:
Constant Field Values

F12_KEY

public static final int F12_KEY
See Also:
Constant Field Values

LEFT_ARROW_KEY

public static final int LEFT_ARROW_KEY
See Also:
Constant Field Values

RIGHT_ARROW_KEY

public static final int RIGHT_ARROW_KEY
See Also:
Constant Field Values

UP_ARROW_KEY

public static final int UP_ARROW_KEY
See Also:
Constant Field Values

DOWN_ARROW_KEY

public static final int DOWN_ARROW_KEY
See Also:
Constant Field Values

INSERT_KEY

public static final int INSERT_KEY
See Also:
Constant Field Values

HOME_KEY

public static final int HOME_KEY
See Also:
Constant Field Values

DELETE_KEY

public static final int DELETE_KEY
See Also:
Constant Field Values

PAGE_UP_KEY

public static final int PAGE_UP_KEY
See Also:
Constant Field Values

PAGE_DOWN_KEY

public static final int PAGE_DOWN_KEY
See Also:
Constant Field Values

ESC_KEY

public static final int ESC_KEY
See Also:
Constant Field Values

TAB_KEY

public static final int TAB_KEY
See Also:
Constant Field Values

SHIFT_KEY

public static final int SHIFT_KEY
See Also:
Constant Field Values

ENTER_KEY

public static final int ENTER_KEY
See Also:
Constant Field Values
Constructor Detail

DrawingPanel

public DrawingPanel(int desiredWidth,
                    int desiredHeight)
Construct a drawing panel of a given width and height enclosed in a window.

Parameters:
desiredWidth - width of panel
desiredHeight - height of panel
Method Detail

run

public void run()
Run the GUI drawing panel in a separate thread so that the application that is drawing to the window can be paused and restarted (never called by an application).

Specified by:
run in interface java.lang.Runnable
Overrides:
run in class java.lang.Thread

setWindowTitle

public void setWindowTitle(java.lang.String name)
Set the windows name -- which appears in the window's header.

Parameters:
name - title of window

closeWindow

public void closeWindow()
Close the DrawingPanel (actually closes the JFrame)


mouseMoved

public void mouseMoved(java.awt.event.MouseEvent e)
MouseInputListener Callback (application never calls this method)

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

mouseEntered

public void mouseEntered(java.awt.event.MouseEvent e)
MouseInputListener Callback (application never calls this method)

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

mouseExited

public void mouseExited(java.awt.event.MouseEvent e)
MouseInputListener Callback (application never calls this method)

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

mousePressed

public void mousePressed(java.awt.event.MouseEvent e)
MouseInputListener Callback (application never calls this method)

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

mouseReleased

public void mouseReleased(java.awt.event.MouseEvent e)
MouseInputListener Callback (application never calls this method)

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

mouseDragged

public void mouseDragged(java.awt.event.MouseEvent e)
MouseInputListener Callback (application never calls this method)

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

mouseClicked

public void mouseClicked(java.awt.event.MouseEvent e)
MouseInputListener Callback (application never calls this method)

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

keyPressed

public void keyPressed(java.awt.event.KeyEvent e)
Callback method for "key pressed" events (never call explicitly). Known problem: if multiple keys are being held down simultaneously, the operating system does not always generate new key pressed events for new keys pressed.

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

keyReleased

public void keyReleased(java.awt.event.KeyEvent e)
Callback method for "key release" events (never call explicitly).

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

keyTyped

public void keyTyped(java.awt.event.KeyEvent e)
Callback method for "key typed" events (never used - never call explicitly).

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

getGraphics

public java.awt.Graphics2D getGraphics()
Obtain the Graphics object needed to draw on the DrawingPanel's offscreen graphics buffer. Make sure you call copyGraphicsToScreen() after all your drawing methods have been called to copy the offscreen graphics to the screen.


setBackground

public void setBackground(java.awt.Color c)
Clears the DrawingPanel's graphics window to the specified color. All previously drawn graphics are erased.

Parameters:
c - new background color

copyGraphicsToScreen

public void copyGraphicsToScreen()
Copy the offscreen graphics buffer to the screen. No graphics are visible until this method is called.


sleep

public void sleep(int millis)
Make your application "sleep" for the specified number of milliseconds.

Parameters:
millis - # of milliseconds

waitForMouseClick

public void waitForMouseClick(int whichButton)
Wait for a mouse click on a specific mouse button (your application goes to sleep until the specific mouse button has been clicked (pressed and released)).

Parameters:
whichButton - which mouse button you want to wait for (DrawingPanel.LEFT_BUTTON, DrawingPanel.MIDDLE_BUTTON, or DrawingPanel.RIGHT_BUTTON).

mouseClickHasOccurred

public boolean mouseClickHasOccurred(int whichButton)
Determine if a mouse click has occurred. Calling this method always resets the status of the specified button to "unclicked." If you want the location of the click, immediately call getMouseClickX(whichButton) and/or getMouseClickY(whichButton)

Parameters:
whichButton - which mouse button you want to check for a click (DrawingPanel.LEFT_BUTTON, DrawingPanel.MIDDLE_BUTTON, or DrawingPanel.RIGHT_BUTTON).
Returns:
true if a mouse click using the specified button has occurred since the last call to this method, false otherwise.

isMouseButtonDown

public boolean isMouseButtonDown(int whichButton)
Determine if a specific mouse button is down at the time of the method call.

Parameters:
whichButton - which mouse button you want to check the status of (DrawingPanel.LEFT_BUTTON, DrawingPanel.MIDDLE_BUTTON, or DrawingPanel.RIGHT_BUTTON).
Returns:
true if a mouse button is down, false otherwise.

getMouseButton

public int getMouseButton()
Get which mouse button was most recently clicked.

Returns:
button code (DrawingPanel.LEFT_BUTTON, DrawingPanel.MIDDLE_BUTTON, or DrawingPanel.RIGHT_BUTTON)

getMouseX

public int getMouseX()
Get the current x coordinate of the mouse.

Returns:
x coordinate of the mouse in pixels (left side of window is 0, right side is the (DrawingPanel's width - 1).

getMouseY

public int getMouseY()
Get the current y coordinate of the mouse.

Returns:
y coordinate of the mouse in pixels (top of window is 0, bottom is the (DrawingPanel's height - 1).

getMouseClickX

public int getMouseClickX(int whichButton)
Get the x coordinate of the mouse's location when the most recent mouse click of the specified button occurred. Call the mouseClickHasOccurred(whichButton) method to determine if a mouse click has occurred. If no mouse click has occurred, this method returns a bogus x coordinate.

Returns:
x coordinate of the mouse (in pixels) when the most recent mouse click for the specified button occurred.

getMouseClickY

public int getMouseClickY(int whichButton)
Get the y coordinate of the mouse's location when the most recent mouse click of the specified button occurred. Call the mouseClickHasOccurred(whichButton) method to determine if a mouse click has occurred. If no mouse click has occurred, this method returns a bogus y coordinate.

Returns:
y coordinate of the mouse (in pixels) when the most recent mouse click for the specified button occurred.

waitForKeyHit

public void waitForKeyHit()
Wait for the user to hit a key on the keyboard -- your application goes to sleep until a key is pressed and released on the keyboard.


keyIsDown

public boolean keyIsDown(int whichKeyCode)
Is the specified key down at this moment in time?

Parameters:
whichKeyCode - which key to check
Returns:
true if the specified key is down, false otherwise.

keyHasBeenHit

public boolean keyHasBeenHit(int whichKeyCode)
Has the specified key been hit (pressed and released)?

Parameters:
whichKeyCode - which key to check
Returns:
true if the specified key has been hit, false otherwise.

waitForKeyPressed

public void waitForKeyPressed()
Wait for the user to press a key on the keyboard -- your application goes to sleep until a key is pressed.


getKeyHitCode

public int getKeyHitCode()
Returns a single "key code" for a key that was pressed and released. If multiple keys are hit at the same time, the order of the keys returned is indeterminate. Key hits are bufferred. Therefore, if multiple keys are hit at the same time, subsequent calls to getKeyHitCode will return the other keys. A "key hit" will only be returned once.

Returns:
returns a key code for the latest key that was pressed and released by a user.

The integer values represents the key that was pressed (and not the upper or lower case of the key).

  • For the digits, the codes are equivalent to the Unicode character '0' - '9'
  • For the characters, the codes are equivalent to the Unicode characters 'A' - 'Z' (NOTE: CAPITAL LETTERS)
  • For the function keys, use the named constants F1_KEY, ... F12_KEY
  • For the other special keys, use:
    • DrawingPanel.LEFT_ARROW_KEY
    • DrawingPanel.RIGHT_ARROW_KEY
    • DrawingPanel.UP_ARROW_KEY
    • DrawingPanel.DOWN_ARROW_KEY
    • DrawingPanel.INSERT_KEY
    • DrawingPanel.HOME_KEY
    • DrawingPanel.DELETE_KEY
    • DrawingPanel.PAGE_UP_KEY
    • DrawingPanel.PAGE_DOWN_KEY
    • DrawingPanel.ESC_KEY
    • DrawingPanel.TAB_KEY
    • DrawingPanel.SHIFT_KEY
    • DrawingPanel.ENTER_KEY

numberOfKeysActive

public int numberOfKeysActive()
Return the number of keys that are currently being pressed on the keyboard.

If you call the getKeyCode() the number of times returned by this method, you will get the key codes for all keys currently being held down.

Returns:
number of pressed keys

getRGB

public int getRGB(int x,
                  int y)
Return an integer representation of the color at a specified pixel location.

Returns:
integer RGB value

setRGB

public void setRGB(int x,
                   int y,
                   int RGB)
Set the color at a specified pixel location.

Parameters:
x - x coordinate
y - y coordinate
RGB - color

saveGraphics

public void saveGraphics(java.lang.String filename)
Save the current graphics (in the offscreen buffer) to a file. The file name should have an appropriate image file extension, such as ".bmp" or ".jpg"

Parameters:
filename - name of file

loadBitmap

public static java.awt.image.BufferedImage loadBitmap(java.lang.String filename)
Load a bitmap image into memory.

Parameters:
filename - the name of the bitmap image file (including the .bmp extension)
Returns:
a BufferedImage object