Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 341, S. Tanimoto Java-AWT- 1 A Brief Overview of the Abstract Windowing Toolkit (AWT) Motivation. Typical Frame setup. BorderLayout, FlowLayout, GridLayout.

Similar presentations


Presentation on theme: "CSE 341, S. Tanimoto Java-AWT- 1 A Brief Overview of the Abstract Windowing Toolkit (AWT) Motivation. Typical Frame setup. BorderLayout, FlowLayout, GridLayout."— Presentation transcript:

1 CSE 341, S. Tanimoto Java-AWT- 1 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

2 CSE 341, S. Tanimoto Java-AWT- 2 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)..

3 CSE 341, S. Tanimoto Java-AWT- 3 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(); }

4 CSE 341, S. Tanimoto Java-AWT- 4 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. }

5 CSE 341, S. Tanimoto Java-AWT- 5 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"); }

6 CSE 341, S. Tanimoto Java-AWT- 6 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) {} }

7 CSE 341, S. Tanimoto Java-AWT- 7 RecipeViewer.java (Continued)

8 CSE 341, S. Tanimoto Java-AWT- 8 BorderLayout WESTEAST SOUTH NORTH CENTER

9 CSE 341, S. Tanimoto Java-AWT- 9 FlowLayout setLayout(new FlowLayout(FlowLayout.LEFT, 10, 10))

10 CSE 341, S. Tanimoto Java-AWT- 10 GridLayout setLayout(new GridLayout(3, 2, 5, 5));

11 CSE 341, S. Tanimoto Java-AWT- 11 Component Class Hierarchy Object Font FontMetrics Color Graphics Component LayoutManager AWTEvent Container Button Canvas TextComponent List Choice Checkbox Label Scrollbar Panel Window TextField TextArea Applet Frame Dialog FileDialog ScrollPane

12 CSE 341, S. Tanimoto Java-AWT- 12 User Events EventObjectAWTEvent ComponentEvent KeyEvent ItemEvent TextEvent ActionEvent AdjustmentEvent InputEvent PaintEvent WindowEvent ContainerEvent FocusEvent MouseEvent

13 CSE 341, S. Tanimoto Java-AWT- 13 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); }

14 CSE 341, S. Tanimoto Java-AWT- 14 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 with this.

15 CSE 341, S. Tanimoto Java-AWT- 15 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.)

16 CSE 341, S. Tanimoto Java-AWT- 16 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; }

17 CSE 341, S. Tanimoto Java-AWT- 17 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) {} }

18 CSE 341, S. Tanimoto Java-AWT- 18 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)); }


Download ppt "CSE 341, S. Tanimoto Java-AWT- 1 A Brief Overview of the Abstract Windowing Toolkit (AWT) Motivation. Typical Frame setup. BorderLayout, FlowLayout, GridLayout."

Similar presentations


Ads by Google