ch.aplu.turtle
Class Playground

java.lang.Object
  |
  +--java.awt.Component
        |
        +--java.awt.Container
              |
              +--javax.swing.JComponent
                    |
                    +--javax.swing.JPanel
                          |
                          +--ch.aplu.turtle.Playground
All Implemented Interfaces:
Accessible, ImageObserver, MenuContainer, Serializable

public class Playground
extends JPanel
implements ImageObserver

A Playground is the Turtle's home, i.e. the Turtle lives and moves in the Playground. ThePlayground is responsible for interpreting angle and position of the Turtle and for choosing the correct turtle image and putting it on the right spot of the Playground. This means: e.g. whenever you wish to switch the x- and y-axis, you should do it in this class, and not in the Turtle class. Remarks:

Version:
0.1.1
Author:
Regula Hoefer-Isenegger
See Also:
Serialized Form

Nested Class Summary
 
Nested classes inherited from class javax.swing.JPanel
JPanel.AccessibleJPanel
 
Nested classes inherited from class javax.swing.JComponent
JComponent.AccessibleJComponent
 
Nested classes inherited from class java.awt.Container
Container.AccessibleAWTContainer
 
Nested classes inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.BltBufferStrategy, Component.FlipBufferStrategy
 
Field Summary
protected  Color DEFAULT_BACKGROUND_COLOR
          The default background color.
 
Fields inherited from class javax.swing.JComponent
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
 
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
Playground(TurtleContainer rootpane)
          Create a new Playground inside the given TurtleContainer and with standard size (400 x 400 pixels).
Playground(TurtleContainer rootpane, Dimension size)
          Create a new Playground inside the given TurtleContainer and size.
 
Method Summary
 void add(Turtle turtle)
          Adds a new Turtle to the Playground.
 void blitToOffscreenBuffer()
          Draws the whole canvas and turtle buffers into the offscreen buffer.
 void blitToOffscreenBuffer(Image buffer)
          Draws an image into the offscreen buffer.
 void blitToOffscreenBuffer(Image buffer, int x, int y, int w, int h)
          Draws part (a rectangle defined by the parameters) of an image into the offscreen buffer.
 void blitToOffscreenBuffer(int x, int y, int w, int h)
          Draws part of canvas and turtle buffers into the offscreen buffer, according to the rectangle definded by the parameters.
 void blitToOffscreenBuffer(Rectangle r)
          Draws part of canvas and turtle buffers into the offscreen buffer, according to the given rectangle.
 void clear()
          Clears the canvas (i.e. the buffer where all turtle lines are drawn).
 void clear(Graphics g)
          Clears the buffer with the given Graphics.
protected  void clearClipTurtle(Turtle turtle)
          This method is called when the given Turtle is in clip mode.
protected  void clearClipTurtle(Turtle turtle, Image im)
          Here the actual clearing of a Turtle in clip mode from the given image is performed.
 void clearTurtle(Turtle turtle)
          Removes the image of the given turtle from the turtle buffer.
 void clearTurtles()
          Removes the image of all turtles from the turtle buffer.
protected  void clearWrapTurtle(Turtle turtle)
          This method is called when the given Turtle is in wrap mode.
protected  void clearWrapTurtle(Turtle turtle, Image im)
          Here the actual clearing of a Turtle in wrap mode from the given image is performed.
 int countTurtles()
          Tells how many Turtles are now in this Playground.
 void fill(Turtle t)
          Fills a region.
protected  Rectangle getBounds(Turtle turtle)
          Calculates the bounds of the Turtles picture on the screen.
 Turtle getTurtle(int index)
          Returns the Turtle at index index.
 boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height)
           
protected  void init(TurtleContainer rootpane, Dimension size)
          Initializes everything, e.g. creates a new vector (which holds the Turtles), the offscreen buffers, and sets the size and background color.
protected  Point2D.Double internalToScreenCoords(double x, double y)
          Calculates the screen coordinates.
 void label(String text, Turtle t)
          Draws the text at the current position of the Turtle t.
 void paintComponent()
          Paints the Playground.
 void paintComponent(Graphics g)
          Draws the canvas and turtle buffers.
 void paintTurtles()
          Just paint all turtles.
 void paintTurtles(Turtle turtle)
          Moves the given Turtle above all the others, then paints all turtles.
 void remove(Turtle turtle)
          Removes a Turtle from the Playground.
 Turtle toBottom(Turtle turtle)
          Puts a Turtle beyond all others.
 Turtle toTop(Turtle turtle)
          Puts a Turtle above all others.
 
Methods inherited from class javax.swing.JPanel
getAccessibleContext, getUI, getUIClassID, paramString, setUI, updateUI
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addPropertyChangeListener, addPropertyChangeListener, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getGraphics, getHeight, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPreferredSize, getPropertyChangeListeners, getPropertyChangeListeners, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isPreferredSizeSet, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFont, setForeground, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setLayout, transferFocusBackward, transferFocusDownCycle, validate, validateTree
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMouseWheelListeners, getName, getParent, getPeer, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULT_BACKGROUND_COLOR

protected Color DEFAULT_BACKGROUND_COLOR
The default background color. White.

Constructor Detail

Playground

public Playground(TurtleContainer rootpane)
Create a new Playground inside the given TurtleContainer and with standard size (400 x 400 pixels).


Playground

public Playground(TurtleContainer rootpane,
                  Dimension size)
Create a new Playground inside the given TurtleContainer and size.

Method Detail

init

protected void init(TurtleContainer rootpane,
                    Dimension size)
Initializes everything, e.g. creates a new vector (which holds the Turtles), the offscreen buffers, and sets the size and background color.

See Also:
DEFAULT_BACKGROUND_COLOR

add

public void add(Turtle turtle)
Adds a new Turtle to the Playground.


remove

public void remove(Turtle turtle)
Removes a Turtle from the Playground.


countTurtles

public int countTurtles()
Tells how many Turtles are now in this Playground.


getTurtle

public Turtle getTurtle(int index)
Returns the Turtle at index index.


paintTurtles

public void paintTurtles(Turtle turtle)
Moves the given Turtle above all the others, then paints all turtles.

See Also:
toTop(ch.aplu.turtle.Turtle)

paintTurtles

public void paintTurtles()
Just paint all turtles.


fill

public void fill(Turtle t)
Fills a region. The region is defined by the Turtles actual position and is bounded by any other color than the background color.
Remark: If the Turtle is already standing on a colored Pixel, nothing happens.


clear

public void clear()
Clears the canvas (i.e. the buffer where all turtle lines are drawn).


clear

public void clear(Graphics g)
Clears the buffer with the given Graphics.


paintComponent

public void paintComponent()
Paints the Playground. This is Just a method for convenience.


paintComponent

public void paintComponent(Graphics g)
Draws the canvas and turtle buffers.

Overrides:
paintComponent in class JComponent

blitToOffscreenBuffer

public void blitToOffscreenBuffer()
Draws the whole canvas and turtle buffers into the offscreen buffer.


blitToOffscreenBuffer

public void blitToOffscreenBuffer(int x,
                                  int y,
                                  int w,
                                  int h)
Draws part of canvas and turtle buffers into the offscreen buffer, according to the rectangle definded by the parameters.

Parameters:
x - the rectangle's x-coordinate
y - the rectangle's y-coordinate
w - the rectangle's width
h - the rectangle's height

blitToOffscreenBuffer

public void blitToOffscreenBuffer(Rectangle r)
Draws part of canvas and turtle buffers into the offscreen buffer, according to the given rectangle.


blitToOffscreenBuffer

public void blitToOffscreenBuffer(Image buffer)
Draws an image into the offscreen buffer.

Parameters:
buffer - The image to be drawn.

blitToOffscreenBuffer

public void blitToOffscreenBuffer(Image buffer,
                                  int x,
                                  int y,
                                  int w,
                                  int h)
Draws part (a rectangle defined by the parameters) of an image into the offscreen buffer.

Parameters:
buffer - The image to be drawn.
x - the rectangle's x-coordinate
y - the rectangle's y-coordinate
w - the rectangle's width
h - the rectangle's height

clearTurtles

public void clearTurtles()
Removes the image of all turtles from the turtle buffer. This is used for repainting.


clearTurtle

public void clearTurtle(Turtle turtle)
Removes the image of the given turtle from the turtle buffer. This is used for repainting.
Only override this method if you have added a new behaviour (like wrap or clip) to the turtle.


clearWrapTurtle

protected void clearWrapTurtle(Turtle turtle)
This method is called when the given Turtle is in wrap mode.

See Also:
Turtle.wrap()

clearWrapTurtle

protected void clearWrapTurtle(Turtle turtle,
                               Image im)
Here the actual clearing of a Turtle in wrap mode from the given image is performed.


clearClipTurtle

protected void clearClipTurtle(Turtle turtle)
This method is called when the given Turtle is in clip mode.

See Also:
Turtle.clip()

clearClipTurtle

protected void clearClipTurtle(Turtle turtle,
                               Image im)
Here the actual clearing of a Turtle in clip mode from the given image is performed.


toTop

public Turtle toTop(Turtle turtle)
Puts a Turtle above all others.


toBottom

public Turtle toBottom(Turtle turtle)
Puts a Turtle beyond all others.


imageUpdate

public boolean imageUpdate(Image img,
                           int infoflags,
                           int x,
                           int y,
                           int width,
                           int height)
Specified by:
imageUpdate in interface ImageObserver
Overrides:
imageUpdate in class Component

internalToScreenCoords

protected Point2D.Double internalToScreenCoords(double x,
                                                double y)
Calculates the screen coordinates. I.e. the interpretation of the coordinates.


getBounds

protected Rectangle getBounds(Turtle turtle)
Calculates the bounds of the Turtles picture on the screen.


label

public void label(String text,
                  Turtle t)
Draws the text at the current position of the Turtle t. Drawing a text at some coordinates (x,y) we mean that the bottom left corner of the text will be at these coordinates. Font and colour are specified by the Turtle's Pen.