For Computational Finance Java Applets and the AWT Jeff Stephenson (slides adapted from Manu Kumar)

Slides:



Advertisements
Similar presentations
1 Graphical User Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
Advertisements

Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Java Applets:. How Applets differ from application?: They do not use main method but init(), start() and paint() methods of the applet class They can.
Graphic User Interfaces Layout Managers Event Handling.
Java Applet & JavaScript SNU OOPSLA Lab. October 2005.
Programming and Problem Solving With Java Copyright 1999, James M. Slack Applets What is an Applet? Applet Parameters Graphics in Applets Other Applet.
1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,
June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 1 Lecture 9 Object Oriented Programming in Java Advanced Topics Abstract Windowing.
18-Jun-15 Applets. 2 An applet is a program that is typically embedded in a Web page and can be run from a browser You need special HTML in the Web page.
CS3157 Java UI Recitation. Material Covered: Overview of AWT components, Event Handling, creating applets, and sample UI. Not covered in recitation: Drawing,
Applets. An applet is a Panel that allows interaction with a Java program A applet is typically embedded in a Web page and can be run from a browser You.
26-Jun-15 Applets. 2 An applet is a Panel that allows interaction with a Java program A applet is typically embedded in a Web page and can be run from.
28-Jun-15 Applets. 2 An applet is a program that is typically embedded in a Web page and can be run from a browser You need special HTML in the Web page.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Java GUI building with the AWT. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many.
Applets  The Applet Class  The HTML Tag F Passing Parameters to Applets.
Applets Java API.
20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 16: Java Applets & AWT Fundamentals of Web Programming.
Chapter 12 Applets and Advanced Graphics  The Applet Class  The HTML Tag F Passing Parameters to Applets F Conversions Between Applications and Applets.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
Java Applets. 2 Introduction to Java Applet Programs  Applications are stand alone programs executed with Java interpreter executed with Java interpreter.
Chapter 12 Applets and Advanced GUI  The Applet Class  The HTML Tag F Passing Parameters to Applets F Conversions Between Applications and Applets F.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Introduction to Applets Course Lecture Slides 29 th July 2010.
Cs884(Prasad)java12AWT1 Abstract Windowing Toolkit Support for Graphical User Interface (Event-driven programming)
Chapter 10: Applets and Advanced Graphics The Applet Class The Applet Class The HTML Tag The HTML Tag Passing Parameters to Applets Passing Parameters.
Java Applets. An applet is a Panel that allows interaction with a Java program. A applet is typically embedded in a Web page and can be run from a browser.
 2002 Prentice Hall, Inc. All rights reserved Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
1 Outline 1 Introduction 2 Overview of Swing Components 3 JLabel 4 Event Handling 5 TextFields 6 How Event Handling Works 7 JButton 8 JCheckBox and JRadioButton.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Java Applets. 2 Introduction to Java Applet Programs Applications are ___________________ programs –executed with Java interpreter Applet is a small program.
CSC 205 – Java Programming II Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are stand-alone.
Pravin Yannawar, DOCS, NMU Jalgaon Basic Java : Applets 2 Objectives of This Session Identify the need for Applets Distinguish between Applets and Applications.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
Layout Managers Arranges and lays out the GUI components on a container.
Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
Java Programming Applets. Topics Write an HTML document to host an applet Understand simple applets Use Labels with simple AWT applets Write a simple.
Chapter 10: Applets and Advanced Graphics The Applet Class The Applet Class The HTML Tag The HTML Tag Passing Parameters to Applets Passing Parameters.
The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include: a rich set of user interface components; a.
Chapter 14 Applets and Advanced GUI  The Applet Class  The HTML Tag F Passing Parameters to Applets F Conversions Between Applications and Applets F.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
1 Applets. 2 What is an applet? Applet: a Java program that can be inserted into a web page and run by loading that page in a browser brings web pages.
AWT Layout Managers (Chapter 10) Java Certification Study Group January 21, 1999 Mark Roth.
Graphical User Interfaces (GUI). PART ONE About GUI’s.
CHAPTER Agenda Applets Servelets Browsers HelloWorld.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Applets. 9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L12: Applets Slide 2 Applets Usually.
XP New Perspectives on Macromedia Dreamweaver MX 2004 Tutorial 5 1 Adding Shared Site Elements.
1 Applets Programming. Introduction Java programs are divided into two main categories, applets and applications. An application is an ordinary Java program.
AWT Vs SWING. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many applications Uses.
Lecture 09 Applets.
Chap 7. Building Java Graphical User Interfaces
Graphical User Interfaces -- Introduction
Distributed Computing, M. L. Liu
Distributed Computing, M. L. Liu
Java applets 1/3/2019.
11.1 Applets & graphics.
GUI building with the AWT
JAVA APPLET PREPARED BY Mr. Jahanzaib Ahmed
G6DICP - Lecture 27 Java Applets.
Presentation transcript:

For Computational Finance Java Applets and the AWT Jeff Stephenson (slides adapted from Manu Kumar)

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!

Java Applet Sandbox u Applets Vs. Application –Applet: runs in Web browser. –Application: runs in any Java VM. u Applet restrictions ( –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

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 :

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

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!

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()

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()

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); }

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”

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; }

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!

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!!!

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

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

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

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

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

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

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… –

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

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

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);

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

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

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);...

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); } …….. }