This is a simple applet which draws a series of filled, concentric circles alternating red and white, in other words a bullseye.

import java.applet.*;
import java.awt.*;

public class Bullseye extends Applet {

  public void paint(Graphics g) {
    int appletHeight = this.getSize().height;
    int appletWidth = this.getSize().width;

    for (int i=8; i >= 0; i--) {
      if ((i % 2) == 0) g.setColor(Color.red);
      else g.setColor(Color.white);
      // Center the rectangle
      int rectHeight = appletHeight*i/8;
      int rectWidth  = appletWidth*i/8;
      int rectLeft   = appletWidth/2  - i*appletWidth/16;
      int rectTop    = appletHeight/2 - i*appletHeight/16;
      g.fillOval(rectLeft, rectTop, rectWidth, rectHeight);


The .class file that draws this image is only 684 bytes. The equivalent GIF image is 1,850 bytes, almost three times larger.

Almost all the work in this applet consists of centering the enclosing rectangles inside the applet. The lines in bold do that. The first two lines just set the height and the width of the rectangle to the appropriate fraction of the applet's height and width. The next two lines set the position of the upper left hand corner. Once the rectangle is positioned, drawing the oval is easy.

Previous | Next | Top | Cafe au Lait

Copyright 1997, 1998, 2001, 2002 Elliotte Rusty Harold
Last Modified February 13, 2002