uni

Thing1's amazing uni repo
Log | Files | Refs

Square.java (875B)


      1 import java.awt.*;
      2 import java.awt.geom.*;
      3 
      4 public class Square {
      5 	private int w;
      6 	private int h;
      7 	private int x;
      8 	private int y;
      9 	private String color;
     10 	private boolean isVisible;
     11 
     12 	public Square() {
     13 		w = 30;
     14 		h = 30;
     15 		x = 30;
     16 		y = 30;
     17 		color = "blue";
     18 		isVisible = false;
     19 	}
     20 
     21 	public String toString() {
     22 		return "width: " + w +
     23 			", height: " + h +
     24 			", x: " + x +
     25 			", y: " + y +
     26 			", color: " + color +
     27 			", visible: " + isVisible;
     28 	}
     29 
     30 	private void draw() {
     31 		if (isVisible) {
     32 			Canvas canvas = Canvas.getCanvas();
     33 			canvas.draw(this, color, 
     34 					new Rectangle2D.Double(x, y, w, h));
     35 			canvas.wait(1000);
     36 		}
     37 	}
     38 
     39 	private void erase() {
     40 		if (isVisible) {
     41 			Canvas canv = Canvas.getCanvas();
     42 			canv.erase(this);
     43 		}
     44 	}
     45 
     46 	public void makeVisible() {
     47 		isVisible = true;
     48 		draw();
     49 	}
     50 
     51 	public void makeInvisible() {
     52 		erase();
     53 		isVisible = false;
     54 	}
     55 };