123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- import java.awt.*;
-
- /**
- * A square that can be manipulated and that draws itself on a canvas.
- *
- * @author Michael Kölling and David J. Barnes
- * @version 1.0 (15 July 2000)
- */
-
- public class Square
- {
- private int size;
- private int xPosition;
- private int yPosition;
- private String color;
- private boolean isVisible;
-
- /**
- * Create a new square at default position with default color.
- */
- public Square()
- {
- size = 30;
- xPosition = 60;
- yPosition = 50;
- color = "red";
- isVisible = false;
- }
-
- /**
- * Make this square visible. If it was already visible, do nothing.
- */
- public void makeVisible()
- {
- isVisible = true;
- draw();
- }
-
- /**
- * Make this square invisible. If it was already invisible, do nothing.
- */
- public void makeInvisible()
- {
- erase();
- isVisible = false;
- }
-
- /**
- * Move the square a few pixels to the right.
- */
- public void moveRight()
- {
- moveHorizontal(20);
- }
-
- /**
- * Move the square a few pixels to the left.
- */
- public void moveLeft()
- {
- moveHorizontal(-20);
- }
-
- /**
- * Move the square a few pixels up.
- */
- public void moveUp()
- {
- moveVertical(-20);
- }
-
- /**
- * Move the square a few pixels down.
- */
- public void moveDown()
- {
- moveVertical(20);
- }
-
- /**
- * Move the square horizontally by 'distance' pixels.
- */
- public void moveHorizontal(int distance)
- {
- erase();
- xPosition += distance;
- draw();
- }
-
- /**
- * Move the square vertically by 'distance' pixels.
- */
- public void moveVertical(int distance)
- {
- erase();
- yPosition += distance;
- draw();
- }
-
- /**
- * Slowly move the square 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 += delta;
- draw();
- }
- }
-
- /**
- * Slowly move the square 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 += delta;
- draw();
- }
- }
-
- /**
- * Change the size to the new size (in pixels). Size must be >= 0.
- */
- public void changeSize(int newSize)
- {
- erase();
- size = newSize;
- draw();
- }
-
- /**
- * Change the color. Valid colors are "red", "yellow", "blue", "green",
- * "magenta" and "black".
- */
- public void changeColor(String newColor)
- {
- color = newColor;
- draw();
- }
-
- /*
- * Draw the square with current specifications on screen.
- */
- private void draw()
- {
- if(isVisible) {
- Canvas canvas = Canvas.getCanvas();
- canvas.draw(this, color,
- new Rectangle(xPosition, yPosition, size, size));
- canvas.wait(10);
- }
- }
-
- /*
- * Erase the square on screen.
- */
- private void erase()
- {
- if(isVisible) {
- Canvas canvas = Canvas.getCanvas();
- canvas.erase(this);
- }
- }
- }
|