Chapter 12

The exercises here are taken from my forthcoming book, The Java Developer's Resource.

Quiz

  1. What happens if you try to draw objects at negative coordinates? e.g. g.drawRect(-5, -4, 64, 50)?
  2. What happens if you specify a negative height or width for a rectangle? e.g. g.drawRect(10, 10, -20, -20)?

Exercises

  1. You may have noticed that the clearRect method allows a simplistic sort of animation. While a more effective animation will require external timing and thus needs to wait for the discussion of threads in Chapter 19, you can begin simple animations now. The key is to write an animation loop completely within the paint method and call g.clearRect(0, 0, size().width-1, size().height-1) between each successive frame. Use this technique to write an applet which moves a rectangle from the lower left hand corner to the upper right hand corner. What happens if you comment out the call to clearRect?
  2. For the artistically inclined: write a version of Mondrian that draws pictures that are more believably in the style of Piet Mondrian. You should probably restrict your color choices and not allow rectangles to overlap.
  3. The Bullseye applet was so convoluted because it is relatively hard to center concentric rectangles inside another rectangle. On the other hand if you think of circles as being defined by their center point and a radius, it's not particularly difficult to draw concentric circles. Just keep the center point the same and draw circles with progressively larger radii. Write a CircleToRect method with the following signature
    	public Rectangle CircleToRect(Point p, int radius)
    
    which takes as its arguments a java.awt.Point and a radius and returns a java.awt.Rectangle. Before beginning you should review the documentation for points and rectangles at http://www.javasoft.com/JDK-1.0/api/java.awt.Rectangle.html and http://www.javasoft.com/JDK-1.0/api/java.awt.Point.html.
  4. Modify the Bullseye Applet so that it always draws a circular bullseye even if the applet isn't square.


[ Exercises | Cafe Au Lait | Books | Trade Shows | Links | FAQ | Tutorial | User Groups ]

Copyright 1996 Elliotte Rusty Harold
elharo@sunsite.unc.edu
Last Modified August 20, 1996