Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. public class Square {
  2. private int size; // The side of a side of the square
  3. private int xPosition; // The x,y coordinates of its top left corner
  4. private int yPosition;
  5. private String color; // The color of the square
  6.  
  7. // Create a new square
  8. public Square(int xPosition, int yPosition, int size, String color) {
  9. this.xPosition = xPosition;
  10. this.yPosition = yPosition;
  11. this.size = size;
  12. this.color = color;
  13. draw();
  14. }
  15.  
  16. // Move the square to a new position.
  17. public void moveTo(int newX, int newY) {
  18. erase();
  19. this.xPosition = newX;
  20. this.yPosition = newY;
  21. draw();
  22. }
  23.  
  24. // Move the square by an amount.
  25. public void move(int deltaX, int deltaY) {
  26. erase();
  27. this.xPosition = this.xPosition + deltaX;
  28. this.yPosition = this.yPosition + deltaY;
  29. draw();
  30. }
  31.  
  32. // Change the side to the new side.
  33. public void changeSize(int newSize) {
  34. erase();
  35. this.size = newSize;
  36. draw();
  37. }
  38.  
  39. // Change the color.
  40. // Valid colors are "red","yellow","blue","green","magenta" and "black".
  41. public void changeColor(String newColor) {
  42. erase();
  43. this.color = newColor;
  44. draw();
  45. }
  46.  
  47. // Draw the square with current specifications on screen.
  48. private void draw() {
  49. Canvas canvas = Canvas.getCanvas();
  50. canvas.setForegroundColour(this.color);
  51. canvas.fill(new java.awt.Rectangle(this.xPosition, this.yPosition, this.size, this.size));
  52. canvas.wait(50);
  53. }
  54.  
  55. // Erase the square on screen.
  56. public void erase() {
  57. Canvas canvas = Canvas.getCanvas();
  58. canvas.erase(new java.awt.Rectangle(this.xPosition, this.yPosition, this.size, this.size));
  59. }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement