Extend the RegularPolygon
class created in Lab Assignment A6.2 to create a GraphicPolygon
class. Use the following declarations as a starting point for your lab work.
class GraphicPolygon extends RegularPolygon{
private DrawingTool pen = new DrawingTool(new SketchPad(400, 400));
private double xPosition, yPosition;
// constructors
// Initializes a polygon of numSides sides and sideLength
// length in the superclass. The polygon is centered at
// xPosition = yPosition = 0
public GraphicPolygon(int numSides, double sideLength){
}
// Initializes a polygon of numSides sides and sideLength
// length in the superclass. The polygon is centered at
// xPosition = x and yPosition = y
public GraphicPolygon(int numSides, double sideLength, double x, double y){
}
// public methods
// Sets xPosition = x and yPosition = y to correspond to the new
// center of the polygon
public void moveTo(double x, double y){
}
// Draws the polygon about the center point (xPosition, yPosition).
// Uses properties inherited from RegularPolygon to determine the
// number and length of the sides, the radius of the inscribed circle,
// and the vertex angle with which to draw the polygon
public void draw(){
}
}
Write two constructor methods. The first constructor initializes the number and length of the sides of a polygon centered about the point (0, 0). The Second constructor is used to initialize a polygon a specified number and length of sides with a center at a specified x and y location.
Write a method that draws the polygon onto the Sketchpad
window using the movement and drawing methods available in the DrawingTool
class.
Write a method that moves the center of the polygon to a specified x
and y
location.
Write a testing class with a main()
method that constructs a GraphicPolygon
and calls each method. Sample usage for a polygon with 9 sides of length 100 would give:
GraphicPolygon gPoly = new GraphicPolygon(9, 100);
gPoly.draw();