180 likes | 316 Views
A Brief Overview of the Abstract Windowing Toolkit (AWT). Motivation. Typical Frame setup. BorderLayout, FlowLayout, GridLayout. Component class hierarchy User events Graphics contexts Handling mouse events. Motivation.
E N D
A Brief Overview of the Abstract Windowing Toolkit (AWT) • Motivation. • Typical Frame setup. • BorderLayout, FlowLayout, GridLayout. • Component class hierarchy • User events • Graphics contexts • Handling mouse events CSE 341 -- S. Tanimoto Java Abstract Windowing Toolkit
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 JDK1.0 and 1.1, and more complex interface components such as TreeView in JDK 1.2 -- in “Swing”). 4. Permit integration with browsers (Netscape Navigator, Microsoft Internet Explorer).. CSE 341 -- S. Tanimoto Java Abstract Windowing Toolkit
Typical Frame Setup // RecipeViewer.java Steve Tanimoto, 12 April 1999. import java.applet.*; import java.awt.*; import java.awt.event.*; public class RecipeViewer extends Frame implements WindowListener, ItemListener { TextArea recipeTextArea; Choice recipeSelector; public static void main( String [] args) { RecipeViewer rv = new RecipeViewer(); rv.init(); rv.show(); } CSE 341 -- S. Tanimoto Java Abstract Windowing Toolkit
RecipeViewer.java (Continued) public void init() { setSize(300, 200); addWindowListener(this); // for the close box. setLayout( new BorderLayout() ); add(new Label("The Recipe Viewer"),BorderLayout.NORTH); recipeTextArea = new TextArea(100, 20); add(recipeTextArea, BorderLayout.CENTER); recipeSelector = new Choice(); recipeSelector.addItem("Select recipe"); recipeSelector.addItem("Limoncello"); recipeSelector.addItem("Salsa"); add(recipeSelector, BorderLayout.SOUTH); recipeSelector.addItemListener(this); // for the Choice. } CSE 341 -- S. Tanimoto Java Abstract Windowing Toolkit
RecipeViewer.java (Continued) public void itemStateChanged(ItemEvent e) { String item = recipeSelector.getSelectedItem(); if (item.equals("Limoncello")) { recipeTextArea.setText( "To make limoncello, soak the zest of 15 lemons\n" + "for 30 days in 1 liter of 100 proof vodka.\n" + "Mix in 1 liter of cool sugar syrup made by cooking\n" + "1 liter sugar in 1 liter water for 5 min.\n" + "Add 1 more liter vodka, soak for 30 more days, and\n" + "strain out into clean storage bottles."); } else if (item.equals("Salsa")) { recipeTextArea.setText( "To make salsa, wash 3 lbs ripe tomatoes and dice.\n" + "Add 3 minced jalapeno peppers, 1 bunch fresh cilantro\n" + "also minced, 2 minced peeled onions, juice of 2 lemons,\n" + "and 1 teaspoon salt."); } else recipeTextArea.setText("Unknown choice"); } CSE 341 -- S. Tanimoto Java Abstract Windowing Toolkit
RecipeViewer.java (Continued) public void windowClosing(WindowEvent e) { dispose(); System.exit(0); } public void windowClosed(WindowEvent e) {} public void windowOpened(WindowEvent e) {} public void windowIconified(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} public void windowActivated(WindowEvent e) {} public void windowDeactivated(WindowEvent e) {} } CSE 341 -- S. Tanimoto Java Abstract Windowing Toolkit
RecipeViewer.java (Continued) CSE 341 -- S. Tanimoto Java Abstract Windowing Toolkit
BorderLayout NORTH WEST EAST CENTER SOUTH CSE 341 -- S. Tanimoto Java Abstract Windowing Toolkit
FlowLayout setLayout(new FlowLayout(FlowLayout.LEFT, 10, 10)) CSE 341 -- S. Tanimoto Java Abstract Windowing Toolkit
GridLayout setLayout(new GridLayout(3, 2, 5, 5)); CSE 341 -- S. Tanimoto Java Abstract Windowing Toolkit
Component Class Hierarchy AWTEvent Container Panel Applet Button Font Window Frame Canvas FontMetrics TextComponent ScrollPane TextField Object Color List Dialog TextArea Choice Graphics FileDialog Checkbox Component Label Scrollbar LayoutManager CSE 341 -- S. Tanimoto Java Abstract Windowing Toolkit
User Events ActionEvent ContainerEvent AdjustmentEvent FocusEvent MouseEvent EventObject AWTEvent ComponentEvent InputEvent KeyEvent ItemEvent PaintEvent TextEvent WindowEvent CSE 341 -- S. Tanimoto Java Abstract Windowing Toolkit
Graphics Contexts void paint (Graphics g) { int x=100; int y=50; int w=100; int h=50; int x2=300; int y2=150; g.drawString("Hello", x, y); // Draws text g.setFont(new Font("Helvetica", Font.BOLD 20)); g.drawString("Hello again", x, y2); g.setColor(Color.red); g.drawLine(x1, y1, x2, y2); g.drawRect(x, y, w, h); g.fillRect(x, y, w, h); g.drawOval(x, y, w, h); } CSE 341 -- S. Tanimoto Java Abstract Windowing Toolkit
Handling Mouse Events 1. Either define a class that implements one or both of the interfaces MouseListener or MouseMotionListener, or 2. Define a class that subclasses MouseAdapter (and, maybe which implements MouseMotionListener), or 3. Declare that your main class implements MouseListener and/or MouseMotionListener. Then, if you have implemented a new class for handling the mouse, then create an instance of it and call one or both of addMouseListener and/or addMouseMotionListener Otherwise call addMouseListener and/or addMouseMotionListener withthis. CSE 341 -- S. Tanimoto Java Abstract Windowing Toolkit
Example: LineSegments with Movable Endpoints Let’s add interactivity to the LineSegmentCollection 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
Handling Mouse Events (cont) class MyMouseHandler extends MouseAdapter implements MouseMotionListener { public void mousePressed (MouseEvent e) { for (Enumeration theEnum = theSegments.elements(); theEnum.hasMoreElements();) { selectedLS = (LineSegmentD)theEnum.nextElement(); selectedPoint = selectedLS.getHit(e.getX(), e.getY()); if (selectedPoint != null) break; selectedLS = null; } } CSE 341 -- S. Tanimoto Java Abstract Windowing Toolkit
Handling Mouse Events (cont) public void mouseReleased (MouseEvent e) { selectedPoint = null; } public void mouseDragged (MouseEvent e) { if (selectedPoint != null) { selectedLS.updateLocation( selectedPoint, e.getX(), e.getY() ); repaint(); } } public void mouseMoved (MouseEvent e) {} } CSE 341 -- S. Tanimoto Java Abstract Windowing Toolkit
Handling Mouse Events (cont) public class LineSegmentCollectionD extends Applet { Vector theSegments; LSPointD selectedPoint = null; LineSegmentD selectedLS = null; public void init() { MyMouseHandler mmh = new MyMouseHandler(); addMouseListener(mmh); addMouseMotionListener(mmh); theSegments = new Vector(); for (int i = 0; i < 10; i++) { theSegments.addElement(new LineSegmentD(i,this)); } } CSE 341 -- S. Tanimoto Java Abstract Windowing Toolkit