O'Reilly logo

Java Cookbook by Ian F. Darwin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Program: PlotterAWT

In Section 8.12, we discussed a series of Plotter classes. The PlotterAWT class shown in Example 12-7 extends that to provide a “plot preview” service: before being plotted on a (probably slow) plotter, the plot is displayed in an AWT window using the Graphics drawing primitives.

Example 12-7. PlotterAWT.java

import java.awt.*; import java.awt.event.*; /** * A Plotter subclass for drawing into an AWT Window. Reflecting back * to AWT gives us a "known working" plotter to test on. * You can also steal this as a basis for your own plotter driver! */ public class PlotterAWT extends Plotter { Frame f; Image os; PCanvas p; Graphics g; Font font; FontMetrics fontMetrics; PlotterAWT( ) { super( ); f = new Frame("Plotter"); p = new PCanvas(os, MAXX, MAXY); f.add(p); f.pack( ); f.setVisible(true); f.addWindowListener(new WindowAdapter( ) { public void windowClosing(WindowEvent e) { // If we do setVisible and dispose, then the Close completes PlotterAWT.this.f.setVisible(false); PlotterAWT.this.f.dispose( ); System.exit(0); } }); g = p.getOsGraphics( ); } public void drawBox(int w, int h) { g.drawRect(curx, cury, w, h); p.repaint( ); } public void rmoveTo(int incrx, int incry){ moveTo(curx += incrx, cury += incry); } public void moveTo(int absx, int absy){ if (!penIsUp) g.drawLine(curx, cury, absx, absy); curx = absx; cury = absy; } public void setdir(float deg){} void penUp( ){ penIsUp = true; } void penDown( ){ penIsUp = false; } void penColor(int c){ switch(c) { case ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required