first BlueJ lab

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**
  2. * This class represents a simple picture. You can draw the picture using
  3. * the draw method. But wait, there's more: being an electronic picture, it
  4. * can be changed. You can set it to black-and-white display and back to
  5. * colors (only after it's been drawn, of course).
  6. *
  7. * This class was written as an early example for teaching Java with BlueJ.
  8. *
  9. * @author Michael Kölling and David J. Barnes
  10. * @version 1.1 (24 May 2001)
  11. */
  12. public class Picture
  13. {
  14. private Square wall;
  15. private Square window;
  16. private Triangle roof;
  17. private Circle sun;
  18. private Circle sun2;
  19. /**
  20. * Constructor for objects of class Picture
  21. */
  22. public Picture()
  23. {
  24. // nothing to do... instance variables are automatically set to null
  25. }
  26. /**
  27. * Draw this picture.
  28. */
  29. public void draw()
  30. {
  31. wall = new Square();
  32. wall.moveVertical(80);
  33. wall.changeSize(100);
  34. wall.makeVisible();
  35. window = new Square();
  36. window.changeColor("black");
  37. window.moveHorizontal(20);
  38. window.moveVertical(100);
  39. window.makeVisible();
  40. roof = new Triangle();
  41. roof.changeSize(50, 140);
  42. roof.moveHorizontal(60);
  43. roof.moveVertical(70);
  44. roof.makeVisible();
  45. sun = new Circle();
  46. sun.changeColor("blue");
  47. sun.moveHorizontal(180);
  48. sun.moveVertical(50);
  49. sun.changeSize(60);
  50. sun.makeVisible();
  51. }
  52. public void sunset()
  53. {
  54. for (int x=1; x<20; x++){
  55. sun.slowMoveVertical(15);
  56. }}
  57. /**
  58. * Change this picture to black/white display
  59. */
  60. public void setBlackAndWhite()
  61. {
  62. if(wall != null) // only if it's painted already...
  63. {
  64. wall.changeColor("black");
  65. window.changeColor("white");
  66. roof.changeColor("black");
  67. sun.changeColor("black");
  68. }
  69. }
  70. /**
  71. * Change this picture to use color display
  72. */
  73. public void setColor()
  74. {
  75. if(wall != null) // only if it's painted already...
  76. {
  77. wall.changeColor("red");
  78. window.changeColor("black");
  79. roof.changeColor("green");
  80. sun.changeColor("yellow");
  81. }
  82. }
  83. }