// // "drawing frame" class. // import java.awt.* ; import java.awt.event.* ; // // a DrawingFrame object is a frame that // (1) listens for window events, and processes "close" by // disposing of the frame, and // (2) provides methods to find the "real" size of the drawing // area (excluding border and titlebar). // this class has a static counter variable that keeps track of the // number of frames created and not closed. when all have been // closed, System.exit() is called to terminate the application. // public class DrawingFrame extends Frame implements WindowListener { static int framecount = 0 ; // zero-argument constructor for frame without title. public DrawingFrame() { super() ; addWindowListener(this) ; framecount++ ; pack() ; // be sure insets are initialized } // one-argument constructor for frame with title. public DrawingFrame(String s) { super(s) ; addWindowListener(this) ; framecount++ ; pack() ; // be sure insets are initialized } // set size such that "drawable area" has specified dimensions. public void setDrawingSize(Dimension dim) { Insets insets = getInsets() ; setSize(new Dimension( dim.width + insets.left + insets.right, dim.height + insets.top + insets.bottom)) ; } // get dimensions of "drawable area". public Dimension getDrawingSize() { Insets insets = getInsets() ; Dimension dim = getSize() ; return new Dimension( dim.width - (insets.left + insets.right), dim.height - (insets.top + insets.bottom)) ; } // translate origin to top left corner of "drawing area". public void translateDrawingArea(Graphics g) { Insets insets = getInsets() ; g.translate(insets.left, insets.top) ; } // respond to window-close event. public void windowClosing(WindowEvent wEvt) { // get rid of frame and release associated resources. dispose() ; framecount-- ; if (framecount <= 0) System.exit(0) ; } // ignore other window events. public void windowClosed(WindowEvent wEvt) {} ; public void windowOpened(WindowEvent wEvt) {} ; public void windowIconified(WindowEvent wEvt) {} ; public void windowDeiconified(WindowEvent wEvt) {} ; public void windowActivated(WindowEvent wEvt) {} ; public void windowDeactivated(WindowEvent wEvt) {} ; // main program to test: // // command-line arguments are x1, y1, x2, y2 -- (width, height) // pairs for two frames. public static void main(String[] args) { if (args.length < 3) { System.out.println("Arguments are " + "x1 y1 x2 y2") ; System.exit(-1) ; } int x1 = Integer.parseInt(args[0]) ; int y1 = Integer.parseInt(args[1]) ; int x2 = Integer.parseInt(args[2]) ; int y2 = Integer.parseInt(args[3]) ; DrawingFrame f1 = new DrawingFrame("f1") ; DrawingFrame f2 = new DrawingFrame("f2") ; System.out.println("initial sizes:") ; System.out.println("f1: " + f1.getDrawingSize()) ; System.out.println("f2: " + f2.getDrawingSize()) ; f1.setDrawingSize(new Dimension(x1, y1)) ; f2.setDrawingSize(new Dimension(x2, y2)) ; System.out.println("sizes after setDrawingSize():") ; System.out.println("f1: " + f1.getDrawingSize()) ; System.out.println("f2: " + f2.getDrawingSize()) ; f1.show() ; f2.show() ; } }