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 ...

Get Java Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.