Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The cursor starts at (150, 150) of a 300 by 300 window. The first 2 pieces of da

ID: 3656358 • Letter: T

Question

The cursor starts at (150, 150) of a 300 by 300 window.

The first 2 pieces of data, 4 100, say move backward (down) 100 pixels. This draws the line segment from the center of the window down almost to the bottom of the window. The next line of data, 3 50, draws a short segment to the right.

Step 1. Write an application, TurtleGraphics, that opens a file mydata.txt, reads the numbers into an array called moves, the prints the values to the screen, just to make sure everything is working correctly. You may use the mydata.txt file above. Declare moves to be big enough to hold 100 ints.

Step 2. Create a class, TurtlePane, for eventually drawing the picture. It should extend JPanel. Add an array, also called moves, as a field of the class. Add a method, setMoves(int[] m) that can be used to set moves of the class TurtlePane to be equal to the array moves of the class TurtleGraphics.

As we usually do for graphics, have TurtleGraphics create a JFrame and a TurtlePane, add the TurtlePane to the JFrame, set the exit,...

In the paintComponent method of TurtlePane, after calling super.paintComponent, print the array moves, just to test all is working. Nothing is being drawn yet.

Step 3. Drawing.

Set the initial values of variables currentX and currentY both to 150, the initial point for

our drawing. Notice that in the array moves, after each instruction 1,2,3, or 4, there is exactly one integer, the number of pixels to be drawn. Write a switch statement that will look at the value of move[0] to decide if you will be drawing a line segment forward, to the left, to the right, or down. Using the value of move[1] , draw a line segment that length in the correct direction. Update the value of currentX and currentY to correspond to this new endpoint.

Repeat this process for the pair of points move[2] and move[3] and continue until a move instruction of 0 is reached. You are done!

Once your program is working, try it with some other files. Here is a file that should result in an H.

Explanation / Answer

package com.cramster.nov16; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; import javax.swing.JFrame; public class TurtleGraphics { private static int[] moves = new int[100]; public static void main(String[] args) { readFile(); TurtlePanel panel = new TurtlePanel(); panel.setMoves(moves); JFrame application = new JFrame(); application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); application.add(panel); application.setSize(300,300); application.setVisible(true); } public static void readFile() { try { Scanner input = new Scanner(new File("mydata.txt")); int count = 0; while(input.hasNext()) { moves[count++] = input.nextInt(); } input.close(); } catch (FileNotFoundException e) { System.err.println("Error! File not found. Terminatig Program"); System.exit(1); } } } package com.cramster.nov16; import java.awt.Graphics; import javax.swing.JPanel; @SuppressWarnings("serial") public class TurtlePanel extends JPanel { private int[] moves; public void setMoves(int[] m) { moves = m; } public void paintComponent(Graphics g) { super.paintComponent(g); int currentX = 150; int currentY = 150; int count = 0; while(moves[count] != 0 && count
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote