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

Picture.java 2.4KB

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