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

Picture.java 2.5KB

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