uni

Thing1's amazing uni repo
Log | Files | Refs | Submodules

Circle.java (3915B)


      1 import java.awt.*;
      2 import java.awt.geom.*;
      3 
      4 /**
      5  * A circle that can be manipulated and that draws itself on a canvas.
      6  * 
      7  * @author	Michael Kolling and David J. Barnes
      8  * @version 1.0  (15 July 2000)
      9  */
     10 
     11 public class Circle
     12 {
     13 	private int diameter;
     14 	private int xPosition;
     15 	private int yPosition;
     16 	private String color;
     17 	private boolean isVisible;
     18 
     19 	@Override
     20 	public String toString(){
     21 		return "diameter: " + diameter + 
     22 			", xPosition: " + xPosition + 
     23 			", yPosition: " + yPosition + 
     24 			", color: " + color + 
     25 			", isVisible: " + isVisible;
     26 	}
     27 
     28 	/**
     29 	 * Create a new circle at default position with default color.
     30 	 */
     31 	public Circle()
     32 	{
     33 		diameter = 30;
     34 		xPosition = 0;
     35 		yPosition = 0;
     36 		color = "blue";
     37 		isVisible = false;
     38 	}
     39 
     40 	/**
     41 	 * Make this circle visible. If it was already visible, do nothing.
     42 	 */
     43 	public void makeVisible()
     44 	{
     45 		isVisible = true;
     46 		draw();
     47 	}
     48 
     49 	/**
     50 	 * Make this circle invisible. If it was already invisible, do nothing.
     51 	 */
     52 	public void makeInvisible()
     53 	{
     54 		erase();
     55 		isVisible = false;
     56 	}
     57 
     58 	public void setX(int x) {
     59 		xPosition = x;	
     60 	}
     61 
     62 	public void setY(int y) {
     63 		yPosition = y;	
     64 	}
     65 
     66 	/**
     67 	 * Move the circle a few pixels to the right.
     68 	 */
     69 	public void moveRight()
     70 	{
     71 		moveHorizontal(20);
     72 	}
     73 
     74 	/**
     75 	 * Move the circle a few pixels to the left.
     76 	 */
     77 	public void moveLeft()
     78 	{
     79 		moveHorizontal(-20);
     80 	}
     81 
     82 	/**
     83 	 * Move the circle a few pixels up.
     84 	 */
     85 	public void moveUp()
     86 	{
     87 		moveVertical(-20);
     88 	}
     89 
     90 	/**
     91 	 * Move the circle a few pixels down.
     92 	 */
     93 	public void moveDown()
     94 	{
     95 		moveVertical(20);
     96 	}
     97 
     98 	/**
     99 	 * Move the circle horizontally by 'distance' pixels.
    100 	 */
    101 	public void moveHorizontal(int distance)
    102 	{
    103 		erase();
    104 		xPosition += distance;
    105 		draw();
    106 	}
    107 
    108 	/**
    109 	 * Move the circle vertically by 'distance' pixels.
    110 	 */
    111 	public void moveVertical(int distance)
    112 	{
    113 		erase();
    114 		yPosition += distance;
    115 		draw();
    116 	}
    117 
    118 	/**
    119 	 * Slowly move the circle horizontally by 'distance' pixels.
    120 	 */
    121 	public void slowMoveHorizontal(int distance)
    122 	{
    123 		int delta;
    124 
    125 		if(distance < 0) 
    126 		{
    127 			delta = -1;
    128 			distance = -distance;
    129 		}
    130 		else 
    131 		{
    132 			delta = 1;
    133 		}
    134 
    135 		for(int i = 0; i < distance; i++)
    136 		{
    137 			xPosition += delta;
    138 			draw();
    139 		}
    140 	}
    141 
    142 	/**
    143 	 * Slowly move the circle vertically by 'distance' pixels.
    144 	 */
    145 	public void slowMoveVertical(int distance)
    146 	{
    147 		int delta;
    148 
    149 		if(distance < 0) 
    150 		{
    151 			delta = -1;
    152 			distance = -distance;
    153 		}
    154 		else 
    155 		{
    156 			delta = 1;
    157 		}
    158 
    159 		for(int i = 0; i < distance; i++)
    160 		{
    161 			yPosition += delta;
    162 			draw();
    163 		}
    164 	}
    165 
    166 	/**
    167 	 * Change the size to the new size (in pixels). Size must be >= 0.
    168 	 */
    169 	public void changeSize(int newDiameter)
    170 	{
    171 		erase();
    172 		diameter = newDiameter;
    173 		draw();
    174 	}
    175 
    176 	/**
    177 	 * Get the current diameter size
    178 	 */
    179 	public int getSize(){
    180 		return diameter;
    181 	}
    182 
    183 	/**
    184 	 * Change the color. Valid colors are "red", "yellow", "blue", "green",
    185 	 * "magenta" and "black".
    186 	 */
    187 	public void changeColor(String newColor)
    188 	{
    189 		color = newColor;
    190 		draw();
    191 	}
    192 
    193 	/**
    194 	 * Get the current color
    195 	 */
    196 	public String getColor(){
    197 		return color.toString();
    198 	}
    199 
    200 	/**
    201 	 * Get current X coordiate in the canvas
    202 	 */
    203 	public int getXCoord(){
    204 		return xPosition;
    205 	}
    206 
    207 	/**
    208 	 * Get current Y coordiate in the canvas
    209 	 */
    210 	public int getYCoord(){
    211 		return yPosition;
    212 	}
    213 
    214 	/*
    215 	 * Draw the circle with current specifications on screen.
    216 	 */
    217 	private void draw()
    218 	{
    219 		if(isVisible) {
    220 			Canvas canvas = Canvas.getCanvas();
    221 			canvas.draw(this, color, new Ellipse2D.Double(xPosition, yPosition, 
    222 						diameter, diameter));
    223 			canvas.wait(10);
    224 		}
    225 	}
    226 
    227 	/*
    228 	 * Erase the circle on screen.
    229 	 */
    230 	private void erase()
    231 	{
    232 		if(isVisible) {
    233 			Canvas canvas = Canvas.getCanvas();
    234 			canvas.erase(this);
    235 		}
    236 	}
    237 }