uni

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

Triangle.java (3856B)


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