The very first BlueJ lab. Draw the house of your dreams.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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("yellow");
  47. sun.moveHorizontal(180);
  48. sun.moveVertical(-10);
  49. sun.changeSize(60);
  50. sun.makeVisible();
  51. }
  52. //sunset
  53. public void sunset() {
  54. sun.slowMoveVertical(300);
  55. }
  56. /**
  57. * Change this picture to black/white display
  58. */
  59. public void setBlackAndWhite()
  60. {
  61. if(wall != null) // only if it's painted already...
  62. {
  63. wall.changeColor("black");
  64. window.changeColor("white");
  65. roof.changeColor("black");
  66. sun.changeColor("black");
  67. }
  68. }
  69. /**
  70. * Change this picture to use color display
  71. */
  72. public void setColor()
  73. {
  74. if(wall != null) // only if it's painted already...
  75. {
  76. wall.changeColor("red");
  77. window.changeColor("black");
  78. roof.changeColor("green");
  79. sun.changeColor("yellow");
  80. }
  81. }
  82. }