import element.*; import java.util.*;
/*Abstracts a Polygon, implements Drawable interface*/
public class Polygon implements Drawable { private final int n; //No of sides of polygon private Pt v[];//Vertices of the polygon
public Polygon(int noofsides, Pt vertex[]){ n = noofsides; v = new Pt[noofsides]; for(int i = 0; i < noofsides; i++) v[i] = new Pt(vertex[i]); }
//Copy constructor public Polygon(Polygon p){ this(p.n, p.v); }
//Returns the bottom-most point's y coordinate public int bottom(){ int bot = v[0].y(); for(int i = 1; i < n; i++){ bot = Math.max(bot, v[i].y()); } return bot; }
//Returns the center of the polygon public Pt center(){ return new Pt( (left() + right() / 2), (top() + bottom() / 2) ); }
//Resets the center of the Polygon. Does not draw Polygon after //resetting center public void center(Pt p){ Pt oldc; oldc = center(); int dx = p.x() - oldc.x(); int dy = p.y() - oldc.y(); for(int i = 0; i < n; i++){ v[i].move(dx, dy); } }
//Clears the Polygon from DrawingWindow public void clearOn(DrawingWindow d){ for(int i = 0; i < n; i++){ d.clear(new Line(v[i], v[(i+1)%n])); } }
//Draws the Polygon on the DrawingWindow public void drawOn(DrawingWindow d){ for(int i = 0; i < n; i++){ d.draw(new Line(v[i], v[(i+1)%n])); }
}
//Draws the Polygon on the DrawingWindow public void fillOn(DrawingWindow d){ drawOn(d); }
//Returns the height of Polygon public int height(){ return bottom() - top(); }
//Returns the leftmost point's x-coordinate public int left(){ int l = v[0].x(); for(int i = 1; i < n; i++){ l = Math.min(l, v[i].x()); } return l; }
//Returns the rightmost point's x-coordinate public int right(){ int r = v[0].x(); for(int i = 1; i < n; i++){ r = Math.max(r, v[i].x()); } return r; }
//Returns the top-most point's y coordinate public int top(){ int t = v[0].y(); for(int i = 1; i < n; i++){ t = Math.min(t, v[i].y()); } return t; }
//Returns width of Polygon public int width(){ return right() - left(); }
public static void polymain(){ DrawingWindow d = new DrawingWindow(); Random r = new Random(); int n = 6; Pt v[] = new Pt[n]; int w = d.bounds().width(); int h = d.bounds().height(); for(int i = 0; i < n; i++){ v[i] = new Pt( Math.abs(r.nextInt()) % w, Math.abs(r.nextInt()) % h ); } Polygon p1 = new Polygon(6, v); Polygon p2 = new Polygon(p1); p2.drawOn(d); }
}
import element.*;
public class DrawObj { public static void drawmain(){ ConsoleWindow c = new ConsoleWindow(); DrawingWindow d = new DrawingWindow(); int choice;
d.clear(d.bounds());
//Display Menu c.out.println("1. Line"); c.out.println("2. Rectangle"); c.out.println("3. Circle"); c.out.println("4. Oval"); c.out.println("What figure do u wish to draw ?(Press enter after u type in the choice)"); choice = c.input.readInt();
//Display prompt message based on choice switch(choice){ case 1: c.out.println("Click on two points to draw a line between them"); break; case 2: c.out.println("Click on the two opposite corner points of rectanlge"); break; case 3: c.out.println("Click on center and then on any point on circumference"); break; case 4: c.out.println("Click on the two opposite corner points of bounding rectangle"); break; default: break; }
//Get the two points d.awaitMousePress(); Pt p1 = d.awaitMouseRelease(); d.awaitMousePress(); Pt p2 = d.awaitMouseRelease(); Line l;
//Draw appropriate figure based on choice switch(choice){ case 1: l = new Line(p1,p2); l.drawOn(d); break; case 2: Rect r = new Rect(p1,p2); r.drawOn(d); break; case 3: l = new Line(p1,p2); int len = (int) Math.sqrt( l.width() * l.width() + l.height() * l.height() ); Circle circ = new Circle(p1, len); circ.drawOn(d); break; case 4: Oval o = new Oval(new Rect(p1,p2)); o.drawOn(d); break; default:
} }
}