170 likes | 283 Views
Applets & Applications. CSC 171 FALL 2001 LECTURE 15. 1952 Grace Hopper working on the UNIVAC, "The Education of a Computer", ( Proc. ACM Conference ) The use of selected pre-written code segments to be assembled into programs written in a high level language
E N D
Applets & Applications CSC 171 FALL 2001 LECTURE 15
1952 Grace Hopper working on the UNIVAC, "The Education of a Computer", (Proc. ACM Conference) The use of selected pre-written code segments to be assembled into programs written in a high level language The concept of a compiler, and the general concept of language translation. History: Grace Hooper
Applets & Applications • Applets • Generally run over networks • Code download • Client side execution • Extend Applet • Applications • Generally run on hosts • Resident host • Local host execution • Extend Frame
GUIs in general • Sometimes, it is useful to have a program that runs as either an applet or an application
Applet/App Architecture Button Handler actionListener Applet actionPerformed() DrawShapes JPanel setWidth() init() DrawPanel setHeight() main() setcurrentchoice() paintComponent()
Relationships • DrawShapes • is-a Applet • has-a Jframe (inside of main) • Has-a DrawPanel • Has-a ButtonHandler • DrawPanel is-a Jpanel • ButtonHandler implements ActionListener
What Happens In an Applet? JVM calls DrawShapes.init() • Builds DrawPanel • Builds ButtonHandler • BuildsButtons • Adds components
What Happens In an Application? JVM calls DrawShapes.main() • Builds Jframe application window • Builds DrawShapes Applet (see previous) • Calls DrawShapes.init() • Calls DrawShapes.start. • Adds applet to application window • I.E. (har) : Frame takes place of browser
Using Command Line Args public static void main( String args[] ){ int width, height; // check for command-line arguments if ( args.length != 2 ) { width = 300; height = 200; } else { width = Integer.parseInt( args[ 0 ] ); height = Integer.parseInt( args[ 1 ] ); }
Build the Application // create window in which applet will execute JFrame applicationWindow = new JFrame( "An applet running as an application"); applicationWindow.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
Build the applet from the application // create one applet instance DrawShapes appletObject = new DrawShapes(); appletObject.setWidth( width ); appletObject.setHeight( height ); // call applet's init and start methods appletObject.init(); appletObject.start();
Get it going // attach applet to center of window applicationWindow.getContentPane().add( appletObject ); //note nested method calls applicationWindow.setSize( width, height ); applicationWindow.setVisible( true );
Architecture of Applet • Separate class for button handler • Separtae class for drawing region • Separate button panel & Draw panel • Applet is in BorderLayout • NORTH • Panel containing grid layout with 3 buttons • CENTER • Drawing region
ButtonHandler private class ButtonHandler implements ActionListener { // can access choices because it is an inner class public void actionPerformed( ActionEvent event ) { for ( int count = 0; count < choices.length; count++ ) if ( event.getSource() == choices[ count ] ) { drawingPanel.setCurrentChoice( count ); break; } } } // end private inner class ButtonHandler
DrawPanel • Using a separate panel allows painting without interfering with other components • Loosely coupled with Applet • No shared data
When ButtonHandler invokes setCurrentChoice() // specify current shape choice and repaint public void setCurrentChoice( int choice ) { currentChoice = choice; repaint(); }
DrawPanel public void paintComponent( Graphics g ) { super.paintComponent( g ); switch( currentChoice ) { case 0: g.drawLine( randomX(), randomY(), randomX(), randomY() ); break; //etc. . . . } } // end method paintComponent