/** FrameApplication: Sample application discussed in class to demonstrate: AWT Containers Events Event Sources Event Listeners Graphics To run this sample, save this file as FrameApplication.java, compile with the "javac" compiler and run with the "java" jre. */ import java.awt.*; import java.awt.event.*; import java.util.*; public class FrameApplication extends Frame implements ActionListener { private Button circle, rectangle, text, clear; private HashSet images = new HashSet(); /** Create main application window. */ public FrameApplication() { setLayout( new FlowLayout( ) ); addWindowListener( new ExitWindowAdapter() ); circle = new Button("Circle"); rectangle = new Button("Rectangle"); text = new Button("Text"); clear = new Button("Clear"); add( circle ); add( rectangle ); add( text ); add( clear ); circle.addActionListener( this ); rectangle.addActionListener( this ); text.addActionListener( this ); clear.addActionListener( this ); } /** Iterate through all added images, determine the type of image, and draw accordingly. */ public void paint(Graphics g) { Iterator i = images.iterator(); while ( i.hasNext() ) { Object o = i.next(); if ( o instanceof Circle ) { g.setColor( Color.green ); Circle c = (Circle) o; g.drawOval( c.getX(), c.getY(), c.getW(), c.getH() ); } else if ( o instanceof Rectangle ) { g.setColor( Color.blue ); Rectangle r = (Rectangle) o; g.drawRect( r.getX(), r.getY(), r.getW(), r.getH() ); } else if ( o instanceof Text ) { g.setColor( Color.red ); Text t = (Text) o; Font f = new Font("SansSerif", Font.BOLD, t.getS()); g.setFont( f ); g.drawString( t.getV(), t.getX(), t.getY() ); } } } /** Called when any of the top buttons are clicked. */ public void actionPerformed( ActionEvent ae ) { Object o = ae.getSource(); Frame popup = null; String component = null; if ( o == clear ) { //clears all images from hashset images = new HashSet(); } else { //determine which type of window to create based on //what button was clicked if ( o == circle ) popup= new CircleChoices( this ); if ( o == rectangle ) popup = new RectangleChoices( this ); if ( o == text ) popup = new TextChoices( this ); popup.setSize(500, 100 ); popup.setLocation( 250, 250 ); popup.setVisible( true ); } repaint(); } /** Accept a new window from popup window, add it to HashSet, and repaint(). */ public void addImage( Object img ) { images.add( img ); repaint(); } /** Starts main application window. */ public static void main( String args[] ) { FrameApplication fm = new FrameApplication(); fm.setSize(500, 500 ); fm.setTitle("Frame Application Sample"); fm.setVisible( true ); } } /** Adapter that closes main application window. */ class ExitWindowAdapter extends WindowAdapter { // Exits when main window is closed. public void windowClosing( WindowEvent we ) { System.exit( 0 ); } } /** Adapter that closes popup configuration windows. */ class PopupWindowAdapter extends WindowAdapter { // Called when any of the submit buttons for // configuration windows are pressed. public void windowClosing( WindowEvent we ) { Frame f = (Frame)we.getSource(); f.setVisible( false ); } } /** Class that draws and processes the Circle Configuration window. */ class CircleChoices extends Frame implements ActionListener { Button submitButton; FrameApplication parent; Label lx, ly, lw, lh; TextField tx, ty, tw, th; public CircleChoices( FrameApplication p ) { setTitle("Circle Configuration"); setLayout( new FlowLayout( ) ); parent = p; addWindowListener( new PopupWindowAdapter() ); submitButton = new Button("Submit"); tx = new TextField(3); ty = new TextField(3); tw = new TextField(3); th = new TextField(3); add( new Label("X:") ); add( tx ); add( new Label("y:") ); add( ty ); add( new Label("w:") ); add( tw ); add( new Label("h:") ); add( th ); add( submitButton ); submitButton.addActionListener( this ); } // called when Submit is selected from Circle Configuration public void actionPerformed( ActionEvent ae ) { int x=250, y=250, w=50, h=50; try { x = Integer.parseInt( tx.getText() ); y = Integer.parseInt( ty.getText() ); w = Integer.parseInt( tw.getText() ); h = Integer.parseInt( th.getText() ); } catch ( Exception e ){} Circle c = new Circle ( x, y, w, h ); parent.addImage( c ); setVisible( false ); } } /** Class that draws and processes the Rectangle Configuration window. */ class RectangleChoices extends Frame implements ActionListener { Button submitButton; FrameApplication parent; Label lx, ly, lw, lh; TextField tx, ty, tw, th; public RectangleChoices( FrameApplication p ) { setTitle("Rectangle Configuration"); setLayout( new FlowLayout( ) ); parent = p; addWindowListener( new PopupWindowAdapter() ); submitButton = new Button("Submit"); tx = new TextField(3); ty = new TextField(3); tw = new TextField(3); th = new TextField(3); add( new Label("X:") ); add( tx ); add( new Label("y:") ); add( ty ); add( new Label("w:") ); add( tw ); add( new Label("h:") ); add( th ); add( submitButton ); submitButton.addActionListener( this ); } // called when Submit is selected from Rectangle Configuration public void actionPerformed( ActionEvent ae ) { int x=250, y=250, w=50, h=50; try { x = Integer.parseInt( tx.getText() ); y = Integer.parseInt( ty.getText() ); w = Integer.parseInt( tw.getText() ); h = Integer.parseInt( th.getText() ); } catch ( Exception e ){} Rectangle r = new Rectangle ( x, y, w, h ); parent.addImage( r ); setVisible( false ); } } /** Class that draws and processes the Text Configuration window. */ class TextChoices extends Frame implements ActionListener { Button submitButton; FrameApplication parent; Label lx, ly, ls, lv; TextField tx, ty, ts, tv; // creates popup accepting text configuration public TextChoices( FrameApplication p ) { setTitle("Text Configuration"); setLayout( new FlowLayout( ) ); parent = p; addWindowListener( new PopupWindowAdapter() ); submitButton = new Button("Submit"); tx = new TextField(3); ty = new TextField(3); ts = new TextField(3); tv = new TextField(15); add( new Label("X:") ); add( tx ); add( new Label("y:") ); add( ty ); add( new Label("size:") ); add( ts ); add( new Label("text:") ); add( tv ); add( submitButton ); submitButton.addActionListener( this ); } // called when Submit is selected from Text Configuration public void actionPerformed( ActionEvent ae ) { int x=250, y=250, s=50; String v="Text"; try { x = Integer.parseInt( tx.getText() ); y = Integer.parseInt( ty.getText() ); s = Integer.parseInt( ts.getText() ); v = tv.getText(); } catch ( Exception e ){} Text t = new Text ( x, y, s, v ); parent.addImage( t ); setVisible( false ); } } /** This class represents a Circle to be drawn in the Frame Application Window. */ class Circle { private int x, y, w, h; public Circle( int inX, int inY, int inW, int inH ) { x = inX; y = inY; w = inW; h = inH; } public int getX() { return x; } public int getY() { return y; } public int getW() { return w; } public int getH() { return h; } } /** This class represents a Rectangle to be drawn in the Frame Application Window. */ class Rectangle { private int x, y, w, h; public Rectangle( int inX, int inY, int inW, int inH ) { x = inX; y = inY; w = inW; h = inH; } public int getX() { return x; } public int getY() { return y; } public int getW() { return w; } public int getH() { return h; } } /** This class represents a String to be drawn in the Frame Application Window. */ class Text { private int x, y, s; String v; public Text( int inX, int inY, int inS, String inV ) { x = inX; y = inY; s = inS; v = inV; } public int getX() { return x; } public int getY() { return y; } public int getS() { return s; } public String getV() { return v; } }