import edu.neu.ccs.gui.*; import java.awt.*; import java.awt.geom.*; // A simple class to use for practice exam. // The x, y, z coordinates are for the center of the cube. // When displayed in the graphics window all that shows is the // front face of the cube (involving just the x and y coordinates). public class Cube { // instance variables private double x; private double y; private double z; private double size; // the length of one edge of the cube private Color color; // constructor public Cube(double x, double y, double z, double size, Color color){ this.x = x; this.y = y; this.z = z; this.size = size; this.color = color; } // getters public double getX(){ return x; } public double getY(){ return y; } public double getZ(){ return z; } public double getSize() { return size; }; public Color getColor() { return color; }; // setter public void setColor(Color c){ color = c; } // other instance methods and helpers // move the cube to a specified location public void moveTo(double xCoord, double yCoord, double zCoord){ x = xCoord; y = yCoord; z = zCoord; } // move the cube by a specified displacement public void moveBy(double deltaX, double deltaY, double deltaZ){ x += deltaX; y += deltaY; z += deltaZ; } // public boolean isHigherThan(Cube otherCube){ // ****** Student writes this // } // grow the size of the cube public void grow(double deltaSize){ size += deltaSize; } // display a Cube object in a graphics window // (but repaint must be done afterward) public void draw(Graphics2D g) { Rectangle2D.Double r = new Rectangle2D.Double(x-size/2, y-size/2, size, size); g.setPaint(color); g.fill(r); } // erase a Cube object in a graphics window // (but repaint must be done afterward) public void erase(Graphics2D g) { Rectangle2D.Double r = new Rectangle2D.Double(x-size/2, y-size/2, size, size); g.setPaint(Color.white); g.fill(r); } // move the center of the cube so it fits entirely // within the graphics window // (only the x and y coordinates matter for this) public void adjustToFit(BufferedPanel window){ adjustX(window); adjustY(window); } private void adjustX(BufferedPanel window){ if (x - size/2 < 0.0) x = size/2; else if (x + size/2 > window.getWidth()) x = window.getWidth() - size/2; } private void adjustY(BufferedPanel window){ if (y - size/2 < 0.0) y = size/2; else if (y + size/2 > window.getHeight()) y = window.getHeight() - size/2; } }