ch.aplu.turtle
Class Playground

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by javax.swing.JComponent
              extended by javax.swing.JPanel
                  extended by ch.aplu.turtle.Playground
All Implemented Interfaces:
java.awt.image.ImageObserver, java.awt.MenuContainer, java.awt.print.Printable, java.io.Serializable, javax.accessibility.Accessible
Direct Known Subclasses:
TurtlePane

public class Playground
extends javax.swing.JPanel
implements java.awt.print.Printable

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:

See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class javax.swing.JComponent
javax.swing.JComponent.AccessibleJComponent
 
Nested classes/interfaces inherited from class java.awt.Component
java.awt.Component.BaselineResizeBehavior
 
Field Summary
 
Fields inherited from class javax.swing.JComponent
TOOL_TIP_TEXT_KEY, 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.print.Printable
NO_SUCH_PAGE, PAGE_EXISTS
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
Playground()
          Create a Playground inside the given TurtleContainer and with standard size (400 x 400 pixels) and default background color.
Playground(java.awt.Color bkColor)
          Create a Playground inside the given TurtleContainer and with standard size (400 x 400 pixels) and given background color.
Playground(java.awt.Dimension size)
          Create a new Playground inside the given TurtleContainer and size.
Playground(java.awt.Dimension size, java.awt.Color bkColor)
          Create a new Playground inside the given TurtleContainer, size and background color.
 
Method Summary
 void add(Turtle turtle)
          Add a new Turtle to the Playground.
 void clear()
          Clear playground.
 void clear(java.awt.Color color)
          Clear the playground with given color.
 void clearTurtle(Turtle turtle)
          Remove the given turtle from the turtle buffer.
 void clearTurtles()
          Remove all turtles from the turtle buffer.
 int countTurtles()
          Tell current number of Turtles in this Playground.
 void enableRepaint(boolean b)
           
 void fill(Turtle t, java.awt.Color bgColor)
          Fill a region.
 java.awt.Color getPixelColor(Turtle t)
          Return the color of the pixel at the current turtle position.
 java.awt.Graphics2D getPrinterG2D()
          Return the graphics context of the printer.
 java.awt.Dimension getSize()
          Return the size of the playground.
 java.awt.image.BufferedImage getTraceBuffer()
          Return the image of the trace buffer.
 java.awt.Graphics2D getTraceG2D()
          Return the graphics context of the trace buffer.
 Turtle getTurtle(int index)
          Return the Turtle at index index.
 java.awt.image.BufferedImage getTurtleBuffer()
          Return the image of the turtle buffer.
 java.awt.Graphics2D getTurtleG2D()
          Returnt the graphics context of the turtle buffer.
 void label(java.lang.String text, Turtle t)
          Draw the text at the current position of the Turtle t.
 void paintComponent()
          Paint the Playground.
 void paintComponent(java.awt.Graphics g)
          Draw the trace and turtle buffers.
 void paintTurtle(Turtle turtle)
          Paint the given Turtle
 void paintTurtles()
          Paint all turtles (calling paintComponent())
 void paintTurtles(Turtle turtle)
          Move the given Turtle above all the others, then paints all turtles.
 int print(java.awt.Graphics g, java.awt.print.PageFormat pf, int pageIndex)
          For internal use only.
 void remove(Turtle turtle)
          Remove a Turtle from the Playground.
 void setAntiAliasing(boolean on)
          Set antialiasing on or off for the turtle trace buffer This may result in an better trace quality.
 Turtle toBottom(Turtle turtle)
          Put a Turtle below all others.
 java.awt.geom.Point2D.Double toScreenCoords(double x, double y)
          Calculate the screen coordinates of the given point coordinates.
 java.awt.geom.Point2D.Double toScreenCoords(java.awt.geom.Point2D.Double p)
          Calculate the screen coordinates of the given point.
 Turtle toTop(Turtle turtle)
          Puts a Turtle above all others.
 java.awt.geom.Point2D.Double toTurtleCoords(double x, double y)
          Calculate the turtle coordinates of the given screen coordinates.
 java.awt.geom.Point2D.Double toTurtleCoords(java.awt.geom.Point2D.Double p)
          Calculate the turtle coordinates of the given screen point.
 
Methods inherited from class javax.swing.JPanel
getAccessibleContext, getUI, getUIClassID, setUI, updateUI
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintImmediately, paintImmediately, print, printAll, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusBackward, transferFocusDownCycle, validate
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, contains, createImage, createImage, createVolatileImage, createVolatileImage, dispatchEvent, enable, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, 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
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Playground

public Playground()
Create a Playground inside the given TurtleContainer and with standard size (400 x 400 pixels) and default background color.


Playground

public Playground(java.awt.Color bkColor)
Create a Playground inside the given TurtleContainer and with standard size (400 x 400 pixels) and given background color.


Playground

public Playground(java.awt.Dimension size)
Create a new Playground inside the given TurtleContainer and size.


Playground

public Playground(java.awt.Dimension size,
                  java.awt.Color bkColor)
Create a new Playground inside the given TurtleContainer, size and background color.

Method Detail

getSize

public java.awt.Dimension getSize()
Return the size of the playground.

Overrides:
getSize in class java.awt.Component

add

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


remove

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


countTurtles

public int countTurtles()
Tell current number of Turtles in this Playground.


getTurtle

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


paintTurtles

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

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

paintTurtles

public void paintTurtles()
Paint all turtles (calling paintComponent())


paintTurtle

public void paintTurtle(Turtle turtle)
Paint the given Turtle. ( no repaint() )


fill

public void fill(Turtle t,
                 java.awt.Color bgColor)
Fill a region. The region is defined by the Turtles actual position and is bounded by any other color than the give background color.


clear

public void clear(java.awt.Color color)
Clear the playground with given color.


clear

public void clear()
Clear playground.


paintComponent

public void paintComponent()
Paint the Playground. just a method for convenience.


paintComponent

public void paintComponent(java.awt.Graphics g)
Draw the trace and turtle buffers.

Overrides:
paintComponent in class javax.swing.JComponent

clearTurtles

public void clearTurtles()
Remove all turtles from the turtle buffer.


clearTurtle

public void clearTurtle(Turtle turtle)
Remove the given turtle from the turtle buffer. Override this method if you have added a new behaviour (like wrap or clip) to the turtle.


toTop

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


toBottom

public Turtle toBottom(Turtle turtle)
Put a Turtle below all others.


toScreenCoords

public java.awt.geom.Point2D.Double toScreenCoords(java.awt.geom.Point2D.Double p)
Calculate the screen coordinates of the given point.


toScreenCoords

public java.awt.geom.Point2D.Double toScreenCoords(double x,
                                                   double y)
Calculate the screen coordinates of the given point coordinates.


toTurtleCoords

public java.awt.geom.Point2D.Double toTurtleCoords(double x,
                                                   double y)
Calculate the turtle coordinates of the given screen coordinates.


toTurtleCoords

public java.awt.geom.Point2D.Double toTurtleCoords(java.awt.geom.Point2D.Double p)
Calculate the turtle coordinates of the given screen point.


getTurtleG2D

public java.awt.Graphics2D getTurtleG2D()
Returnt the graphics context of the turtle buffer.


getTurtleBuffer

public java.awt.image.BufferedImage getTurtleBuffer()
Return the image of the turtle buffer.


getTraceG2D

public java.awt.Graphics2D getTraceG2D()
Return the graphics context of the trace buffer.


getPrinterG2D

public java.awt.Graphics2D getPrinterG2D()
Return the graphics context of the printer.


getTraceBuffer

public java.awt.image.BufferedImage getTraceBuffer()
Return the image of the trace buffer.


label

public void label(java.lang.String text,
                  Turtle t)
Draw 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.


setAntiAliasing

public void setAntiAliasing(boolean on)
Set antialiasing on or off for the turtle trace buffer This may result in an better trace quality.


print

public int print(java.awt.Graphics g,
                 java.awt.print.PageFormat pf,
                 int pageIndex)
For internal use only. Implementation of Printable. (Callback method called by printing system.)

Specified by:
print in interface java.awt.print.Printable

getPixelColor

public java.awt.Color getPixelColor(Turtle t)
Return the color of the pixel at the current turtle position.


enableRepaint

public void enableRepaint(boolean b)