Question
Define a public class Coord which will hold one (x,y) coordinate, as a pair of int values. Its OK to make the instance variables for
Define a public class Coord which will hold one (x,y) coordinate, as a pair of int values. Its OK to make the instance variables for x and y public in a small class like this. Provide a constructor, which accepts the two int values (x and y in that order). Provide a toString method which returns a String in the format (143,319), mainly for testing and debugging.
B. Define a public class Path which will hold a list of Coords. Do this by making Path a subclass of ArrayList
a. Provide a constructor with one parameter of type PathWindow. In order to draw the path in a window, you will need to know which window it is, so save this reference to a window in an instance variable. The PathWindow class is described below.
b. This class should have only one instance method (for now): void plotPath() which will draw the path as shown on the right. To do this, you will need to draw circles and lines. Methods will be provided to do these things in the PathWindow class, which is described below. Dont forget that Path will also have a very large number of methods inherited from the ArrayList class.
C. Define a public class PathApp which will act as the main program for one particular window. This class will behave a lot like a Processing program, which will be familiar to you if you took COMP 1010. It should contain three methods:
a. A constructor with one parameter of type PathWindow. (This will be like setup() in Processing.) The PathApp will need to know which window it is controlling. Save this reference in an instance variable. In this phase, each window will contain only one Path, so you should create that new Path.
b. public void draw() will be called whenever the image in the window needs to be drawn. It should use plotPath() to draw your Path.
c. public void mouseClicked(int x, int y) will be called whenever the mouse is clicked in the window at location (x,y). In this phase, it should add that coordinate to the end of your Path.
d. Note that each of these two instance methods should contain only one line of code (for now).
D. A class PathWindow will be supplied to you. It will open three Java windows, and will test your classes. Just click in the windows to create paths like the one shown above. If you have written your classes correctly, all three windows should function independently. This class provides two methods that you can call to draw circles and lines (below). These are instance methods that must be applied to a PathWindow object, since the circles and lines will be drawn in one particular window. Thats why your classes need to keep track of which window theyre using.
a. circle(int x, int y, int diameter)
b. line(int x1, int y1, int x2, int y2)
This is the Pathwindow class below
import javax.swing.*; //Needed for windows import java.awt.*; //Needed for graphics import java.awt.event.*; //Needed for the mouse events
public class PathWindow extends JFrame { //JFrame means "window", in effect private static final long serialVersionUID = 1L; //eliminates warnings private JPanel wholeWindow; //A JPanel is the content within a window private PathApp myApp; //A reference to the PathApp handling this window. private Graphics myGraphics; //Saved reference to the graphics environment //Constructor public PathWindow(int wide, int high) { setTitle("Path app"); setSize(wide,high); wholeWindow = new GraphicsPanel(); //GraphicsPanel is defined below add(wholeWindow); wholeWindow.addMouseListener(new HandleMouse()); myApp = new PathApp(this); //Set up the user's app setVisible(true); }//PathWindow constructor public static void main(String[] args){ //Main program. All it has to do is create //some windows, and then take the rest of the day off. //The execution of the program takes place in separate //threads created by the windows. PathWindow window1 = new PathWindow(900,900); PathWindow window2 = new PathWindow(700,700); PathWindow window3 = new PathWindow(500,500); }//main //Provide some simple access to the graphics environment public void circle(int x, int y, int size){ myGraphics.fillOval(x-size/2,y-size/2,size,size); } public void line(int x1, int y1, int x2, int y2){ myGraphics.drawLine(x1,y1,x2,y2); } private class GraphicsPanel extends JPanel { private static final long serialVersionUID = 1L; public void paintComponent(Graphics g){ /* This is where all the drawing commands go. * Whenever the window needs to be drawn, or redrawn, * this method will automatically be called. */ myGraphics = g; //Just remember the Graphics environment myApp.draw(); //And then let the PathApp do what it wants }//paintComponent method }//private inner class graphicsPanel //A private inner class to take care of all mouse actions private class HandleMouse implements MouseListener { //The five standard methods are required. I don't want these ones: public void mousePressed(MouseEvent e){ /*Do nothing */ } public void mouseReleased(MouseEvent e){ /*Do nothing */ } public void mouseEntered(MouseEvent e){ /*Do nothing */ } public void mouseExited(MouseEvent e){ /*Do nothing */ } //The only one we really want to pay attention to public void mouseClicked(MouseEvent e){ myApp.mouseClicked(e.getX(),e.getY()); repaint(); //Redraw everything, since a change was probably made. }//mouseClicked }//private inner class HandleMouse }//class PathWindow
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started