Chapter 12

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


  1. What happens if you try to draw objects at negative coordinates? e.g. g.drawRect(-5, -4, 64, 50)?

    The part of the object that's in negative coordinate space is not shown. You only see part of the object.

  2. What happens if you specify a negative height or width for a rectangle? e.g. g.drawRect(10, 10, -20, -20)?

    The rectangle is not drawn. It does not appear on the screen.


  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 and

     public Rectangle CircleToRect(Point p, int radius) {
       Rectangle r = new Rectangle;
       int leftmost = p.x - radius;
       int topmost = p.y - radius;
       int height = 2 * radius;
       int width = height;
       return new Rectangle(leftmost, topmost, width, height);
  4. Modify the Bullseye Applet so that it always draws a circular bullseye even if the applet isn't square.

    This is simple. Just set both AppletHeight and AppletWidth to the smaller of the applet's size and width.

    import java.applet.Applet;
    import java.awt.Graphics;
    import java.awt.Color;
    public class Bullseye extends Applet {
      public void paint(Graphics g) {
        int RectLeft, RectTop, RectHeight, RectWidth;
        int AppletHeight = Math.min(size().height, size().width) ;
        int AppletWidth = AppletHeight;
    	for (int i=8; i >= 0; i--) {
          if ((i % 2) == 0) g.setColor(;
          else g.setColor(Color.white);
          RectHeight = AppletHeight*i/8;
          RectWidth = AppletWidth*i/8;
          RectLeft = AppletWidth/2 - i*AppletWidth/16;
          RectTop = AppletHeight/2 - i*AppletHeight/16;
          g.fillOval(RectLeft, RectTop, RectWidth, RectHeight);

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

Copyright 1996 Elliotte Rusty Harold
Last Modified August 23, 1996