public class Playground
extends javax.swing.JPanel
implements java.awt.print.Printable
Playground
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:
Playground
needs a Window (e.g. Frame
, Applet
)
to be displayed. One possible solution for this problem is already given with the TurtleFrame
class, which implements the TurtleContainer
interface. Applets are not yet supported.TOOL_TIP_TEXT_KEY, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
Constructor and Description |
---|
Playground()
Creates a Playground with standard size and default background color.
|
Playground(java.awt.Color bkColor)
Creates a Playground with standard size and given background color.
|
Playground(java.awt.Dimension size)
Creates a new Playground with given size.
|
Playground(java.awt.Dimension size,
java.awt.Color bkColor)
Creates a new Playground with given size and background color.
|
Modifier and Type | Method and Description |
---|---|
void |
add(Turtle turtle)
Adds a new
Turtle to the Playground. |
void |
clean()
Same as clean(Color color) with the current background color.
|
void |
clean(java.awt.Color color)
Cleans the traces using the given color.
|
void |
clear()
Same as clear(Color color) with the current background color.
|
void |
clear(java.awt.Color color)
Clears the playground with given color.
|
void |
clearTurtle(Turtle turtle)
Removes the given turtle from the turtle buffer.
|
void |
clearTurtles()
Remove all turtles from the turtle buffer.
|
int |
countTurtles()
Tells current number of
Turtle s in this Playground. |
boolean |
drawImage(java.awt.image.BufferedImage bi,
double x,
double y)
Draws the given buffered image into background. x,y is the center of the image
in turtle coordinates.
|
boolean |
drawImage(java.lang.String imagePath,
double x,
double y)
Draws the given image into background. x,y is the center of the image
in turtle coordinates.
|
void |
drawLine(double x0,
double y0,
double x1,
double y1,
Pen pen)
Draws a line from the point
(x0, y0) to (x1, y1)
with the color and width of the given Pen . |
void |
enableRepaint(boolean b)
Enables/disables automatic screen rendering.
|
void |
fill(Turtle t)
Fills a region with the
Turtle s fill color (flood fill). |
java.awt.Dimension |
getBufferSize()
Returns the size of the graphics buffer that holds the
drawing area
(number of pixels in horizontal direction and vertical direction).
|
java.awt.Color |
getPixelColor(Turtle t)
Returns the color of the pixel at the current turtle position.
|
java.awt.Graphics2D |
getPrinterG2D()
Returns the graphics context of the printer.
|
java.awt.Dimension |
getSize()
Returns the size (defines coordinate span) of the playground.
|
java.awt.image.BufferedImage |
getTraceBuffer()
Returns the image of the trace buffer.
|
java.awt.Graphics2D |
getTraceG2D()
Returns the graphics context of the trace buffer.
|
Turtle |
getTurtle(int index)
Returns the
Turtle at index index . |
java.awt.image.BufferedImage |
getTurtleBuffer()
Returns the image of the turtle buffer.
|
java.awt.Graphics2D |
getTurtleG2D()
Returns the graphics context of the turtle buffer.
|
void |
label(java.lang.String text,
Turtle t)
Same as label(String text, Turtle t, char align) with align = 'l'.
|
void |
label(java.lang.String text,
Turtle t,
char align)
Draws the
text at the current position of the Turtle t . |
void |
paintComponent()
Paints the playground.
|
void |
paintComponent(java.awt.Graphics g)
Draws the trace and turtle buffers.
|
void |
paintTurtle(Turtle turtle)
Paints the given
Turtle . |
void |
paintTurtles()
Paints all turtles (calling paintComponent())
|
void |
paintTurtles(Turtle turtle)
Moves 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)
Removes a
Turtle from the Playground. |
boolean |
save(java.lang.String fileName,
java.lang.String formatName)
Saves the playground (turtles and traces) in an image file.
|
void |
setAntiAliasing(boolean on)
Sets antialiasing on or off for the turtle trace buffer
This may result in an better trace quality.
|
void |
setBkColor(java.awt.Color color)
Sets the background color of the playground.
|
void |
setBkImage(java.lang.String imagePath)
Draws the given image into the background (trace buffer).
|
Turtle |
toBottom(Turtle turtle)
Puts a Turtle below all others.
|
java.awt.geom.Point2D.Double |
toScreenCoords(double x,
double y)
Calculates the screen coordinates of the given point coordinates.
|
java.awt.geom.Point2D.Double |
toScreenCoords(java.awt.geom.Point2D.Double p)
Calculates 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)
Calculates the turtle coordinates of the given screen coordinates.
|
java.awt.geom.Point2D.Double |
toTurtleCoords(java.awt.geom.Point2D.Double p)
Calculates the turtle coordinates of the given screen point.
|
getAccessibleContext, getUI, getUIClassID, setUI, updateUI
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
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, transferFocusDownCycle, validate
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, transferFocusBackward, transferFocusUpCycle
public Playground()
public Playground(java.awt.Color bkColor)
public Playground(java.awt.Dimension size)
public Playground(java.awt.Dimension size, java.awt.Color bkColor)
public java.awt.Dimension getSize()
getSize
in class java.awt.Component
public java.awt.Dimension getBufferSize()
public void add(Turtle turtle)
Turtle
to the Playground.public void remove(Turtle turtle)
Turtle
from the Playground.public int countTurtles()
Turtle
s in this Playground.public Turtle getTurtle(int index)
Turtle
at index index
.public void paintTurtles(Turtle turtle)
Turtle
above all the others, then
paints all turtles.toTop(ch.aplu.turtle.Turtle)
public void paintTurtles()
public void paintTurtle(Turtle turtle)
Turtle
.public boolean drawImage(java.lang.String imagePath, double x, double y)
public boolean drawImage(java.awt.image.BufferedImage bi, double x, double y)
public void drawLine(double x0, double y0, double x1, double y1, Pen pen)
(x0, y0)
to (x1, y1)
with the color and width of the given Pen
.public void fill(Turtle t)
Turtle
s fill color (flood fill).
The region is defined by the Turtle
s actual position and
is bounded by any other color than the pixel color at the turtle position.
If this pixel color is the same as the fill color, nothing is done. If the current
turtle position is outside the playground, nothing happens.public void clear(java.awt.Color color)
public void clear()
public void clean(java.awt.Color color)
public void clean()
public void setBkColor(java.awt.Color color)
public void setBkImage(java.lang.String imagePath)
imagePath
- the file name or urlpublic void paintComponent()
public void paintComponent(java.awt.Graphics g)
paintComponent
in class javax.swing.JComponent
public void clearTurtles()
public void clearTurtle(Turtle turtle)
public java.awt.geom.Point2D.Double toScreenCoords(java.awt.geom.Point2D.Double p)
public java.awt.geom.Point2D.Double toScreenCoords(double x, double y)
public java.awt.geom.Point2D.Double toTurtleCoords(double x, double y)
public java.awt.geom.Point2D.Double toTurtleCoords(java.awt.geom.Point2D.Double p)
public java.awt.Graphics2D getTurtleG2D()
public java.awt.image.BufferedImage getTurtleBuffer()
public java.awt.Graphics2D getTraceG2D()
public java.awt.Graphics2D getPrinterG2D()
public java.awt.image.BufferedImage getTraceBuffer()
public void label(java.lang.String text, Turtle t)
public void label(java.lang.String text, Turtle t, char align)
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.
Text alignment: align = 'l' left, 'c': center, 'r': rightpublic void setAntiAliasing(boolean on)
public int print(java.awt.Graphics g, java.awt.print.PageFormat pf, int pageIndex)
print
in interface java.awt.print.Printable
public java.awt.Color getPixelColor(Turtle t)
public void enableRepaint(boolean b)
public boolean save(java.lang.String fileName, java.lang.String formatName)
fileName
- the image file pathformatName
- the image format (supported values: "PNG", "GIF")