public class Terrarium extends JPanel
A Turtle
lives (and draws) inside a Terrarium
.
A terrarium can be resized using the setSize(int, int)
method and repositioned on the screen using the
setPosition(int, int)
. The terrarium can also have a custom background color (set via the
JComponent.setBackground(Color)
method — it defaults to white.
Initially, there is only a single terrarium, into which all new turtles are added. However, it is possible to
instantiate additional terraria, and to direct Turtles to them using the Turtle.setTerrarium(Terrarium)
method. From a technical standpoint, the initial terrarium is a quasi-singleton, and will continue to be treated as
a singleton by any new turtles as they are instantiated. The singleton terrarium instance can be accessed statically
via the getInstance()
method. When additional terraria have been instantiated, they may also be accessed
statically via their index (in instantiation order) using the getInstance(int)
method.
Modifier and Type | Class and Description |
---|---|
static class |
Terrarium.UnderTheSurface
The parts of the terrarium that are "under the surface" are not meant to be used by students.
|
JPanel.AccessibleJPanel
JComponent.AccessibleJComponent
Container.AccessibleAWTContainer
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
Modifier and Type | Field and Description |
---|---|
static Color |
DEFAULT_BACKGROUND
|
static int |
DEFAULT_HEIGHT
400 pixels
|
static int |
DEFAULT_WIDTH
600 pixels
|
protected static Terrarium.UnderTheSurface |
UNDER_THE_SURFACE
|
listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
Constructor and Description |
---|
Terrarium()
Construct a new terrarium of default dimensions, centered on the screen in its own window
|
Modifier and Type | Method and Description |
---|---|
void |
add(Track track,
Turtle.UnderTheShell key)
Adds a new turtle track to the terrarium
|
void |
add(Turtle turtle,
Turtle.UnderTheShell key)
Adds a new turtle to the terrarium
|
void |
clear()
Clear all turtle tracks from the terrarium
|
protected void |
draw(Graphics2D context)
For synchronous drawing requests (e.g.
|
static Terrarium |
getInstance()
Get the default terrarium instance (instantiating it, if necessary)
|
static Terrarium |
getInstance(int index)
Get a particular Terrarium instance
|
void |
paintComponent(Graphics context)
Repaint the contents of the terrarium (tracks and turtles) as-needed
|
void |
remove(Turtle turtle,
Turtle.UnderTheShell key)
Remove a turtle from the terrarium
|
void |
setPosition(int x,
int y)
Adjust the location of the terrarium window.
|
void |
setSize(int width,
int height)
Adjust the dimensions of the terrarium view
|
getAccessibleContext, getUI, getUIClassID, paramString, setUI, updateUI
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, 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, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingOrigin, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, 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, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
add, add, add, add, add, addContainerListener, addImpl, 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, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusDownCycle, validate, validateTree
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, firePropertyChange, 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, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, 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, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, 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, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
protected static final Terrarium.UnderTheSurface UNDER_THE_SURFACE
public static final int DEFAULT_WIDTH
public static final int DEFAULT_HEIGHT
public static final Color DEFAULT_BACKGROUND
public Terrarium()
public static Terrarium getInstance()
public static Terrarium getInstance(int index)
index
- [0..n) if there are n terraria, sequenced by instantiation
orderpublic void add(Track track, Turtle.UnderTheShell key)
Adds a new turtle track to the terrarium
May only be called by Turtle
and its subclasses, enforced via Turtle.UnderTheShell
.
track
- to be addedkey
- to authenticate "Turtleness"public void clear()
public void add(Turtle turtle, Turtle.UnderTheShell key)
Adds a new turtle to the terrarium
May only be called by Turtle
and its subclasses, enforced via Turtle.UnderTheShell
.
turtle
- to be addedkey
- to authenticate "Turtleness"public void remove(Turtle turtle, Turtle.UnderTheShell key)
Remove a turtle from the terrarium
May only be called by Turtle
and its subclasses, enforced via Turtle.UnderTheShell
.
turtle
- to be removedkey
- to authenticate "Turtleness"public void setSize(int width, int height)
public void setPosition(int x, int y)
x
- coordinatey
- coordinatepublic void paintComponent(Graphics context)
Repaint the contents of the terrarium (tracks and turtles) as-needed
This method is called automatically by the enclosing JFrame
to repaint the terrarium as-needed. It is
not meant to be called at will. If the terrarium needs to be updated, a Component.repaint()
request will schedule
the update.
paintComponent
in class JComponent
context
- for drawing commandsprotected void draw(Graphics2D context)
context
- for drawing commandsCopyright © 2018. All rights reserved.