{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Turtle_Solutions

Turtle_Solutions - import javax.swing import java.awt...

Info icon This 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
Image of page 1

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

View Full Document Right Arrow Icon
Sheet1 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 // as possible within the JFrame, whiten the panel, and // repaint the panel.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern