Turtle_Solutions - import javax.swing import java.awt import java.awt.event An instance is a turtle in a panel within a JFrame The turtle is a spot

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Sheet1 Page 1 import javax.swing.* import java.awt.* import java.awt.event.* /** An instance is a turtle in a panel within a JFrame. The turtle is a spot on the panel, facing a certain direction and holding a colored pen. You can move the turtle from place to place, drawing as the turtle moves. You can ask the turtle to pick the pen up so that moving doesn't draw. You can draw rectangles and circles of any size around the turtle. All instances of Turtle use the same JFrame and panel. */ public class Turtle { /** The frame on which the turtles move. */ private static JFramePaint jframe /** The panel in the JFrame. */ private static JPanel panel /** The graphics associated with the panel. */ private static Graphics graphics /** The difference between frame and panel heights. */ private int difference /** The panel on which the turtle moves size (width, height). */ private static int width= 500 /** The panel on which the turtle moves has size (width, height). */ private static int height= 500 /** The list of commands to execute when repainting are in data[0. .iWrite-1] */ private static final int ARRAY_INCREMENT=256 private static PaintCommand[] data= new PaintCommand[256] private static int iWrite= 0 /** = "window has been resized and not yet repainted" */ private static boolean resizing= false /** The turtle is at point (x, y). */ private double x, y /** The turtle points in direction angle: 0 <= angle < 360. 0: east, 90:north, 180: west, 270 south. */ private double angle /** = "the pen is down". */ private boolean penDown= true /** the pen color. */ private Color turtleColor= Color.black
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Page 2 /** Constructor: a black turtle starting at the middle of the panel with angle 0 (looking east). East (right) is angle 0 South (down). 270. The pen is down.*/ public Turtle() { this(width/2, height/2, 0) } /** Constructor: a black turtle starting at (x,y) with angle ang. East (right) is angle 0 South (down), 270. The pen is down. */ public Turtle(double x, double y, double ang) { if (jframe == null) { panel= new JPanel() panel.setPreferredSize(new Dimension(width, height)) panel.setBackground(Color.white) jframe= new JFramePaint("Turtle. After resizing, move mouse into window to redraw.") jframe.getContentPane().add(panel, BorderLayout.CENTER) jframe.pack() jframe.setVisible(true) difference= jframe.getHeight() - panel.getHeight() graphics= panel.getGraphics() clear() jframe.addComponentListener(jframe) jframe.addMouseListener(new MouseListener() { public void mouseClicked (MouseEvent me) {} /** The mouse entered the JFrame. If it had been used to resize the JFrame, then its panel as big as possible and repaint it. */ public void mouseEntered (MouseEvent me) { mouseReleased(me) } public void mousePressed (MouseEvent me) {} /** The mouse has been released inside the JFrame. If it had been used to resize the JFrame, then its panel as big as possible and repaint it. */ public void mouseReleased (MouseEvent me) { if(!resizing) { return } // The JFrame has been resized. So make the panel as big
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This homework help was uploaded on 02/20/2009 for the course CS 100 taught by Professor Fan/vanloan during the Spring '07 term at Cornell University (Engineering School).

Page1 / 26

Turtle_Solutions - import javax.swing import java.awt import java.awt.event An instance is a turtle in a panel within a JFrame The turtle is a spot

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online