Instructions to do assignment 6: READ the hand out!!! To set up your project: 1. create a new CUCSGraphicsApplication. 2. Paste CUCSGraphicsApplication and CUCSDrawing classes from this file over the ones in the project. 3. Add two new files to project: Grid and GridTriangle and paste code from this file into those. 4. See if it runs. It should do something, though not much. what you must do in these files: 1. In the class Grid, there is a function: public void drawGrid(Graphics g, int minDim, int argX, int argY) It does not work. You must modify it so that it draws an n by n grid of dots, where n is a field in the class grid that is already set. 2. Also in the class Grid, there is another function: public int fence(int Argx1, int Argy1, int Argx2, int Argy2) It returns zero right now. Using your fence function from the last assignment, modify this function to return the number of points on the line connecting (Argx1, Argy1) and (Argx2, Argy2) 3. In the class GridTriangle, which inherits from Grid, there is a function: public int boundary() It returns zero. Modify it so that it calculates the number of points on the triangle boundary described by the inherited fields (x1,y1) and (x2,y2) as well as the new field (x3,y3) and returns that value. 4. Also in GridTriange, there is a function: public int inside() It also presently returns 0. Modify it to count the number of points inside of a triangle. If you have no idea how to do this, re-read the handout. ************************************************************/ //paste this class over the supplied CUCSGraphicsApplication public class CUCSGraphicsApplication { public static void main(String args[]) { SystemInput sysIn; TokenReader in; GridTriangle Rhubarb; sysIn = new SystemInput(); in = new TokenReader(System.in); int x1,y1,x2,y2,x3,y3; System.out.println("enter a number for x1"); x1 = in.readInt(); System.out.println("enter a number for y1"); y1 = in.readInt(); System.out.println("enter a number for x2"); x2 = in.readInt(); System.out.println("enter a number for y2"); y2 = in.readInt(); System.out.println("enter a number for x3"); x3 = in.readInt(); System.out.println("enter a number for y3"); y3 = in.readInt(); Rhubarb = new GridTriangle(x1,y1,x2,y2,x3,y3); CUCSDrawing d = new CUCSDrawing(Rhubarb); System.out.println("the area is: " + Rhubarb.area()); d.setSize(200,150); d.setLocation(0,75); d.setTitle("Drawing"); d.show(); d.toFront(); d.repaint(); } } //paste this class over the supplied CUCSDrawing public class CUCSDrawing extends Frame { public GridTriangle m_Grid; CUCSDrawing(GridTriangle argM_Grid) { m_Grid = argM_Grid; } //tells the grid to draw itself public void paint(Graphics g) { int dim; //calculate the minimum dimension so the grid knows how big to be dim = (8* Math.min(getSize().width, getSize().height))/10; m_Grid.drawGridTriangle(g,dim,(dim/10), (dim/10)); } } //paste this class (and import statement) into a file named "Grid.java" import java.awt.Graphics; public class Grid{ protected int n;//size of grid. All grids are n*n, that is, square. protected int x,y; //position of upperleft corner of board protected int spacing;//distance from one point to another protected int diam;// diameter of a dot on the grid protected boolean draw=false;//if true, the grid will draw itself protected int x1,y1, x2,y2; //constructor takes as an argument the number of points to draw // in both x and y direction public Grid(int Argx1,int Argy1,int Argx2,int Argy2) { int minX, minY,DX,DY; if(Argx1>Argx2) {minX = Argx2;DX=Argx1-minX;} else {minX = Argx1;DX=Argx2-minX;} if(Argy1>Argy2) {minY = Argy2;DY=Argy1-minY;} else {minY = Argy1;DY=Argy2-minY;} x1=Argx1-minX; y1=Argy1-minY; x2=Argx2-minX; y2=Argy2-minY; n=Math.max(DX,DY); //anything bigger than 450 is going to be hard to draw if(n>450) draw = false; else draw = true; if(n<2) draw = false;//sanity check } public Grid(int paramN) { n = paramN; if(n>450) draw = false; else draw = true; if(n<2) draw = false;//sanity check } //drawGrid method has arguments for a graphics object, //an int indicating how big a square it should be drawn in, and //the x and y values of the upper left hand corner. public void drawGrid(Graphics g, int minDim, int argX, int argY) { /*diameter is the size of a dot in the grid. use as the 3rd and fourth arguments for draw oval */ diam = minDim/(4*(n-1)); //spacing is the distance from one dot to another spacing = (minDim-diam)/(n-1); //make sure dots are visible if(diam<3) diam = 3; //set x and y fields x=argX; y=argY; //check to see if we should bother to draw the grid if(draw) { //put code to draw a grid here /*hint: here is a sample call to fillOval that would draw a circle of diameter "diam" at x,y which is the upper left corner dot of the grid that you have to draw. the next dot to the right on the same line would be at x+ spacing, y: g.fillOval(x+spacing*0,y+spacing*0, diam, diam); next dot to rigth:: g.fillOval(x+spacing*1, y+spacing*0, diam,diam); */ //draw line g.drawLine(x+x1*spacing + (diam/2),y+y1*spacing+(diam/2),x+x2*spacing+(diam/2),y+y2*spacing+(diam/2)); }//end if(draw) }//end drawGrid //returns points on fence public int fence(int Argx1,int Argy1,int Argx2,int Argy2) { //modify this function to return the number of //points on a line connecting (Argx1, Argy1) to //(Argx2, Argy2). It should not return 0. return 0; } } //paste this class into a file named GridTriangle.java import java.awt.Graphics; import Grid; public class GridTriangle extends Grid { //need one point int x3,y3; public GridTriangle(int Argx1,int Argy1,int Argx2,int Argy2, int Argx3, int Argy3) { super(Math.max( ( Math.max(Math.max(Argx1,Argx2), Argx3)) -(Math.min(Math.min(Argx1,Argx2), Argx3)), ( Math.max(Math.max(Argy1,Argy2), Argy3)) -(Math.min(Math.min(Argy1,Argy2), Argy3)) )+1 ); int minX = Math.min(Math.min(Argx1,Argx2), Argx3); int minY = Math.min(Math.min(Argy1,Argy2), Argy3); x1 = Argx1-minX; y1 = Argy1-minY; x2 = Argx2-minX; y2 = Argy2-minY; x3 = Argx3-minX; y3 = Argy3-minY; } //draws grid and one line in call to parent, then draws other 2 lines public void drawGridTriangle(Graphics g, int minDim, int argX, int argY) { //draw grid and 1 side of triangle drawGrid(g, minDim, argX, argY); if(draw) { //draw other 2 lines connecting vertices of triangle g.drawLine(x+x1*spacing+(diam/2), y+y1*spacing+(diam/2), x+x3*spacing+(diam/2), y+y3*spacing+(diam/2)); g.drawLine(x+x3*spacing+(diam/2), y+y3*spacing+(diam/2), x+x2*spacing+(diam/2), y+y2*spacing+(diam/2)); } } //returns number of dots inside the three pts public int inside() { //insert code to calculate the number of points within //a triangle here //note that you have to change return statement return 0; } //calculate number of points on boundary public int boundary() { //insert code to calculate number of points on //boundary of triangle here //note that you must change return statement return 0; } public double area() { return ((double) boundary())/2 + inside()-1; } //handy function to check all of the values. //you need not use this. public void printVals() { System.out.println("n is "+ n + "and draw is "+ draw); System.out.println("x1: " + x1+ "\t y1:" + y1); System.out.println("x2: " + x2+ "\t y2:" + y2); System.out.println("x3: " + x3+ "\t y3:" + y3); } }