Skip to Main Content
Java Cookbook
book

Java Cookbook

by Ian F. Darwin
June 2001
Intermediate to advanced content levelIntermediate to advanced
888 pages
21h 1m
English
O'Reilly Media, Inc.
Content preview from Java Cookbook

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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Practical Cloud-Native Java Development with MicroProfile

Practical Cloud-Native Java Development with MicroProfile

Emily Jiang, Andrew McCright, John Alcorn, David Chan, Alasdair Nottingham
Distributed Computing in Java 9

Distributed Computing in Java 9

Raja Malleswara Rao Malleswara Rao Pattamsetti

Publisher Resources

ISBN: 0596001703Supplemental ContentCatalog PageErrata