Presentation is loading. Please wait.

Presentation is loading. Please wait.

20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 16: Java Applets & AWT Fundamentals of Web Programming.

Similar presentations


Presentation on theme: "20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 16: Java Applets & AWT Fundamentals of Web Programming."— Presentation transcript:

1 20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 16: Java Applets & AWT Fundamentals of Web Programming Lecture 16: Java Applets & AWT

2 20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 2 Lecture 16: Java Applets & AWT Outline Why use Java Applets? What Java Applets can and can’t do (example)? Compiling and adding an Applet to a web page. Applet structure. Abstract Window Toolkit (AWT). Events.

3 20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 3 Lecture 16: Java Applets & AWT Why use Java Applets? Easily added to html. Executed within the browser (client-side). Extensive computation at the client possible, a full-blown programming language (Java vs. JavaScript). Good support of graphics. Platform independence.

4 20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 4 Lecture 16: Java Applets & AWT Structure of an Applet All applets extend the Applet class. They inherit a bunch of methods that need to be redefined. They usually do not have the main method (unless standalone). States in the browser: loaded, running, stopped, unloaded.

5 20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 5 Lecture 16: Java Applets & AWT Applet states and methods Loaded: the code of the applet is loaded with the web page; The browser calls init(); Destroyed: when the applet is unloaded (new page); The browser calls destroy() just before unloading;

6 20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 6 Lecture 16: Java Applets & AWT Applet states and methods Running: the applet is visible to the user. When it becomes visible the browser runs start(). Stopped: when the applet becomes temporarily invisible. The browser runs stop().

7 20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 7 Lecture 16: Java Applets & AWT Applet states and methods init() typically allocates needed resources, gets images/sounds from the server (saves time), etc. start() is usually used to start an animation. stop() stops an animation. destroy() deallocates system resources other than memory.

8 20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 8 Lecture 16: Java Applets & AWT Applet states and methods Applets that provide graphical contents usually redefine the paint() method. The browser will call this method whenever the applet window needs to be updated.

9 20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 9 Lecture 16: Java Applets & AWT Restrictions on Applets Applets contain foreign code. It can be used to attack the client. Certain restrictions on what applets can do. E.g., Applets have very restricted access to the file system. More in the next lecture.

10 20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 10 Lecture 16: Java Applets & AWT Abstract Window Toolkit HTML provides ways to encode standard elements of a web page (checkboxes, labels, text fields, etc). The user can assume that his web page will look similar in different browsers. AWT also platform independent.

11 20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 11 Lecture 16: Java Applets & AWT AWT AWT is a Java package, a collection of reusable objects and methods. AWT provides graphical components that look similar on all platforms running Java. Provides labels, checkboxes, radio buttons, buttons, choices, text fields and areas (same as HTML).

12 20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 12 Lecture 16: Java Applets & AWT AWT But also menus, containers and various ways to align elements in the applet window. Basic graphics supported: you can draw lines, rectangles, ovals, etc (not possible in HTML, unless you load an image). You can have different buttons, not only SUBMIT and CLEAR...

13 20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 13 Lecture 16: Java Applets & AWT Layout Managers HTML doesn’t support many layout choices. Typical HTML elements are laid out in sequence as they are described in the HTML code according to the available space. This corresponds to the flow layout.

14 20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 14 Lecture 16: Java Applets & AWT Layout Managers In HTML you can use a table to align components. You can use the grid layout in an Applet. Border layout is very handy; divides the window in a five areas: “North”, “South”, “East”, “West” and “Center”.

15 20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 15 Lecture 16: Java Applets & AWT Layout Managers Components can be placed in these areas.

16 20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 16 Lecture 16: Java Applets & AWT Layout Managers Grid Bag layout: like the grid layout but you don’t have to specify how may rows and columns you need. Other layouts: card layout a stack of containers with one visible at a time.

17 20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 17 Lecture 16: Java Applets & AWT Containers In addition to Layout Managers, AWT provides containers. Containers are used to organize components into manageable groups. Some containers (Panel) are invisible and they only hold a bunch of components together.

18 20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 18 Lecture 16: Java Applets & AWT Basic Graphics in AWT Basic methods for displaying graphics in the Graphics object. An applet has a Graphics object associated with it. It provided the context which all graphical elements are drawn. It’s the argument of the paint method.

19 20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 19 Lecture 16: Java Applets & AWT Graphics in AWT Graphics provides a coordinate system in the applet window. The origin is in the upper left corner. public void paint(Graphics g) { g.drawLine(0,0,100,100); }

20 20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 20 Lecture 16: Java Applets & AWT Interaction with the user Interaction with the user is mediated by events. An event is an object that AWT creates when a user performs an action such as moving the mouse. The event has to be handled by the Applet code if you wish to react to the action the user performed. You can also ignore the event.

21 20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 21 Lecture 16: Java Applets & AWT Examples of events Events are associated with a component. Different events can be generated for different components. Examples: button pressed, mouse down, mouse dragged over a component, changed the contents of a text field.

22 20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 22 Lecture 16: Java Applets & AWT Handling events Unfortunately, Java 1.1 redefined the event handling model. Will look only at the old one, since it’s easier. You should use the new model in your code. Yet another function to redefine: action(Event e, Object arg)

23 20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 23 Lecture 16: Java Applets & AWT Handling events public boolean action(Event e, Object arg) { if (e.target == button) { buttonClicked(); return true; } return false; } arg contains the parameter of e.

24 20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 24 Lecture 16: Java Applets & AWT Other window toolkits AWT is very simple. It’s possible to use your own, customized window toolkit. Sun provided the Swing components with Java 1.2. New look, customizable, but not all browsers support Java 1.2 yet...


Download ppt "20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 16: Java Applets & AWT Fundamentals of Web Programming."

Similar presentations


Ads by Google