A Brief Overview of the Abstract Windowing Toolkit
A Brief Overview of the Abstract Windowing Toolkit (AWT) • Motivation. • Typical Frame setup. • Border. Layout, Flow. Layout, Grid. Layout. • Component class hierarchy • User events • Graphics contexts • Handling mouse events CSE 341 -- S. Tanimoto Java Abstract Windowing Toolkit 1
Motivation 1. A portable graphics model and window system (Unix, Microsoft Windows, Macintosh). 2. Support interaction in a general way (Mouse events, keyboard events). 3. Provide high-level building blocks (widgets like buttons and canvases in JDK 1. 0 and 1. 1, and more complex interface components such as Tree. View in JDK 1. 2 -- in “Swing”). 4. Permit integration with browsers (Netscape Navigator, Microsoft Internet Explorer). . CSE 341 -- S. Tanimoto Java Abstract Windowing Toolkit 2
Typical Frame Setup // Recipe. Viewer. java Steve Tanimoto, 12 April 1999. import java. applet. *; import java. awt. event. *; public class Recipe. Viewer extends Frame implements Window. Listener, Item. Listener { Text. Area recipe. Text. Area; Choice recipe. Selector; public static void main( String [] args) { Recipe. Viewer rv = new Recipe. Viewer(); rv. init(); rv. show(); } CSE 341 -- S. Tanimoto Java Abstract Windowing Toolkit 3
Recipe. Viewer. java (Continued) public void init() { set. Size(300, 200); add. Window. Listener(this); // for the close box. set. Layout( new Border. Layout() ); add(new Label("The Recipe Viewer"), Border. Layout. NORTH); recipe. Text. Area = new Text. Area(100, 20); add(recipe. Text. Area, Border. Layout. CENTER); recipe. Selector = new Choice(); recipe. Selector. add. Item("Select recipe"); recipe. Selector. add. Item("Limoncello"); recipe. Selector. add. Item("Salsa"); add(recipe. Selector, Border. Layout. SOUTH); recipe. Selector. add. Item. Listener(this); // for the Choice. } CSE 341 -- S. Tanimoto Java Abstract Windowing Toolkit 4
Recipe. Viewer. java (Continued) public void item. State. Changed(Item. Event e) { String item = recipe. Selector. get. Selected. Item(); if (item. equals("Limoncello")) { recipe. Text. Area. set. Text( "To make limoncello, soak the zest of 15 lemonsn" + "for 30 days in 1 liter of 100 proof vodka. n" + "Mix in 1 liter of cool sugar syrup made by cookingn" + "1 liter sugar in 1 liter water for 5 min. n" + "Add 1 more liter vodka, soak for 30 more days, andn" + "strain out into clean storage bottles. "); } else if (item. equals("Salsa")) { recipe. Text. Area. set. Text( "To make salsa, wash 3 lbs ripe tomatoes and dice. n" + "Add 3 minced jalapeno peppers, 1 bunch fresh cilantron" + "also minced, 2 minced peeled onions, juice of 2 lemons, n" + "and 1 teaspoon salt. "); } else recipe. Text. Area. set. Text("Unknown choice"); } CSE 341 -- S. Tanimoto Java Abstract Windowing Toolkit 5
Recipe. Viewer. java (Continued) public void window. Closing(Window. Event e) { dispose(); System. exit(0); } public void window. Closed(Window. Event e) {} public void window. Opened(Window. Event e) {} public void window. Iconified(Window. Event e) {} public void window. Deiconified(Window. Event e) {} public void window. Activated(Window. Event e) {} public void window. Deactivated(Window. Event e) {} } CSE 341 -- S. Tanimoto Java Abstract Windowing Toolkit 6
Recipe. Viewer. java (Continued) CSE 341 -- S. Tanimoto Java Abstract Windowing Toolkit 7
Border. Layout NORTH WEST CENTER EAST SOUTH CSE 341 -- S. Tanimoto Java Abstract Windowing Toolkit 8
Flow. Layout set. Layout(new Flow. Layout(Flow. Layout. LEFT, 10)) CSE 341 -- S. Tanimoto Java Abstract Windowing Toolkit 9
Grid. Layout set. Layout(new Grid. Layout(3, 2, 5, 5)); CSE 341 -- S. Tanimoto Java Abstract Windowing Toolkit 10
Component Class Hierarchy AWTEvent Font Container Panel Button Window Canvas Font. Metrics Object Color Applet Text. Component Frame Text. Field Scroll. Pane Text. Area Dialog List Choice Graphics Checkbox Component Layout. Manager File. Dialog Label Scrollbar CSE 341 -- S. Tanimoto Java Abstract Windowing Toolkit 11
User Events Action. Event Container. Event Adjustment. Event Focus. Event Mouse. Event. Object AWTEvent Component. Event Input. Event Key. Event Item. Event Paint. Event Text. Event Window. Event CSE 341 -- S. Tanimoto Java Abstract Windowing Toolkit 12
Graphics Contexts void paint (Graphics g) { int x=100; int y=50; int w=100; int h=50; int x 2=300; int y 2=150; g. draw. String("Hello", x, y); // Draws text g. set. Font(new Font("Helvetica", Font. BOLD 20)); g. draw. String("Hello again", x, y 2); g. set. Color(Color. red); g. draw. Line(x 1, y 1, x 2, y 2); g. draw. Rect(x, y, w, h); g. fill. Rect(x, y, w, h); g. draw. Oval(x, y, w, h); } CSE 341 -- S. Tanimoto Java Abstract Windowing Toolkit 13
Handling Mouse Events 1. Either define a class that implements one or both of the interfaces Mouse. Listener or Mouse. Motion. Listener, or 2. Define a class that subclasses Mouse. Adapter (and, maybe which implements Mouse. Motion. Listener), or 3. Declare that your main class implements Mouse. Listener and/or Mouse. Motion. Listener. Then, if you have implemented a new class for handling the mouse, then create an instance of it and call one or both of add. Mouse. Listener and/or add. Mouse. Motion. Listener Otherwise call add. Mouse. Listener and/or add. Mouse. Motion. Listener with this. CSE 341 -- S. Tanimoto Java Abstract Windowing Toolkit 14
Example: Line. Segments with Movable Endpoints Let’s add interactivity to the Line. Segment. Collection applet. The user can click on the endpoint of any segment. The user can drag the mouse and move the endpoint. Any intersections with other line segments are updated, too. (Note, this version leaves copies of some intersections until the other line is moved, too. ) CSE 341 -- S. Tanimoto Java Abstract Windowing Toolkit 15
Handling Mouse Events (cont) class My. Mouse. Handler extends Mouse. Adapter implements Mouse. Motion. Listener { public void mouse. Pressed (Mouse. Event e) { for (Enumeration the. Enum = the. Segments. elements(); the. Enum. has. More. Elements(); ) { selected. LS = (Line. Segment. D)the. Enum. next. Element(); selected. Point = selected. LS. get. Hit(e. get. X(), e. get. Y()); if (selected. Point != null) break; selected. LS = null; } } CSE 341 -- S. Tanimoto Java Abstract Windowing Toolkit 16
Handling Mouse Events (cont) public void mouse. Released (Mouse. Event e) { selected. Point = null; } public void mouse. Dragged (Mouse. Event e) { if (selected. Point != null) { selected. LS. update. Location( selected. Point, e. get. X(), e. get. Y() ); repaint(); } } public void mouse. Moved (Mouse. Event e) {} } CSE 341 -- S. Tanimoto Java Abstract Windowing Toolkit 17
Handling Mouse Events (cont) public class Line. Segment. Collection. D extends Applet { Vector the. Segments; LSPoint. D selected. Point = null; Line. Segment. D selected. LS = null; public void init() { My. Mouse. Handler mmh = new My. Mouse. Handler(); add. Mouse. Listener(mmh); add. Mouse. Motion. Listener(mmh); the. Segments = new Vector(); for (int i = 0; i < 10; i++) { the. Segments. add. Element(new Line. Segment. D(i, this)); } } CSE 341 -- S. Tanimoto Java Abstract Windowing Toolkit 18
- Slides: 18