first BlueJ lab

Picture.java 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. /**
  19. * Constructor for objects of class Picture
  20. */
  21. public Picture()
  22. {
  23. // nothing to do... instance variables are automatically set to null
  24. }
  25. /**
  26. * Draw this picture.
  27. */
  28. public void draw()
  29. {
  30. wall = new Square();
  31. wall.moveVertical(80);
  32. wall.changeSize(100);
  33. wall.makeVisible();
  34. window = new Square();
  35. window.changeColor("black");
  36. window.moveHorizontal(20);
  37. window.moveVertical(100);
  38. window.makeVisible();
  39. roof = new Triangle();
  40. roof.changeSize(50, 140);
  41. roof.moveHorizontal(60);
  42. roof.moveVertical(70);
  43. roof.makeVisible();
  44. sun = new Circle();
  45. sun.changeColor("yellow");
  46. sun.moveHorizontal(180);
  47. sun.moveVertical(-10);
  48. sun.changeSize(60);
  49. sun.makeVisible();
  50. }
  51. /**
  52. * Change this picture to black/white display
  53. */
  54. public void setBlackAndWhite()
  55. {
  56. if(wall != null) // only if it's painted already...
  57. {
  58. wall.changeColor("black");
  59. window.changeColor("white");
  60. roof.changeColor("black");
  61. sun.changeColor("black");
  62. }
  63. }
  64. /**
  65. * Change this picture to use color display
  66. */
  67. public void setColor()
  68. {
  69. if(wall != null) // only if it's painted already...
  70. {
  71. wall.changeColor("red");
  72. window.changeColor("black");
  73. roof.changeColor("green");
  74. sun.changeColor("yellow");
  75. }
  76. }
  77. }