Class TGCanvas

java.lang.Object
  extended by java.awt.Component
      extended by TGCanvas
All Implemented Interfaces:
java.awt.event.FocusListener, java.awt.event.KeyListener, java.awt.event.MouseListener, java.awt.event.MouseMotionListener, java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable, java.util.EventListener

public class TGCanvas
extends java.awt.Component
implements java.awt.event.FocusListener, java.awt.event.KeyListener, java.awt.event.MouseListener, java.awt.event.MouseMotionListener

This class is an implementation of a graphics canvas window for TG. All graphics (pixels painted) are maintained in an Image for redisplay in the event of resizing.

Other features of TGCanvas are:

- the coordinate space is the traditional mathematics; [0,0] is in the middle of the graphics window.

- KeyListener method invocations are handled and may be passed on.

- MouseListener.mouseReleased() and MouseMotionListener.mouseMoved() are captured and can be passed on.

- higher-level functionality (i.e., flood-fill provided).

- in-memory Image maintained with buffered operations to improve graphics display performance.

See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class java.awt.Component
java.awt.Component.AccessibleAWTComponent, java.awt.Component.BaselineResizeBehavior, java.awt.Component.BltBufferStrategy, java.awt.Component.FlipBufferStrategy
 
Field Summary
static int AQUA
           
static int BLACK
          The SETPENCOLOR command's input is a built-in color number or an RGB value.
static int BLUE
           
static int BROWN
           
static int CHOCOLATE
           
static int CYAN
           
static int EAST
          Turtle heading for the positive X axis.
static int FOREST
           
static int GOLD
           
static int GRAY
           
static int GREEN
           
static int KHAKI
           
static int LAWNGREEN
           
static int LIGHTBLUE
           
static int LIGHTGRAY
           
static int LIME
           
static java.awt.Color[] LOGO_COLORS
          All derivations of Logo have a basic, minimum set of colors that are specified by numbers.
static int MAGENTA
           
static int MAX_LOGO_COLOR
          The maximum value for a Logo color which require conversion to RGB colors used by Java.
static int NAVY
           
static int NORTH
          Turtle heading for the positive Y axis.
static int OLIVE
           
static int ORANGE
           
static int PALEGREEN
           
static int PERU
           
static int PINK
           
static int PURPLE
           
static int RED
           
static int SALMON
           
static int SKYBLUE
           
static int SOUTH
          Turtle heading for the negative Y axis.
static int STEELBLUE
           
static int TAN
           
static int VIOLET
           
static int WEST
          Turtle heading for the negative X axis.
static int WHEAT
           
static int WHITE
           
static int YELLOW
           
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
TGCanvas()
          Instantiate a turtle graphics canvas with initial width of 700 turtle steps and a height of 400 turtle steps.
TGCanvas(int width, int height)
          Instantiate a turtle graphics canvas of a specified width and height.
 
Method Summary
 void addGraphOp(TGGraphicsOp grafOp)
          Append a graphics operation, a TGGraphicsOp object, to the queue of outstanding operations.
 void addKeyHandler(TGKeyHandler kh)
          Add an object which implements TGKeyHandler to the list of those that want a callback when a key is pressed while the graphics canvas has focus.
 void addMouseHandler(TGMouseHandler mh)
          Add an object which implements TGMouseHandler to the list of those that want a callback.
 void addSprite(Sprite sprite)
          Add a new Sprite, start displaying its image on the graphics canvas.
 int canvasHeight()
          Return the height of the graphics canvas.
 int canvasWidth()
          Return the width of the graphics canvas.
 void clean()
          Clean graphics off of the display.
 void focusGained(java.awt.event.FocusEvent e)
          Invoked when this Component gets focus.
 void focusLost(java.awt.event.FocusEvent e)
          Invoked when this Component loses focus.
 int getBackgroundColor()
          Return the background Logo color number.
 java.awt.image.BufferedImage getPicture()
          Return the contents of the graphics canvas overlaid with existing Sprite images as a composite BufferedImage.
 int[] getPixels(TGPoint topLeft, int width, int height)
          Return an array of pixel RGB values.
 void keyPressed(java.awt.event.KeyEvent ke)
          Invoked when a key has been pressed.
 void keyReleased(java.awt.event.KeyEvent ke)
          Invoked when a key has been released.
 void keyTyped(java.awt.event.KeyEvent ke)
          Invoked when a key has been typed.
 void loadPicture(java.awt.Image pictureImage)
          Load the provided array of pixels into the background.
static java.awt.Color logoColorToJavaColor(int logoColor)
          Convert a Logo color to a Java Color object.
static int logoColorToRGB(int logoColor)
          Convert a Logo color to a Java RGB int with the alpha component maintained.
 void mouseClicked(java.awt.event.MouseEvent me)
          Invoked when a mouse button has been clicked (pressed and released) on this Component.
 void mouseDragged(java.awt.event.MouseEvent me)
          Invoked when a mouse button is held down on TGCanvas and the mouse is moved.
 void mouseEntered(java.awt.event.MouseEvent me)
          Invoked when the mouse enters a Component.
 void mouseExited(java.awt.event.MouseEvent me)
          Invoked when the mouse exits a Component.
 void mouseMoved(java.awt.event.MouseEvent me)
          Invoked when the mouse is moved around on the TGCanvas without any buttons in a depressed state.
 void mousePressed(java.awt.event.MouseEvent me)
          Invoked when a mouse button has been pressed while on a Component.
 void mouseReleased(java.awt.event.MouseEvent me)
          Invoked when a mouse button has been released on TGCanvas.
 int mousex()
          Return the TurtleSpace x-coordinate of the mouse when it was last clicked.
 int mousey()
          Return the TurtleSpace y-coordinate of the mouse when it was last clicked.
 void paint(java.awt.Graphics g)
          Paints this Component.
 void removeKeyHandler(TGKeyHandler kh)
          Remove an object from TGCanvas' list of those wanting their keyPressed() method invoked when TGCanvas receives this Event.
 void removeMouseHandler(TGMouseHandler mh)
          Remove an object from TGCanvas' list of those wanting their mouseClicked() and mouseMoved() methods invoked when we receive these Events.
 void removeSprite(Sprite sprite)
          Remove the specified Sprite from the list of sprites whose images are displayed on the graphics canvas.
static int rgbToLogoColor(int rgbValue)
          Return a Logo pen color number given an ARGB or RGB value.
 void setbg(int logoColor)
          Set the background color of the graphics canvas.
 void setBounds(int x, int y, int width, int height)
          Moves and resizes this Component.
 void setSize(int width, int height)
          Resizes this Component to have the specified width and height.
 void update(java.awt.Graphics gfx)
          Update the displayed image of this Component.
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, addNotify, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, deliverEvent, disable, disableEvents, dispatchEvent, doLayout, enable, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAccessibleContext, getAlignmentX, getAlignmentY, getBackground, getBaseline, getBaselineResizeBehavior, getBounds, getBounds, getColorModel, getComponentAt, getComponentAt, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeys, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getListeners, getLocale, getLocation, getLocation, getLocationOnScreen, getMaximumSize, getMinimumSize, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPreferredSize, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, invalidate, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusCycleRoot, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, layout, list, list, list, list, list, locate, location, lostFocus, minimumSize, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, paramString, postEvent, preferredSize, prepareImage, prepareImage, print, printAll, processComponentEvent, processEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, reshape, resize, resize, setBackground, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeys, setFocusTraversalKeysEnabled, setFont, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setMinimumSize, setName, setPreferredSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle, validate
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

NORTH

public static final int NORTH
Turtle heading for the positive Y axis. The Logo turtle's heading in degrees does not match the standard mathematics notion of having the positive X axis as zero, with degrees increasing counter-clockwise. Logo has the positive Y axis as zero with degrees increasing clockwise. The use of abstract symbolic constants for north, east, south, and west help.

See Also:
Constant Field Values

EAST

public static final int EAST
Turtle heading for the positive X axis. The Logo turtle's heading in degrees does not match the standard mathematics notion of having the positive X axis as zero, with degrees increasing counter-clockwise. Logo has the positive Y axis as zero with degrees increasing clockwise. The use of abstract symbolic constants for north, east, south, and west help.

See Also:
Constant Field Values

SOUTH

public static final int SOUTH
Turtle heading for the negative Y axis. The Logo turtle's heading in degrees does not match the standard mathematics notion of having the positive X axis as zero, with degrees increasing counter-clockwise. Logo has the positive Y axis as zero with degrees increasing clockwise. The use of abstract symbolic constants for north, east, south, and west help.

See Also:
Constant Field Values

WEST

public static final int WEST
Turtle heading for the negative X axis. The Logo turtle's heading in degrees does not match the standard mathematics notion of having the positive X axis as zero, with degrees increasing counter-clockwise. Logo has the positive Y axis as zero with degrees increasing clockwise. The use of abstract symbolic constants for north, east, south, and west help.

See Also:
Constant Field Values

BLACK

public static final int BLACK
The SETPENCOLOR command's input is a built-in color number or an RGB value. Built-in colors numbers are in range of 0-31 with 0 for black.

See Also:
Constant Field Values

BLUE

public static final int BLUE
See Also:
Constant Field Values

GREEN

public static final int GREEN
See Also:
Constant Field Values

CYAN

public static final int CYAN
See Also:
Constant Field Values

RED

public static final int RED
See Also:
Constant Field Values

MAGENTA

public static final int MAGENTA
See Also:
Constant Field Values

YELLOW

public static final int YELLOW
See Also:
Constant Field Values

WHITE

public static final int WHITE
See Also:
Constant Field Values

BROWN

public static final int BROWN
See Also:
Constant Field Values

TAN

public static final int TAN
See Also:
Constant Field Values

FOREST

public static final int FOREST
See Also:
Constant Field Values

AQUA

public static final int AQUA
See Also:
Constant Field Values

SALMON

public static final int SALMON
See Also:
Constant Field Values

VIOLET

public static final int VIOLET
See Also:
Constant Field Values

ORANGE

public static final int ORANGE
See Also:
Constant Field Values

GRAY

public static final int GRAY
See Also:
Constant Field Values

NAVY

public static final int NAVY
See Also:
Constant Field Values

SKYBLUE

public static final int SKYBLUE
See Also:
Constant Field Values

LIME

public static final int LIME
See Also:
Constant Field Values

STEELBLUE

public static final int STEELBLUE
See Also:
Constant Field Values

CHOCOLATE

public static final int CHOCOLATE
See Also:
Constant Field Values

PURPLE

public static final int PURPLE
See Also:
Constant Field Values

GOLD

public static final int GOLD
See Also:
Constant Field Values

LIGHTGRAY

public static final int LIGHTGRAY
See Also:
Constant Field Values

PERU

public static final int PERU
See Also:
Constant Field Values

WHEAT

public static final int WHEAT
See Also:
Constant Field Values

PALEGREEN

public static final int PALEGREEN
See Also:
Constant Field Values

LIGHTBLUE

public static final int LIGHTBLUE
See Also:
Constant Field Values

KHAKI

public static final int KHAKI
See Also:
Constant Field Values

PINK

public static final int PINK
See Also:
Constant Field Values

LAWNGREEN

public static final int LAWNGREEN
See Also:
Constant Field Values

OLIVE

public static final int OLIVE
See Also:
Constant Field Values

MAX_LOGO_COLOR

public static final int MAX_LOGO_COLOR
The maximum value for a Logo color which require conversion to RGB colors used by Java.

See Also:
Constant Field Values

LOGO_COLORS

public static final java.awt.Color[] LOGO_COLORS
All derivations of Logo have a basic, minimum set of colors that are specified by numbers. jLogo supports a set of sixteen, compatible with Brian Harvey's Berkeley Logo.

Constructor Detail

TGCanvas

public TGCanvas()
Instantiate a turtle graphics canvas with initial width of 700 turtle steps and a height of 400 turtle steps.


TGCanvas

public TGCanvas(int width,
                int height)
Instantiate a turtle graphics canvas of a specified width and height.

Method Detail

focusGained

public void focusGained(java.awt.event.FocusEvent e)
Invoked when this Component gets focus. Note that this has happened, that the graphics canvas now gets keyboard and mouse events.

Specified by:
focusGained in interface java.awt.event.FocusListener

focusLost

public void focusLost(java.awt.event.FocusEvent e)
Invoked when this Component loses focus. Note that this has happened.

Specified by:
focusLost in interface java.awt.event.FocusListener

keyPressed

public void keyPressed(java.awt.event.KeyEvent ke)
Invoked when a key has been pressed.

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

keyReleased

public void keyReleased(java.awt.event.KeyEvent ke)
Invoked when a key has been released.

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

keyTyped

public void keyTyped(java.awt.event.KeyEvent ke)
Invoked when a key has been typed.

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

mouseClicked

public void mouseClicked(java.awt.event.MouseEvent me)
Invoked when a mouse button has been clicked (pressed and released) on this Component.

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

mouseEntered

public void mouseEntered(java.awt.event.MouseEvent me)
Invoked when the mouse enters a Component.

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

mouseExited

public void mouseExited(java.awt.event.MouseEvent me)
Invoked when the mouse exits a Component.

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

mousePressed

public void mousePressed(java.awt.event.MouseEvent me)
Invoked when a mouse button has been pressed while on a Component.

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

mouseReleased

public void mouseReleased(java.awt.event.MouseEvent me)
Invoked when a mouse button has been released on TGCanvas.

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

mouseDragged

public void mouseDragged(java.awt.event.MouseEvent me)
Invoked when a mouse button is held down on TGCanvas and the mouse is moved.

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

mouseMoved

public void mouseMoved(java.awt.event.MouseEvent me)
Invoked when the mouse is moved around on the TGCanvas without any buttons in a depressed state.

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

paint

public void paint(java.awt.Graphics g)
Paints this Component.

Something has occured that requires updating the graphics canvas. As an example, TGGraphicsOps have been queued to be performed, or a Sprite whose image is being displayed has moved or rotated, or the AWT has decided we need to redraw at least some subset of our pixels, e.g., partially covered stuff (by some other application on the desktop) has moved/gone away, etc...

Overrides:
paint in class java.awt.Component

setBounds

public void setBounds(int x,
                      int y,
                      int width,
                      int height)
Moves and resizes this Component. The new location of the top-left corner is specified by x and y. The new size is specified by width and height.

Overrides:
setBounds in class java.awt.Component

setSize

public void setSize(int width,
                    int height)
Resizes this Component to have the specified width and height.

Overrides:
setSize in class java.awt.Component

update

public void update(java.awt.Graphics gfx)
Update the displayed image of this Component.

Overridden in TGCanvas to eliminate Component.update()'s invocation of Graphics.clear().

Overrides:
update in class java.awt.Component

addGraphOp

public void addGraphOp(TGGraphicsOp grafOp)
Append a graphics operation, a TGGraphicsOp object, to the queue of outstanding operations.


addKeyHandler

public void addKeyHandler(TGKeyHandler kh)
Add an object which implements TGKeyHandler to the list of those that want a callback when a key is pressed while the graphics canvas has focus. The object's keyPressed() method is invoked.


addMouseHandler

public void addMouseHandler(TGMouseHandler mh)
Add an object which implements TGMouseHandler to the list of those that want a callback. A callback is performed when the graphics canvas has focus and


addSprite

public void addSprite(Sprite sprite)
Add a new Sprite, start displaying its image on the graphics canvas.


canvasHeight

public int canvasHeight()
Return the height of the graphics canvas.


canvasWidth

public int canvasWidth()
Return the width of the graphics canvas.


clean

public void clean()
Clean graphics off of the display.


getBackgroundColor

public int getBackgroundColor()
Return the background Logo color number. Values zero through fifteen (inclusive) are fixed Logo colors (black, blue, etc...) other values are RGB numbers, with or without alpha values.


getPicture

public java.awt.image.BufferedImage getPicture()
Return the contents of the graphics canvas overlaid with existing Sprite images as a composite BufferedImage.


getPixels

public int[] getPixels(TGPoint topLeft,
                       int width,
                       int height)
Return an array of pixel RGB values. A rectangular area of the graphics canvas defined by a top-left corner (in TurtleSpace coordinates) and its width and height.

Since all drawing operations are queued to be performed by the paint() method, we must wait for all outstanding operations to complete before we grab pixels.


loadPicture

public void loadPicture(java.awt.Image pictureImage)
Load the provided array of pixels into the background.

*NOTE* a side-effect of changing the background color is that all current graphics is cleared.


logoColorToJavaColor

public static java.awt.Color logoColorToJavaColor(int logoColor)
Convert a Logo color to a Java Color object. If the alpha component is zero it is replaced with 0xFF - assumed to be an opaque color.


logoColorToRGB

public static int logoColorToRGB(int logoColor)
Convert a Logo color to a Java RGB int with the alpha component maintained.


mousex

public int mousex()
Return the TurtleSpace x-coordinate of the mouse when it was last clicked.

See Also:
mousey()

mousey

public int mousey()
Return the TurtleSpace y-coordinate of the mouse when it was last clicked.

See Also:
mousex()

removeKeyHandler

public void removeKeyHandler(TGKeyHandler kh)
Remove an object from TGCanvas' list of those wanting their keyPressed() method invoked when TGCanvas receives this Event.


removeMouseHandler

public void removeMouseHandler(TGMouseHandler mh)
Remove an object from TGCanvas' list of those wanting their mouseClicked() and mouseMoved() methods invoked when we receive these Events.


removeSprite

public void removeSprite(Sprite sprite)
Remove the specified Sprite from the list of sprites whose images are displayed on the graphics canvas.


rgbToLogoColor

public static int rgbToLogoColor(int rgbValue)
Return a Logo pen color number given an ARGB or RGB value.


setbg

public void setbg(int logoColor)
Set the background color of the graphics canvas.

*NOTE* a side-effect of changing the background color is that all current graphics is cleared.