JMU
Lab: Experimenting with 2D Graphics


Instructions: Answer as many of the following questions as you can during the lab period. If you are unable to complete the assignment during the lab period it is strongly recommended that you complete it on your own.

Getting Ready: Before going any further, you should:

  1. Make a directory for this lab.
  2. Setup your development environment.
  3. Download the following files:
    to your working directory. (In most browsers, the easiest way to do this is by right-clicking on each of the links above.)

1. Review of User Interfaces: This part of the lab will help refresh your memory about GUI windows and components.
  1. Open, read, and understand BuzzyDriver.java
  2. Open, read, and understand BuzzyWindow.java
  3. What is the super class of BuzzyWindow?
  4. What is the purpose of the setupLayout() method in the BuzzyWindow class?
  5. What is the purpose of the setupWindow() method in the BuzzyWindow class?
2. Graphics Basics: This part of the lab will help you understand some basic concepts in computer graphics.
  1. Open, read, and understand BuzzyComponent.java
  2. What is the super class of BuzzyComponent?
  3. Compile and execute BuzzyDriver
  4. Why is nothing rendered in the window?
  5. At the bottom of the paint() method, send the setStroke(STROKE) message to g2. (i.e., call the setStroke() method of g2 passing it STROKE).
  6. Compile and execute BuzzyDriver
  7. Why is nothing rendered in the window?
  8. At the bottom of the paint() method, send a message to g2 that instructs it to use GOLD paint (i.e., send the setPaint(GOLD) message to g2).
  9. What code did you add?
  10. At the bottom of the paint() method, send a message to g2 that instructs it to fill the HELMET object.
  11. What code did you add?
  12. Compile and execute BuzzyDriver
  13. What is rendered in the window?
  14. Why doesn't the oval have a black border?
  15. At the bottom of the paint() method, send a message to g2 that instructs it to stroke the HELMET object.
  16. What code did you add?
  17. Why doesn't the oval have a black border?
  18. Before the call to the draw() method, send a message to g2 that instructs it to set the color to Color.BLACK.
  19. What code did you add?
  20. Compile and execute BuzzyDriver
  21. What is rendered in the window?
  22. In the constructor, initialize visor to be an Arc2D.Float object with the parameters (40,25,35,30,315,90,Arc2D.PIE).
  23. What code did you add?
  24. At the bottom of the paint() method, send messages to g2 that instruct it to set the color to gray, fill the visor, set the color to black, and draw the visor.
  25. What code did you add?
  26. Compile and execute BuzzyDriver
  27. What is rendered in the window?
3. Building Complicated Shapes: This part of the lab will help you understand how to build more complicated shapes. Note: This part of the lab assumes you are using Java v1.6 or higher. If not, you will need to use the GeneralPath class instead of the Path2D.Float class.
  1. In the constructor, send a message to the body object instructing it to "move the pen to" the point (20,50). That is, send the moveTo(20,50) message to body.
  2. Just after that, send a message to the body object instructing it to "draw a line to" the point (20,70).
  3. Just after that, send messages to the body object instructing it to "draw a line to" the points (20,90), (10,90), (10,100), (80,100), (80,90), (40,90), (40,70), and (40,50).
  4. Just after that, send messages to the body object instructing it to "close the path".
  5. What code did you add?
4. Some Important Considerations when Rendering: This part of the lab will help you understand how the order in which things are rendered can matter.
  1. At the bottom of the paint() method, send messages to g2 that instruct it to set the color to purple, fill the body, set the color to black, and draw the body.
  2. What code did you add?
  3. Compile and execute BuzzyDriver
  4. What is wrong with what is rendered?
  5. Fix this problem by changing the order in which the various objects are rendered.
  6. What does your code look like now?
  7. In the constructor, initialize antenna to be a QuadCurve2D.Float object with the parameters (10,0,40,10,30,25).
  8. What code did you add?
  9. Add code to the paint() method that draws the antenna in black "underneath" (i.e., visually underneath) the helmet.
  10. What code did you add?
  11. Where did you add it?
5. Rendering Images: This part of the lab will help you understand how to render images.
  1. At the bottom of the paint() method, check to see if the Image object named mars is null, and if it isn't, send the drawImage(mars, 100, 100, null) to g2.
  2. Compile and execute BuzzyDriver.
  3. Where is the upper-left corner of the image rendered?
  4. Modify the call to drawImage() so that the upper-left corner of the image is rendered at (0,0).
  5. What happened to Buzzy?
  6. Fix this problem.
  7. What change did you make?
6. Rendering Text: This part of the lab will help you understand how to render text.
  1. At an appropriate place in the paint() method, instruct the g2 object to draw the String literal "JMU" at the location (20,40). (Note: The String should be drawn in black.)
  2. What code did you add?
  3. Just before the call to drawString(), set the font used by g2 to the object named SLANTED.
  4. What code did you add?
  5. Compile and execute BuzzyDriver.
  6. What changed?

Copyright 2011