/** *
* In version 3.1 if the client code is in colorMode(HSB, 360, 100, 100), * the default fill color is set to grey; * in RGB mode the default fill is set to white. * * @author Rosie Coyle * @version 3.0 * @since 2017-07-05 */ import java.awt.Point; import java.awt.Dimension; class Cylinder { /** * The centers of the ellipse faces of the cylinder that are farthest from (backCenter) and closest to * (frontCenter) the viewer. Point objects. */ private Point backCenter, frontCenter; /** * The width and height of the ellipse faces of the cylinder that are farthest from (backSize) and closest to * (frontSize) the viewer. Dimension objects. */ private Dimension backSize, frontSize; /** * totalSteps: number of total ellipses or arcs drawn * xStep: the amount the x value of the center of the ellipse or pair of arcs changes with each drawn * yStep: the amount the y value of the center of the ellipse or pair of arcs changes with each drawn * wStep: the amount the width of the ellipse or pair of arcs changes with each drawn * hStep: the amount the width of the ellipse or pair of arcs changes with each drawn */ private float totalSteps, xStep, yStep, wStep, hStep; /** * strokeColor: color of the outside of the cylinder object * fillColor: color of the inside of the cylinder object */ color strokeColor, fillColor; /** * Constructs a Cylinder object with a default fill color and a black stroke color from * two Points and two Dimensions. * * @pre backXY and frontXY are Point objects, backWH and frontWH are Dimension objects * @post constructs a Cylinder, fillColor is default, strokeColor is black * @param backXY A Point object representing the center of the ellipse face of the * Cylinder object which is farthest from the viewer * @param frontXY A Point object representing the center of the ellipse face of the * Cylinder object which is closest to the viewer * @param backWH A Dimension object representing the size, width and height, of the * ellipse face of the Cylinder object which is farthest from the viewer * @param frontWH A Dimension object representing the size, width and height, of the * ellipse face of the Cylinder object which is closest to the viewer */ Cylinder(Point backXY, Point frontXY, Dimension backWH, Dimension frontWH) { this(backXY, frontXY, backWH, frontWH, color(255), color(0)); } /** * Constructs a Cylinder object with a a black stroke color from two Points, two * Dimensions, and a fill color. * * @pre backXY and frontXY are Point objects, backWH and frontWH are Dimension objects, * fillC is a color * @post constructs a Cylinder, strokeColor is black * @param backXY A Point object representing the center of the ellipse face of the * Cylinder object which is farthest from the viewer * @param frontXY A Point object representing the center of the ellipse face of the * Cylinder object which is closest to the viewer * @param backWH A Dimension object representing the size, width and height, of the * ellipse face of the Cylinder object which is farthest from the viewer * @param frontWH A Dimension object representing the size, width and height, of the * ellipse face of the Cylinder object which is closest to the viewer * @param fillC A color which will the the fill color of the cylinder */ Cylinder(Point backXY, Point frontXY, Dimension backWH, Dimension frontWH, color fillC) { this(backXY, frontXY, backWH, frontWH, fillC, color(0)); } /** * Constructs a Cylinder object with a a black stroke color from two Points, two * Dimensions, a fill color, and a stroke color. * * @pre backXY and frontXY are Point objects, backWH and frontWH are Dimension objects, * fillC is a color, strokeC is a color * @post constructs a Cylinder * @param backXY A Point object representing the center of the ellipse face of the * Cylinder object which is farthest from the viewer * @param frontXY A Point object representing the center of the ellipse face of the * Cylinder object which is closest to the viewer * @param backWH A Dimension object representing the size, width and height, of the * ellipse face of the Cylinder object which is farthest from the viewer * @param frontWH A Dimension object representing the size, width and height, of the * ellipse face of the Cylinder object which is closest to the viewer * @param fillC A color which will the the fill color of the cylinder * @param strokeC A color which will the the stroke color of the cylinder */ Cylinder(Point backXY, Point frontXY, Dimension backWH, Dimension frontWH, color fillC, color strokeC) { backCenter = backXY; frontCenter = frontXY; backSize = backWH; frontSize = frontWH; strokeColor = strokeC; fillColor = fillC; init(); } /** * Assigns values to instance variables that aren't assigned values in the constructors: xStep, yStep, wStep, and hStep. * * @post assigns xStep, yStep, wStep, and hStep values */ private void init() { totalSteps = max( abs( backCenter.x - frontCenter.x ), abs( backCenter.y - frontCenter.y ) ); xStep = ( frontCenter.x - backCenter.x )/totalSteps; yStep = ( frontCenter.y - backCenter.y )/totalSteps; wStep = ( frontSize.width - backSize.width )/totalSteps; hStep = ( frontSize.height - backSize.height )/totalSteps; } /** * Fetch backCenter Point from cylinder. * * @post returns Point backCenter from cylinder * @return Point backCenter from cylinder object. */ public Point getBackCenter() { return backCenter; } /** * Fetch frontCenter Point from cylinder. * * @post returns Point frontCenter from cylinder * @return Point frontCenter from cylinder object. */ public Point getFrontCenter() { return frontCenter; } /** * Fetch backSize Dimension from cylinder. * * @post returns Dimension backSize from cylinder * @return Dimension backSize from cylinder object. */ public Dimension getBackSize() { return backSize; } /** * Fetch frontSize Dimension from cylinder. * * @post returns Dimension frontSize from cylinder * @return Dimension frontSize from cylinder object. */ public Dimension getFrontSize() { return frontSize; } /** * Sets the backCenter Point of the cylinder. * * @post sets cylinder's backCenter to newBackCenter * @param newBackCenter The new backCenter Point. */ public void setBackCenter(Point newBackCenter) { backCenter = newBackCenter; init(); } /** * Sets the frontCenter Point of the cylinder. * * @post sets cylinder's frontCenter to newFrontCenter * @param newFrontCenter The new frontCenter Point. */ public void setFrontCenter(Point newFrontCenter) { frontCenter = newFrontCenter; init(); } /** * Sets the backSize Dimension of the cylinder. * * @post sets cylinder's backSize to newBackSize * @param newBackSize The new backSize Dimension. */ public void setBackSize(Dimension newBackSize) { backSize = newBackSize; init(); } /** * Sets the frontSize Dimension of the cylinder. * * @post sets cylinder's frontSize to newFrontSize * @param newFrontSize The new frontSize Dimension. */ public void setFrontSize(Dimension newFrontSize) { frontSize = newFrontSize; init(); } /** * Standard string representation of a cylinder. * * @post returns string representation * @return String representing cylinder. */ public String toString() { return "Cylinder [back: x = " + backCenter.x +", y = " + backCenter.y + ", w = " + backSize.width + ", h = " + backSize.height + " front: x = " + frontCenter.x + ", y = " + frontCenter.y + ", w = " + frontSize.width + ", h = " + frontSize.height + "]"; } /** * Standard vomparison function. Comparison based on frontSize and backSize only. * * @pre other is non-null Cylinder * @post returns true iff the backSize and frontSize are equal * @param other Another cylinder. * @return true iff the backSize and frontSize are equal. */ public boolean equals(Cylinder other) { if (other instanceof Cylinder) { return (other.getBackSize().width == backSize.width && other.getBackSize().height == backSize.height && other.getFrontSize().width == frontSize.width && other.getFrontSize().height == frontSize.height); } else { return false; } } /** * Draws cylinder in window without shading. * * @post draws cylinder in window */ public void drawCylinder() { drawShadedCylinder(0, 0, strokeColor); } /** * Draws cylinder in window with shading. * * @pre angle1 and angle2 are floats, shadeColor is a color * @post draws cylinder in window * @param angle1 Starting angle of the shaded section of the cylinder in degrees. * @param angle2 Ending angle of the shaded section of the cylinder in degrees. * @param shadeColor Color of shading. */ public void drawShadedCylinder(float angle1, float angle2, color shadeColor) { float x = backCenter.x; float y = backCenter.y; float w = backSize.width; float h = backSize.height; float startAngle = angle1 * (PI/180); float stopAngle = angle2 * (PI/180); fill(fillColor); strokeWeight(2); for ( int stepCount = 0; stepCount < totalSteps; stepCount++ ) { stroke(shadeColor); arc(x, y, w, h, startAngle, stopAngle); stroke(strokeColor); arc(x, y, w, h, stopAngle, 2*PI + startAngle); x += xStep; y += yStep; w += wStep; h += hStep; } } }