// Graphic User Interface Demo program. This AWT program shows // how to use various Components and how to add a "wallpaper" // effect by overriding paint. // // Written 2003 by Wayne Pollock, Tampa Florida USA. // Updated 2011 to replace Vector with ArrayList. import java.awt.*; import java.awt.event.*; import java.util.*; public class UIDemo extends Frame { private java.util.List<Component> components = new ArrayList<Component>(); private boolean done = false; private MenuBar mb; public static void main ( String [] args ) { UIDemo myFrame = new UIDemo("UI Demo"); myFrame.addWindowListener( new WindowAdapter() { public void windowClosing ( WindowEvent e ) { System.exit( 0 ); } } ); myFrame.runDemo(); } public UIDemo ( String title ) { super( title ); setSize( 450, 300 ); setLayout( new FlowLayout( FlowLayout.CENTER, 10, 10 ) ); mb = new MenuBar(); Menu fileMenu = new Menu( "File" ); Menu editMenu = new Menu( "Edit" ); Menu fruitMenu = new Menu( "Fruit" ); mb.add( fileMenu ); mb.add( editMenu ); fileMenu.add( new MenuItem( "New..." ) ); fileMenu.add( new MenuItem( "Open..." ) ); fileMenu.addSeparator(); // add( "-" ); works too. fileMenu.add( fruitMenu ); editMenu.add( "Cut" ); editMenu.add( "Copy" ); editMenu.add( "Paste" ); fruitMenu.add( "Apple" ); fruitMenu.add( "Orange" ); fruitMenu.add( "Mango" ); components.add( new Label( "I'm a Label!" ) ); components.add( new Button( "Do nothing Button" ) ); components.add( new TextField( "A TextField" ) ); components.add( new TextArea( "A 2 x 20 TextArea", 2, 20 ) ); java.awt.List lst = new java.awt.List(); lst.add( "A List"); lst.add( "by default has" ); lst.add( "4 lines showing," ); lst.add( "only one item" ); lst.add( "can be selected." ); components.add( lst ); Choice myChoice = new Choice(); myChoice.add( "A Choice"); myChoice.add( "Apples" ); myChoice.add( "Oranges" ); myChoice.add( "Mangos" ); components.add( myChoice ); components.add( new Checkbox( "A Checkbox item" ) ); Panel radioBtns = new Panel(); CheckboxGroup cbg = new CheckboxGroup(); radioBtns.add(new Checkbox("Radio Btn 1", cbg, true)); radioBtns.add(new Checkbox("Radio Btn 2", cbg, false)); components.add( radioBtns ); } private void runDemo () { setVisible( true ); pause( 3 ); setMenuBar( mb ); validate(); Iterator<Component> it = components.iterator(); while ( it.hasNext() ) { repaint(); // Shouldn't be needed but it is. pause( 3 ); Component c = it.next(); add( c ); validate(); } done = true; repaint(); } public void paint ( Graphics g ) { wallpaper( g ); if ( ! done ) g.drawString( "Running Demo...", 180, getHeight() - 20 ); else g.drawString( "Demo Finished!", 180, getHeight() - 20 ); super.paint( g ); // Paint lightweight components (if any) } private void wallpaper ( Graphics g ) { Toolkit tk = Toolkit.getDefaultToolkit(); Image tile = tk.getImage( "backgrnd.gif" ); int tileHeight = tile.getHeight( this ); int tileWidth = tile.getWidth( this ); if ( tileHeight == -1 || tileWidth == -1 ) return; // Must wait for image to fully load. int frameHeight = getHeight(); int frameWidth = getWidth(); for ( int y = 0; y < frameHeight; y += tileHeight ) { for ( int x = 0; x < frameWidth; x += tileWidth ) { g.drawImage( tile, x, y, this ); } } } // The loop to sleep is better than trying to sleep the // whole interval in one shot, because most system clocks // are just not accurate enough! private void pause ( int seconds ) { long now = System.currentTimeMillis(); long stopTime = now + ( seconds * 1000 ); while ( now < stopTime ) { try { Thread.sleep( 50 ); } catch ( InterruptedException ignored ) {} now = System.currentTimeMillis(); } } }