Download presentation
Presentation is loading. Please wait.
1
For Computational Finance Java Applets and the AWT Jeff Stephenson (slides adapted from Manu Kumar)
2
Java Applets? u Applets are Java programs which can run within your web browser u Applets vs. Applications –Applets t run in the web browser t subject to security restrictions t do not have a main() –Applications t full fledged Java programs t run from commandline t no security restrictions u So far we’ve built applications –for good reason too since we’ll need what we’ve learnt!
3
Java Applet Sandbox u Applets Vs. Application –Applet: runs in Web browser. –Application: runs in any Java VM. u Applet restrictions (http://www.javasoft.com/sfaq) –no access to Client’s file System t cannot check for existence, read, write, or rename a file t cannot create or list the contents of a directory t cannot check a file’s type, timestamp or size. –Network connection ONLY to originating host t cannot connect to arbitrary servers / sites across the net t key issue when designing client-server applets
4
Java Architecture in light of Sandbox DB WWW Applet The Web server machine must act as the proxy for all calls to services on other machines An overly simplified description :
5
Java Displaying applets in the browser u To add a Java applet to your HTML page –use </APPLET tag t Full Syntax: t [ ] [Text for non-Java supporting browsers] u Browsers treatment of an applet tag –Java enabled browser will load applet –non-Java enabled browser will ignore t it will display Text for non-Java supporting browsers
6
Java JavaPhysical Example u Place compiled Applet classes in directory under web server u Include tag in HTML file – Your Browser is NOT Java Enabled! u Load up page in Java enabled browser!
7
Java Applet Class u Applets inherit from java.applet.Applet –provides basic structure for applet to run in a browser u What you must/should override –default constructor, init(), start(), stop(), destroy() u For a threaded applet –implement Runnable interface –override run() u For graphics –may override repaint(), update() and paint()
8
Java Applet Lifecycle u Browser loads HTML page –Find tag –Locates code using codebase –Downloads.class files to browser –Browser verifies.class files for security u Applet methods executed AUTOMATICALLY in this order: –init() –start() –leaving the page calls stop() –returning to the page calls start() again –exiting the browser or leaving the page permanently calls destroy()
9
Java HelloWorldApplet import java.awt.*; import java.applet.*; public class HelloWorld extends Applet { public void init() { resize (150, 25); } public void paint(Graphics g) { g.drawString("Hello World!", 50, 25); }
10
Java Applets in the Real World u To make applets more functional –May need a complicated interface –Must respond to events –Do something useful u Applet uses –Make information and functionality available via browser! –No download necessary –Works automatically –Browser becomes the “platform”
11
Java Handling Events u Sribble Applet u public boolean mouseDown(Event e, int x, int y) { lastX = x; lastY = y; return true; } u public boolean mouseDrag(Event e, int x, int y) { Graphics g = getGraphics(); g.drawLine(lastX, lastY, x, y); lastX = x; lastY = y; return true; }
12
Java Scribble Applet CodeWalk u Notice the two methods: –mouseDown –mouseDrag u These get call automatically when any mouse event occurs –all we need to do is override them and take the appropriate action u If we run the applet –we can draw anything we like with the mouse!
13
Java Making applet functional u Requires: –may more graphical components t buttons t labels t checkboxes t text fields etc. –handling events other than mouse events t click on button t check a checkbox t doubleclick in a list t type in a text box etc. u To do this we’ll need the AWT!!!
14
Java AWT u AWT –Abstract Windowing Toolkit –Awkward Windowing Toolkit ?!? u Java’s library for graphical elements –Makes EXTENSIVE use of inheritance and OO techniques! t That’s why you needed to learn them before we got to Applets –Hierarchical approach to developing “Components” t you’ll see when we look at the API –The AWT is platform independent t gives you the same components and “widgets” on all platforms! t Allows you to write code for the Java-platform as opposed to Windows or Mac or UNIX
15
Java AWT Classes u The two MAIN classes in the AWT are –Component –Container u Component –nearly all the “widgets” in the AWT inherit from Component t it inherits attributes and behavior from Component u Container –a placeholder –a Container can “contain” other Components –a Container is a Component t I.e. a Container can contain another Container
16
Java Layout Managers u AWT uses the concept of “Layout Managers” to define the user interface u Layout Managers –tell the AWT where to position a component or a container –tell the AWT what to do when we resize the application/applet! t Dynamic resizing t Layout Managers specify everything using “relationships” –There are lots of layout managers, we’ll look at a few… t GridLayout t BorderLayout
17
Java Handling Events u Two approaches to handling events –we saw one approach earlier t overriding specific event handler methods such as mouseDown, mouseDrag etc. –today we will see the better approach t using Listners
18
Java AWT Widgets u Frame u Panel u Canvas u Button u Checkbox u Choice u Label u List u ScrollBar u TextArea u TextField
19
Java AWT development process u Steps: –Create a Container t An applet extends Panel and is therefore already a Container –Set the layout manager –Instantiate the components –Add the components to the container using the layout manager –Call show() to actually draw the components on the screen u Tricks: –Remember the containers can be NESTED t VERY USEFUL TRICK! –Use multiple nested layouts for complex user interfaces
20
Java LayoutManagers u We’ll only look at two today –BorderLayout t Provide North, South, East, West and Center regions –GridLayout t Divides the container into equally spaced rows and columns u Lets see some working Demos and explanations!! –Off to the Java Tutorial from here… –http://www.javasoft.com/docs/books/tutorial/ui/layout/using.html
21
Java Nesting in AWT u Key Concept: –A Container is also a Component t in most cases we care about for this class u Example Problem –Say we wish to create the layout shown on the right –Characteristics: t Button Bar on the left t TextField at the bottom t TextArea in the rest –Note: t buttons are equally spaced
22
Java Nesting in AWT (2) u Process –Layout the design you want on paper or a whiteboard! –Analyze the design to see what sections you can identify t Buttons on the left can be one possible piece t TextArea and TextField are the other two pieces –Work from top to bottom t identify a suitable layout for the big pieces identified above –BorderLayout (surprise!) Buttons in the West TextField in the South TextArea in the North t But what about the “Buttons” –GridLayout for the buttons! Here is where we’ll use nesting
23
Java Nesting in AWT (3) u Sample Solution –(see ButtonPad.java): // set the top level layout setLayout(new BorderLayout(3,3)); // create the text area notesArea = new WrappedTextArea(10, 30); notesArea.setEditable(false); // add the text area add("Center", notesArea); // create the text field entryField = new TextField(40); // add the entry field add("South", entryField); // create a NEW PANEL for NESTING! Panel buttonPanel = new Panel(); //set the layout in this panel buttonPanel.setLayout(new GridLayout(0, 1, 5, 5)); //create and add the buttons button1 = new Button("Button 1"); button2 = new Button("Button 2"); button3 = new Button("Button 3"); button4 = new Button("Button 4"); button5 = new Button("Button 5"); buttonPanel.add(button1); buttonPanel.add(button2); buttonPanel.add(button3); buttonPanel.add(button4); buttonPanel.add(button5); //add the panel to the west of the main BorderLayout add("West", buttonPanel);
24
Java Handling Events u Two approaches to handling events –we saw one approach earlier t overriding specific event handler methods such as mouseDown, mouseDrag etc. –today we will see the better approach t using Listners
25
Java Handling Events u Types of events: –ACTION_EVENT, LIST_SELECT, LIST_DESELECT, WINDOW_DESTROY, WINDOW_ICONIFY, WINDOW_DEICONIFY, WINDOW_MOVED, MOUSE_DOWN, MOUSE_UP, MOUSE_DRAG, KEY_PRESS, KEY_ACTION, KEY_RELEASE, KEY_ACTION_RELEASE, GOT_FOCUS, LOST_FOCUS, MOUSE_ENTER, MOUSE_EXIT, MOUSE_MOVE u All can be handled with special listners –We’ll use a MouseListner as an example u OR you can override the methods corresponding to each event –what we saw in the Scribble applet
26
Java Handling Events (2) public class MouseEventDemo... implements MouseListener {...//where initialization occurs: //Register for mouse events on blankArea and applet (panel). blankArea.addMouseListener(this); addMouseListener(this);...
27
Java Handling Events (3) u Continued ….. public void mousePressed(MouseEvent e) { saySomething("Mouse pressed; # of clicks: " + e.getClickCount(), e); } public void mouseReleased(MouseEvent e) { saySomething("Mouse released; # of clicks: " + e.getClickCount(), e); } public void mouseEntered(MouseEvent e) { saySomething("Mouse entered", e); } …….. }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.