Class TurtleBox

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by java.awt.Window
              extended by java.awt.Frame
                  extended by javax.swing.JFrame
                      extended by TurtleBox
All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, Accessible, RootPaneContainer, WindowConstants

public class TurtleBox
extends JFrame

TurtleBox is a small class which implements a kind of "Logo".

See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class javax.swing.JFrame
JFrame.AccessibleJFrame
 
Nested classes/interfaces inherited from class java.awt.Frame
Frame.AccessibleAWTFrame
 
Nested classes/interfaces inherited from class java.awt.Window
Window.AccessibleAWTWindow
 
Nested classes/interfaces inherited from class java.awt.Container
Container.AccessibleAWTContainer
 
Nested classes/interfaces inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
 
Field Summary
protected  double angle
           
protected  Vector<Vector<Integer>> elements
          This data structure contains the different elements to move.
private static int LINE
           
private static int MOVE
           
protected  boolean penIsDown
           
protected  int speed
           
protected  JPanel surface
           
protected  boolean turtleHidden
           
protected  Point2D.Double turtlePos
           
 
Fields inherited from class javax.swing.JFrame
accessibleContext, EXIT_ON_CLOSE, rootPane, rootPaneCheckingEnabled
 
Fields inherited from class java.awt.Frame
CROSSHAIR_CURSOR, DEFAULT_CURSOR, E_RESIZE_CURSOR, HAND_CURSOR, ICONIFIED, MAXIMIZED_BOTH, MAXIMIZED_HORIZ, MAXIMIZED_VERT, MOVE_CURSOR, N_RESIZE_CURSOR, NE_RESIZE_CURSOR, NORMAL, NW_RESIZE_CURSOR, S_RESIZE_CURSOR, SE_RESIZE_CURSOR, SW_RESIZE_CURSOR, TEXT_CURSOR, W_RESIZE_CURSOR, WAIT_CURSOR
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface javax.swing.WindowConstants
DISPOSE_ON_CLOSE, DO_NOTHING_ON_CLOSE, HIDE_ON_CLOSE
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
TurtleBox(int width, int height)
          Constructs a new TurtleBox
 
Method Summary
private  void addElement(int kind, double x1, double y1, double x2, double y2)
          adds an element to the list
 void backward(double pixels)
          Moves the turtle by some pixels backwards
 void clear()
          Clear drawing surface (turtle remains at the same position)
private  void drawThickLine(Graphics g, double x1, double y1, double x2, double y2, int thickness, Color c)
          Method for drawing a thick line in Java.
private  void drawTurtle(Graphics2D graphics, Color innerColor, Color outerColor)
          Draws the turtle with given colors
 void forward(double pixels)
          Forward the turtle by some pixels
private  double getAngle()
          Get the actual angle of the turtle (in degrees)
private  void gotoXY(double x, double y)
          Positions the turtle to a given place
 void hideTurtle()
          Hides the turtle
 void home()
          Set turtle back to home position and orientation
 void left(double angle)
          Turns the turle left
static void main(String[] args)
          The main entry point for executing this program.
 void paint(Graphics graphics)
          Paints the box with stored elements
 void penDown()
          Takes the pen down (draw while moving)
 void penUp()
          Takes the pen up (move without drawing)
 void right(double angle)
          Turns the turle right
private  void setAngle(double angle)
          Sets a new angle for the turtle (in degrees)
 void setSpeed(int pSpeed)
          Sets the drawing speed to a value between 0 and 100
 void showTurtle()
          Shows the turtle
 
Methods inherited from class javax.swing.JFrame
addImpl, createRootPane, frameInit, getAccessibleContext, getContentPane, getDefaultCloseOperation, getGlassPane, getGraphics, getJMenuBar, getLayeredPane, getRootPane, getTransferHandler, isDefaultLookAndFeelDecorated, isRootPaneCheckingEnabled, paramString, processWindowEvent, remove, repaint, setContentPane, setDefaultCloseOperation, setDefaultLookAndFeelDecorated, setGlassPane, setIconImage, setJMenuBar, setLayeredPane, setLayout, setRootPane, setRootPaneCheckingEnabled, setTransferHandler, update
 
Methods inherited from class java.awt.Frame
addNotify, getCursorType, getExtendedState, getFrames, getIconImage, getMaximizedBounds, getMenuBar, getState, getTitle, isResizable, isUndecorated, remove, removeNotify, setCursor, setExtendedState, setMaximizedBounds, setMenuBar, setResizable, setState, setTitle, setUndecorated
 
Methods inherited from class java.awt.Window
addPropertyChangeListener, addPropertyChangeListener, addWindowFocusListener, addWindowListener, addWindowStateListener, applyResourceBundle, applyResourceBundle, createBufferStrategy, createBufferStrategy, dispose, getBufferStrategy, getFocusableWindowState, getFocusCycleRootAncestor, getFocusOwner, getFocusTraversalKeys, getGraphicsConfiguration, getIconImages, getInputContext, getListeners, getLocale, getModalExclusionType, getMostRecentFocusOwner, getOwnedWindows, getOwner, getOwnerlessWindows, getToolkit, getWarningString, getWindowFocusListeners, getWindowListeners, getWindows, getWindowStateListeners, hide, isActive, isAlwaysOnTop, isAlwaysOnTopSupported, isFocusableWindow, isFocusCycleRoot, isFocused, isLocationByPlatform, isShowing, pack, postEvent, processEvent, processWindowFocusEvent, processWindowStateEvent, removeWindowFocusListener, removeWindowListener, removeWindowStateListener, reshape, setAlwaysOnTop, setBounds, setBounds, setCursor, setFocusableWindowState, setFocusCycleRoot, setIconImages, setLocationByPlatform, setLocationRelativeTo, setMinimumSize, setModalExclusionType, setSize, setSize, setVisible, show, toBack, toFront
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getAlignmentX, getAlignmentY, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalPolicy, getInsets, getLayout, getMaximumSize, getMinimumSize, getMousePosition, getPreferredSize, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, print, printComponents, processContainerEvent, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusTraversalKeys, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setFont, 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, contains, createImage, createImage, createVolatileImage, createVolatileImage, disable, disableEvents, dispatchEvent, enable, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBaseline, getBaselineResizeBehavior, getBounds, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocation, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isPreferredSizeSet, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, prepareImage, prepareImage, printAll, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resize, resize, setBackground, setComponentOrientation, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeysEnabled, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setName, setPreferredSize, show, size, toString, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.awt.MenuContainer
getFont, postEvent
 

Field Detail

LINE

private static final int LINE
See Also:
Constant Field Values

MOVE

private static final int MOVE
See Also:
Constant Field Values

surface

protected JPanel surface

turtlePos

protected Point2D.Double turtlePos

turtleHidden

protected boolean turtleHidden

penIsDown

protected boolean penIsDown

angle

protected double angle

speed

protected int speed

elements

protected Vector<Vector<Integer>> elements
This data structure contains the different elements to move. Let's admit, that this approach IS NOT object oriented, but the goal was to achive a TurtleBox in a single file without using inner classes.

The inner vector contains alway 5 elements: the type of the element as well as the starting and ending coordinates.

Constructor Detail

TurtleBox

public TurtleBox(int width,
                 int height)
Constructs a new TurtleBox

Parameters:
width - the width of the box
height - the height of the box
Method Detail

drawThickLine

private void drawThickLine(Graphics g,
                           double x1,
                           double y1,
                           double x2,
                           double y2,
                           int thickness,
                           Color c)
Method for drawing a thick line in Java.
http://www.rgagnon.com/javadetails/java-0260.html


drawTurtle

private void drawTurtle(Graphics2D graphics,
                        Color innerColor,
                        Color outerColor)
Draws the turtle with given colors


paint

public void paint(Graphics graphics)
Paints the box with stored elements

Overrides:
paint in class Container

getAngle

private double getAngle()
Get the actual angle of the turtle (in degrees)


setAngle

private void setAngle(double angle)
Sets a new angle for the turtle (in degrees)

Parameters:
angle - the angle given in degree

setSpeed

public void setSpeed(int pSpeed)
Sets the drawing speed to a value between 0 and 100

Parameters:
pSpeed - the new speed value

left

public void left(double angle)
Turns the turle left

Parameters:
angle - the angle to turn left

right

public void right(double angle)
Turns the turle right

Parameters:
angle - the angle to turn right

hideTurtle

public void hideTurtle()
Hides the turtle


showTurtle

public void showTurtle()
Shows the turtle


penUp

public void penUp()
Takes the pen up (move without drawing)


penDown

public void penDown()
Takes the pen down (draw while moving)


addElement

private void addElement(int kind,
                        double x1,
                        double y1,
                        double x2,
                        double y2)
adds an element to the list

Parameters:
kind - MOVE or LINE
x1,y1,x2,y2 - coords of new element

forward

public void forward(double pixels)
Forward the turtle by some pixels

Parameters:
pixels - the number of pixels the turtle has to move

backward

public void backward(double pixels)
Moves the turtle by some pixels backwards

Parameters:
pixels - the number of pixels the turtle has to move backwards

gotoXY

private void gotoXY(double x,
                    double y)
Positions the turtle to a given place

Parameters:
x - the X coordinate
y - the Y coordinate

clear

public void clear()
Clear drawing surface (turtle remains at the same position)


home

public void home()
Set turtle back to home position and orientation


main

public static void main(String[] args)
The main entry point for executing this program.