Download presentation
Presentation is loading. Please wait.
1
Applets & Applications CSC 171 FALL 2001 LECTURE 15
2
History: Grace Hooper 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.
3
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
4
GUIs in general Sometimes, it is useful to have a program that runs as either an applet or an application
5
Applet/App Architecture DrawShapes Applet init() setWidth() setHeight() main() Button Handler DrawPanel actionPerformed() actionListener JPanel paintComponent() setcurrentchoice()
6
Relationships DrawShapes – is-a Applet – has-a Jframe (inside of main) – Has-a DrawPanel – Has-a ButtonHandler DrawPanel is-a Jpanel ButtonHandler implements ActionListener
7
What Happens In an Applet? JVM calls DrawShapes.init() – Builds DrawPanel – Builds ButtonHandler – BuildsButtons – Adds components
8
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
9
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 ] ); }
10
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 );
11
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();
12
Get it going // attach applet to center of window applicationWindow.getContentPane().add( appletObject ); //note nested method calls applicationWindow.setSize( width, height ); applicationWindow.setVisible( true );
13
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
14
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
15
DrawPanel Using a separate panel allows painting without interfering with other components Loosely coupled with Applet – No shared data
16
When ButtonHandler invokes setCurrentChoice() // specify current shape choice and repaint public void setCurrentChoice( int choice ) { currentChoice = choice; repaint(); }
17
DrawPanel public void paintComponent( Graphics g ) { super.paintComponent( g ); switch( currentChoice ) { case 0: g.drawLine( randomX(), randomY(), randomX(), randomY() ); break; //etc.... } } // end method paintComponent
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.