import java.awt.*; import java.awt.geom.*; /** * A circle that can be manipulated and that draws itself on a canvas. * * @author Michael Kölling and David J. Barnes * @version 2016.02.29 */ public class Circle { private int diameter; // The circle's diameter private int xPosition; // The circle's x position private int yPosition; private String color; private boolean isVisible; /** * Create a new circle at default position with default color. * This is a constructor. It should give values to every field * in the class. */ public Circle() { diameter = 68; // This sets the diameter to 68 xPosition = 230; yPosition = 90; color = "blue"; isVisible = false; System.out.println("Created a circle with diameter of "+diameter); } /** * Here's a constructor that takes a parameter, and uses that * value to set up the initial size. */ public Circle(int initialSize) { if (initialSize < 0) { System.out.println("You can't have a negative diameter!!"); initialSize = Math.abs(initialSize); } if (initialSize > 200) { System.out.println("That seems too big. I'll use 100 instead."); initialSize = 100; } diameter = initialSize; // copy value into field so it sticks around xPosition = 230; yPosition = 90; //color = "blue"; if (diameter < 50) { color = "blue"; } else { if (diameter >= 100) { color = "yellow"; } else { color = "red"; } } // isVisible = false; makeVisible(); // Start it out visible } /** * Here's a constructor that takes initial x and y positions. */ public Circle(int x, int y) { diameter = 68; xPosition = x; yPosition = y; color = "blue"; // isVisible = false; makeVisible(); // Start it out visible } /** * Our first attempt at a method that returns a value. */ public int getSize() { System.out.println("About to return the diameter."); return diameter; // Describes how to get returned value // System.out.println("Just returned the diameter."); } /** * Make this circle visible. If it was already visible, do nothing. */ public void makeVisible() { isVisible = true; draw(); } /** * Make this circle invisible. If it was already invisible, do nothing. */ public void makeInvisible() { erase(); isVisible = false; } /** * Move the circle a few pixels to the right. */ public void moveRight() { moveHorizontal(20); } /** * Moves the circle farther to the right than normal. */ public void moveFartherRight() { moveHorizontal(50); } /** * Move the circle a few pixels to the left. */ public void moveLeft() { moveHorizontal(-20); } /** * Move the circle a few pixels up. */ public void moveUp() { moveVertical(-20); } /** * Move the circle a few pixels down. */ public void moveDown() { moveVertical(20); } /** * Move the circle horizontally by 'distance' pixels. */ public void moveHorizontal(int distance) { erase(); xPosition = xPosition + distance; draw(); } /** * Move the circle vertically by 'distance' pixels. */ public void moveVertical(int distance) { erase(); yPosition = yPosition + distance; draw(); } /** * Slowly move the circle horizontally by 'distance' pixels. */ public void slowMoveHorizontal(int distance) { int delta; if(distance < 0) { delta = -1; distance = -distance; } else { delta = 1; } for(int i = 0; i < distance; i++) { xPosition = xPosition + delta; draw(); } } /** * Slowly move the circle vertically by 'distance' pixels. */ public void slowMoveVertical(int distance) { int delta; if(distance < 0) { delta = -1; distance = -distance; } else { delta = 1; } for(int i = 0; i < distance; i++) { yPosition = yPosition + delta; draw(); } } /** * Change the size to the new size (in pixels). Size must be >= 0. */ public int changeSize(int newDiameter) { int oldDiameter = diameter; // Remember the current diameter // System.out.println("Changed size from "+diameter+" to "+newDiameter); System.out.print("Changed size from "+diameter); erase(); diameter = newDiameter; draw(); System.out.println(" to "+diameter); return oldDiameter; } /** * Change the color. Valid colors are "red", "yellow", "blue", "green", * "magenta" and "black". */ public void changeColor(String newColor) { color = newColor; draw(); } /** * Draw the circle with current specifications on screen. */ private void draw() { if(isVisible) { Canvas canvas = Canvas.getCanvas(); canvas.draw(this, color, new Ellipse2D.Double(xPosition, yPosition, diameter, diameter)); canvas.wait(10); } } /** * Erase the circle on screen. */ private void erase() { if(isVisible) { Canvas canvas = Canvas.getCanvas(); canvas.erase(this); } } }