----------------------------------------------------------
Coord class :
public class Coord { //variables int x; int y;
//parameterized constructor public Coord(int x, int y) { this.x = x; this.y = y; }
//returns a string containing coordinates public String toString() { return "(" + x + "," + y + ")"; }
}//end of Coord.java
-------------------------------------------------------------
Path class :
import java.util.ArrayList;
public class Path extends ArrayList { PathWindow window; static final int DIAMETER=10;//diameter of the circle public Path(PathWindow window) { this.window=window;//initializing the window } //*method to draw the path public void plotPath(){ int size=size();//getting the current size of the list Coord current=null,prev = null;//Defining two variables for storing current and previous coords
//looping through all coords for(int i=0;i }//end of Path.java
------------------------------------------------------------------
PathApp class :
public class PathApp { PathWindow window; Path path; public PathApp(PathWindow pathWindow) { this.window=pathWindow;//storing PathWindow reference for future use path=new Path(window);//initializing an empty path } //method to draw the Path public void draw() { path.plotPath();//drawing the current path }
//method to create a coord and add to the path public void mouseClicked(int x, int y) { Coord coord=new Coord(x, y);//creating a coord path.add(coord);//adding to path }
}//end of PathApp.java
--------------------------------------------------------------------
PathWindow class :
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 //The font used for the buttons, and how far from the left and bottom //of each button the characters will be drawn. private static final Font myFont = new Font("Helvetica",Font.BOLD,24); private static final int LABEL_INSET_X = 10; private static final int LABEL_INSET_Y = 15; //The size and positions and labels of each button, and the //public constants that will be used to identify them private static final int BUTTON_HEIGHTS = 50; private static final String[] LABELS = {"Extend","Delete","Insert","Select","Move"}; public static final int EXTEND = 0, DELETE = 1, INSERT = 2, SELECT = 3, MOVE = 4; //The x coordinates of the dividing lines between buttons private static final int[] DIVIDERS = {0,95,190,285,380,475};
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 private int mode; //The current mode (which button is selected) private int width, height; //The current window size (may change any time) //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()); mode = EXTEND; //Always begin in EXTEND mode 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, boolean selected){ if(selected) myGraphics.setColor(Color.RED); else myGraphics.setColor(Color.BLACK); myGraphics.fillOval(x-size/2,y-size/2,size,size); } public void line(int x1, int y1, int x2, int y2){ myGraphics.setColor(Color.BLACK); myGraphics.drawLine(x1,y1,x2,y2); } //public access to the current mode public int getMode() { return mode; } //A static utility to determine if the point (x,y) is within //a distance of maxDist from the line segment connecting //the points (x1,y1) and (x2,y2). public static boolean pointNearLine(int x, int y, //the point int x1, int y1, int x2, int y2, //the line double maxDist){//the maximum distance allowable //1. Find the point on the line closest to (x,y) double closestX,closestY; if(x1==x2){ //vertical line. Easy. closestX=x1; closestY=y; } else{ //Find the line's equation in the form ax+by+c=0 double a=y1-y2; double b=x2-x1; double c=x1*y2-y1*x2; //Then the formulae are: closestX=(b*(b*x-a*y)-a*c)/(a*a+b*b); closestY=(a*(a*y-b*x)-b*c)/(a*a+b*b); } //2. Find the distance from (x,y) to the closest point double dist = Math.hypot(x-closestX,y-closestY); //3. It must be very close AND between the endpoints return dist=Math.min(x1,x2) && closestX=Math.min(y1,y2) && closestYheight-BUTTON_HEIGHTS){ for(int i=0; i Phase 2: Operations on Paths Add four more operations to your application. There will be a set of five buttons drawn along the bottom of the window, as shown. The supplied PathWindow class will manage the buttons for you. Exactly one button will be selected at any time, and that button will be drawn in a darker gray, as shown. This will control the mode of the program. . The public instance method int getMode) n the PathWindow class will tell you which mode is currently selected. There are five public class (static) constants in the PathWindow class: EXTEND, DELETE, INSERT, SELECT, and MOVE. The . getMode() function will return one of these 5 values. The operation of the program will be different in each of the . five modes, as specified below Extend Delete Insert Select Move EXTEND mode: This is exactly as it was in Phase 1. When the mouse is clicked, a new Coord should be added to the end of the path DELETE mode: When the mouse is clicked very close to a Coord in the path, that Coord should be deleted from the path. (A line will now join the two Coords on either side of it.) Any click that is not close to a Coord in the path should be ignored. Add a boolean near(Coord) instance method to your Coord class which will return true when two Coords are very close together. Define your own constant to control how close is "close enough INSERT mode: When the mouse is clicked anywhere close to a line in the path, a new Coord should be inserted at that point in the path (between the two Coords that are the endpoints of that line). Determining if a point is close to a line can be rather tricky, so a public static boolean pointNearLine(x,y,x1,y1,x2,y2,max) function is supplied in the PathWindow class. It will return true if the point (x,y) is within a distance of max pixels of the line joining (x1,y1) and (x2,y2). All parameters are type int, except for max which is double. Define your own constant to control how close is "close enough" SELECT mode: Clicking on a Coord in the path should make that Coord the selected one. There can only be one selected Coord, and at times there may be no selected Coord at all. Add an instance variable to your Path class to keep track of which Coord is selected, if any. When the mouse is clicked on a non-selected Coord, then it should become the selected one. When the mouse is clicked on the selected Coord, then it should be un-selected (and now there will be no selected Coord.) In the Phase 2 version of the PathWindow class, the circle method has been given an extra parameter: circle(int x, int y, int size, boolean selected). If the last parameter is true, the circle will be drawn in red. Modify your Path class to draw the path with the selected Coord in red MOVE mode: When the mouse is clicked, the selected Coord should be moved to the location of the mouse click. It should remain selected. If there is no selected Coord, then nothing should happen. (The SELECT and MOVE modes work together.) Modify your Coord, Path, and PathApp classes so that the program performs as described above. Add appropriate methods of your own choosing to these classes. A file PathWindowPhase2 is supplied but you will have to rename it PathWindow before compiling it and running it. There will be separate PathWindow files for Phases 1, 2, and 3. Phase 2: Operations on Paths Add four more operations to your application. There will be a set of five buttons drawn along the bottom of the window, as shown. The supplied PathWindow class will manage the buttons for you. Exactly one button will be selected at any time, and that button will be drawn in a darker gray, as shown. This will control the mode of the program. . The public instance method int getMode) n the PathWindow class will tell you which mode is currently selected. There are five public class (static) constants in the PathWindow class: EXTEND, DELETE, INSERT, SELECT, and MOVE. The . getMode() function will return one of these 5 values. The operation of the program will be different in each of the . five modes, as specified below Extend Delete Insert Select Move EXTEND mode: This is exactly as it was in Phase 1. When the mouse is clicked, a new Coord should be added to the end of the path DELETE mode: When the mouse is clicked very close to a Coord in the path, that Coord should be deleted from the path. (A line will now join the two Coords on either side of it.) Any click that is not close to a Coord in the path should be ignored. Add a boolean near(Coord) instance method to your Coord class which will return true when two Coords are very close together. Define your own constant to control how close is "close enough INSERT mode: When the mouse is clicked anywhere close to a line in the path, a new Coord should be inserted at that point in the path (between the two Coords that are the endpoints of that line). Determining if a point is close to a line can be rather tricky, so a public static boolean pointNearLine(x,y,x1,y1,x2,y2,max) function is supplied in the PathWindow class. It will return true if the point (x,y) is within a distance of max pixels of the line joining (x1,y1) and (x2,y2). All parameters are type int, except for max which is double. Define your own constant to control how close is "close enough" SELECT mode: Clicking on a Coord in the path should make that Coord the selected one. There can only be one selected Coord, and at times there may be no selected Coord at all. Add an instance variable to your Path class to keep track of which Coord is selected, if any. When the mouse is clicked on a non-selected Coord, then it should become the selected one. When the mouse is clicked on the selected Coord, then it should be un-selected (and now there will be no selected Coord.) In the Phase 2 version of the PathWindow class, the circle method has been given an extra parameter: circle(int x, int y, int size, boolean selected). If the last parameter is true, the circle will be drawn in red. Modify your Path class to draw the path with the selected Coord in red MOVE mode: When the mouse is clicked, the selected Coord should be moved to the location of the mouse click. It should remain selected. If there is no selected Coord, then nothing should happen. (The SELECT and MOVE modes work together.) Modify your Coord, Path, and PathApp classes so that the program performs as described above. Add appropriate methods of your own choosing to these classes. A file PathWindowPhase2 is supplied but you will have to rename it PathWindow before compiling it and running it. There will be separate PathWindow files for Phases 1, 2, and 3