Printing Graphics: An example

The following simple program prints some circles:

import java.awt.*;


public class PrintableBullseye extends Canvas {

  public static void main(String[] args) {
  
    PrintableFrame pf = new PrintableFrame("Printable Frame");
    pf.add("Center", new PrintableBullseye());
    pf.setVisible(true);
  
  }

  public Dimension getPreferredSize() {
    return new Dimension(250, 250);
  }

  public Dimension getMinimumSize() {
    return new Dimension(50, 50);
  }

  public void paint(Graphics g) {
    
    int rectLeft, rectTop, rectHeight, rectWidth;
    int height = this.getSize().height;
    int width = this.getSize().width;
    Color red = Color.red;
    if (g instanceof PrintGraphics) {
      red = Color.black;
    }
    for (int i=8; i >= 0; i--) {
      if ((i % 2) == 0) g.setColor(red);
      else g.setColor(Color.white);
      rectHeight = height*i/8;
      rectWidth = width*i/8;
      rectLeft = width/2 - i*width/16;
      rectTop = height/2 - i*height/16;
      g.fillOval(rectLeft, rectTop, rectWidth, rectHeight);
    }
  }

}

Previous | Next | Top
Last Modified May 19, 1999
Copyright 1997, 1999 Elliotte Rusty Harold
elharo@metalab.unc.edu