- java.lang.Object
-
- java.awt.Component
-
- java.awt.Container
-
- javax.swing.JComponent
-
- javax.swing.JPanel
-
- uk.ac.leedsbeckett.oop.TurtleGraphics
-
- All Implemented Interfaces:
- java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable, javax.accessibility.Accessible
public class TurtleGraphics extends javax.swing.JPanel
extended JPanel with simple drawing commands and a visual representation of a turtle to perform "turtle graphics" drawing operations. the jar file should be added to your build path. right click on your project, select "Build Path-Add External Archive" and add jar file. It will appear in your project explorer under "referenced libraries", inside the jar will be TurtleGraphics.class Don't forget to look at the inherited methods from JPanel and above, which will also be if use.- Since:
- 12/2018
- Version:
- 1.5 Mar 2020
All software has bugs, if you find one please report to author. Ensure you have the latest version
V1.3 adds setPanelSize() to resize the turtle area
V1.4 adds getBufferedImage()
*V1.5 adds setBufferdImage()
V1.6 adds reset() to make resetting the turtle very, very easy.
package whateverMyPackageIs;
import uk.ac.leedsbeckett.oop.TurtleGraphics;
import javax.swing.JFrame;
public class MainClass {
public static void main(String[] args) { TurtleGraphics gp = new TurtleGraphics(); JFrame MainFrame = new JFrame(); MainFrame.add(gp); MainFrame.setSize(640, 480); MainFrame.setVisible(true);; gp.about(); }
}
- Author:
- Duncan Mullier
- See Also:
- Serialized Form
-
-
Fields Modifier and Type Field and Description protected int
direction
direction the turtle is pointing in in degreesprotected java.awt.Color
PenColour
Colour of the pen the turtle draws with (A Java Color)protected boolean
penDown
a moving turtle will draw if this is true and not if it is false (set by penDown and PenUp methods)protected int
sleepPeriod
delay for turtle animationprotected int
xPos
x position of the turtle on the screenprotected int
yPos
y position of the turtle on the screen-
listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
-
-
Constructors Constructor and Description TurtleGraphics()
Constructor.
-
All Methods Instance Methods Concrete Methods Modifier and Type Method and Description void
about()
draws a simple graphic on the canvas and reports the version number of this classvoid
circle(int radius)
unimplemented circle commandvoid
clear()
Clears the image contents with the current background colour.void
drawLine(java.awt.Color color, int x1, int y1, int x2, int y2)
draws a line directly on the panel without affecting the turtlevoid
forward(int distance)
move the turtle (in the direction it is pointing) by {distance} pixels.java.awt.Color
getBackground_Col()
gets the background colour used by clear() to fill the panel.java.awt.image.BufferedImage
getBufferedImage()
return a BufferedImage of he display, so that it can be savedint
getDirection()
getDirection gets the direction the turtle is pointing in.java.awt.Graphics
getGraphicsConext()
returns the graphicsContext of the Turtle display so you can drw on it using the normal Java drawing methodsjava.awt.Graphics
getGraphicsContext()
returns the graphicsContext (same as above but spelt correctly) of the Turtle display so you can drw on it using the normal Java drawing methodsjava.awt.Color
getPenColour()
getPenColour returns the colour that the turtle draws inint
getxPos()
get the x position of the turtleint
getyPos()
get the y position of the turtlevoid
paint(java.awt.Graphics g)
overridden paint method to handle image updating (do not call directly, use repaint();)void
penDown()
puts pen down so a line will be drawn when the turtle is movedvoid
penUp()
puts pen up so a line will not be drawn when turtle is movedvoid
reset()
reset() moves turtle to initial positionvoid
setBackground_Col(java.awt.Color background_Col)
sets the background colour used by clear() to fill the panel.void
setBufferedImage(java.awt.image.BufferedImage newImage)
sets the background image to be the passed in BufferedImagevoid
setPanelSize(int xSize, int ySize)
Resize the turtle area, current display will be lost.void
setPenColour(java.awt.Color col)
setPenColour sets the colour that the turtle will draw invoid
setPreferredSize(int width, int height)
void
setTurtleImage(java.lang.String filename)
Replace the standard turtle image with given image give full path or store in application directory don't make the image too big, have background of the image transparent and it should be pointing right (90 degrees) best to make the image have a transparent background(Google it).void
setTurtleSpeed(int speed)
sets the speed of the turtle's movement.void
setxPos(int xPos)
manually set the x position of the turtle (i.e.void
setyPos(int yPos)
manually set the y position of the turtle (i.e.void
turnLeft()
turtle is rotated 90 degrees to the left.void
turnLeft(int amount)
turtle is rotated 90 degrees to the right by amount degrees.void
turnRight()
turtle is rotated 90 degrees to the right.void
turnRight(int amount)
turtle is rotated 90 degrees to the right by amount degrees.-
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, paintBorder, paintChildren, paintComponent, 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, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
-
-
-
-
-
protected java.awt.Color PenColour
Colour of the pen the turtle draws with (A Java Color)
-
protected boolean penDown
a moving turtle will draw if this is true and not if it is false (set by penDown and PenUp methods)
-
protected int xPos
x position of the turtle on the screen
-
protected int yPos
y position of the turtle on the screen
-
protected int direction
direction the turtle is pointing in in degrees
-
protected int sleepPeriod
delay for turtle animation
-
-
-
public TurtleGraphics()
Constructor. Create a panel with pen set to the middle and turtle pointing down the screen The pen is up.
-
-
-
public java.awt.Graphics getGraphicsConext()
returns the graphicsContext of the Turtle display so you can drw on it using the normal Java drawing methods- Returns:
- graphics context
-
public java.awt.Graphics getGraphicsContext()
returns the graphicsContext (same as above but spelt correctly) of the Turtle display so you can drw on it using the normal Java drawing methods- Returns:
- graphics context
-
public java.awt.image.BufferedImage getBufferedImage()
return a BufferedImage of he display, so that it can be saved- Returns:
- BufferedImage of display
-
public void setBufferedImage(java.awt.image.BufferedImage newImage)
sets the background image to be the passed in BufferedImage- Parameters:
newImage
- saved BufferedImage
-
public java.awt.Color getPenColour()
getPenColour returns the colour that the turtle draws in- Returns:
- Color pencolour
-
public void setPenColour(java.awt.Color col)
setPenColour sets the colour that the turtle will draw in- Parameters:
col
- Java Color
-
public int getDirection()
getDirection gets the direction the turtle is pointing in.- Returns:
- direction in degrees
-
public void drawLine(java.awt.Color color, int x1, int y1, int x2, int y2)
draws a line directly on the panel without affecting the turtle- Parameters:
color
- colour to draw inx1
- xpos of start of liney1
- ypos of start of linex2
- xpos of end of liney2
- ypos of end of line
-
public java.awt.Color getBackground_Col()
gets the background colour used by clear() to fill the panel.- Returns:
- the background_Col used when clear() is called.
-
public void setBackground_Col(java.awt.Color background_Col)
sets the background colour used by clear() to fill the panel.- Parameters:
background_Col
- the background_Col to set (used when clear() is called.).
-
public int getxPos()
get the x position of the turtle- Returns:
- the xPos
-
public void setxPos(int xPos)
manually set the x position of the turtle (i.e. lift the turtle up and drop it).- Parameters:
xPos
- the xPos to set
-
public int getyPos()
get the y position of the turtle- Returns:
- the yPos
-
public void setyPos(int yPos)
manually set the y position of the turtle (i.e. lift the turtle up and drop it).- Parameters:
yPos
- the yPos to set
-
public void about()
draws a simple graphic on the canvas and reports the version number of this class
-
public void setTurtleSpeed(int speed)
sets the speed of the turtle's movement.- Parameters:
speed
- 0 is fastest 1 for each microsecond of delay
-
public void penDown()
puts pen down so a line will be drawn when the turtle is moved
-
public void penUp()
puts pen up so a line will not be drawn when turtle is moved
-
public void turnRight()
turtle is rotated 90 degrees to the right. i.e. if it is facing upwards (north) before it will facing right (east) after
-
public void turnRight(int amount)
turtle is rotated 90 degrees to the right by amount degrees. i.e. it will rotate right by amount degrees The turtle will wrap around if it goes beyond 360- Parameters:
amount
- degrees to rotate
-
public void turnLeft()
turtle is rotated 90 degrees to the left. i.e. if it is facing upwards (north) before it will facing left (west) after
-
public void turnLeft(int amount)
turtle is rotated 90 degrees to the right by amount degrees. i.e. it will rotate right by amount degrees The turtle will wrap around if it goes beyond 360- Parameters:
amount
- degrees to rotate
-
public void forward(int distance)
move the turtle (in the direction it is pointing) by {distance} pixels. A line will be drawn if the pen is down, not if it is up- Parameters:
distance
- in pixels to move
-
public void clear()
Clears the image contents with the current background colour.
-
public void circle(int radius)
unimplemented circle command- Parameters:
radius
- radius of the circle to draw
-
public void paint(java.awt.Graphics g)
overridden paint method to handle image updating (do not call directly, use repaint();)- Overrides:
paint
in classjavax.swing.JComponent
-
public void setPreferredSize(int width, int height)
-
public void setTurtleImage(java.lang.String filename)
Replace the standard turtle image with given image give full path or store in application directory don't make the image too big, have background of the image transparent and it should be pointing right (90 degrees) best to make the image have a transparent background(Google it).- Parameters:
filename
- file or path to save the to
-
public void setPanelSize(int xSize, int ySize)
Resize the turtle area, current display will be lost.- Parameters:
xSize
- width of panelySize
- height of panel
-
public void reset()
reset() moves turtle to initial position
-
-