/******************************************************************************
* Name: Donna Gabai
* NetID: dgabai
* Precept: P99
*
* Description: An object class to represent a 2D diamond.
*
* Dependencies: StdDraw
****************************************************************************/
import java.awt.Color;
public class Diamond {
// instance variables
private final double r; // center to vertex distance
private double x; // center x-coordinate
private double y; // center y-coordinate
private Color c; // color of the diamond
// constructor (does not draw the diamond)
public Diamond(double r, double x, double y, Color c) {
// put the values of the arguments into the instance variables
this.r = r;
this.x = x;
this.y = y;
this.c = c;
}
// sets the pen color and draws the diamond
public void drawFilled() {
// compute the vertices and pack into x and y arrays
double[] xPts = {x - r, x, x + r, x};
double[] yPts = {y, y + r, y, y - r};
// set pen color
StdDraw.setPenColor(c);
// draw
StdDraw.filledPolygon(xPts, yPts);
}
// returns the area of the diamond: area = 2 * (r squared)
public double area() {
double a = 2 * r * r;
return a;
}
// changes the color
public void changeColor(Color newColor) {
c = newColor;
}
// changes the position
public void move(double deltax, double deltay) {
x = x + deltax;
y = y + deltay;
}
// test main (provided)
public static void main(String[] args) {
StdDraw.setXscale(-1, 1); // set scale
StdDraw.setYscale(-1, 1); // in main()
Color red = StdDraw.RED;
Diamond d = new Diamond(.5, -.5, .5, red);
d.drawFilled();
System.out.println(d.area());
d.changeColor(StdDraw.BLUE);
d.move(1, -1);
d.drawFilled();
System.out.println(d.area());
}
}