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

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.
Programming in Java; Instructor:John Punin Graphics and Graphical User Interfaces1 Programming in Java Graphics and Graphical User Interfaces.
CSE 341, S. Tanimoto Java-PolyDraw- 1 A Java Case Study: An Applet for Drawing Polygons Motivation -- See a number of AWT features integrated. Get ideas.
Jan Event Handling -1.1 Yangjun Chen Dept. Business Computing University of Winnipeg.
Events and the AWT The objectives of this chapter are: To understand the principles of the Java 1.1 event model To understand how the event model is used.
June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 1 Lecture 9 Object Oriented Programming in Java Advanced Topics Abstract Windowing.
Event Handling. In this class we will cover: Basics of event handling The AWT event hierarchy Semantic and low-level events in the AWT.
Gui Interfaces a la Swing Up to speed with Swing by Steven Gutz is a good source It don’t mean a thing if it ain’t got that swing Duke Ellington.
Events ● Anything that happens in a GUI is an event. For example: – User clicks a button, presses return when typing text, or chooses a menu item ( ActionEvent.
Event Handling n Events: an event is an object that describes a state change in a source. n Event Sources: A source is an object that generates an event.
Intermediate Java1 An example that uses inner classes Week Four Continued.
Graphical User Interfaces (GUI) Abstract Windows Toolkit (AWT): java.awt GUI elements: Primitive Button, Label, Checkbox, Scrollbar, etc. Container Panel,
1 lecture 12Lecture 13 Event Handling (cont.) Overview  Handling Window Events.  Event Adapters Revisited.  Introduction to Components and Containers.
Java GUI building with the AWT. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many.
AWT Components (Chapter 9) Java Certification Study Group January 21, 1999 Mark Roth.
Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
Java Programming 1 Java Programming II Events, AWT, and Swing.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
Cs884(Prasad)java12AWT1 Abstract Windowing Toolkit Support for Graphical User Interface (Event-driven programming)
Graphical User Interface Components: Part 1 Chapter 11.
– 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.
1 Unit 5 GUI Aum Amriteshwaryai Namah. 2 Overview Shall learn how to reuse the graphics classes provided by Java for constructing Graphical User Interface.
MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 1 Lecture 4 – Event Handling r Painting r Event types r Catching different event types.
CS GUI Frameworks CS 3331 Fall CS 3331 Outline MVC Model GUI components (widgets) Layout managers Handling events.
CS102 – GUI AWT & Swing Components & Containers, Layout Managers, Events & Listeners MVC design pattern. David Davenport.
Pravin Yannawar, DOCS, NMU Jalgaon. Basic Java : Event handling in AWT and Swing 2 Objectives of This Session Explain the Event handling mechanism & demonstrate.
Computer Science [3] Java Programming II - Laboratory Course Lab 4: Common GUI Event Types and Listener Interfaces Layout Mangers Faculty of Engineering.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
Event Handling. 2 GUIs are event driven –Generate events when user interacts with GUI e.g., moving mouse, pressing button, typing in text field, etc.
The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include: a rich set of user interface components; a.
Universidad Nacional de Colombia Facultad de Ingeniería Departamento de Sistemas ertificación en AVA.
Object Oriented Programming.  Interface  Event Handling.
Event Handling. The signals that a program receives from the operating system as a result of the actions are called events. A window based program is.
Ch13 Creating windows and applets. Short overview AWT (Abstract Windowing Toolkit) Early Java development used graphic classesEarly Java development used.
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
Events in JAVA Needed for Applets and GUI based programs. Applet uses GUI to interact with user. Most of the events are generated by Mouse, Keyboard.
Events (Chapter 11) Java Certification Study Group January 25, 1999 Mark Roth.
CSI 3125, Preliminaries, page 1 Event Handling. CSI 3125, Preliminaries, page 2 Event Handling An Event Change in the state of an object is known as event.
Event Handling and Listeners in SWING The practice of event handling.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
1 Lecture 8: User Interface Components with Swing.
Sep 181 Example Program DemoTranslateEnglishGUI.java.
Java Programming (By Rama Bhadra Rao M) You can Trace me in Day 5
AWT Vs SWING. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many applications Uses.
GUI Programming in Java Hao Jiang Boston College April, 2009.
CSC 205 Programming II Lecture 5 AWT - I.
Welcome To java
Programming in Java, 2e Sachin Malhotra Saurabh Choudhary.
CHAPTER Reacting to the user.
Chapter 12 Event-Driven Programming
Programming in Java Sachin Malhotra, Chairperson, PGDM-IT, IMS Ghaziabad Saurabh Chaudhary, Dean, Academics, IMS Ghaziabad.
Aum Amriteshwaryai Namah
Event Handling Chapter 2 Objectives
Layout Managers Layout Manager—an object that decides how components will be arranged in a container Some types of layout managers: BorderLayout FlowLayout.
Advanced GUIs II CS Lecture
CSE Software Engineering Fall 1999 Updated by J. Brown
AWT.
Introduction to Computing Using Java
A Brief Overview of the Abstract Windowing Toolkit (AWT)
A Brief Overview of the Abstract Windowing Toolkit (AWT)
A Brief Overview of the Abstract Windowing Toolkit (AWT)
Chapter 10 Getting Started with Graphics Programming
Programming Graphical User Interface (GUI)
GUI building with the AWT
Advanced GUIs II CS Lecture
Presentation transcript:

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

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 in “Swing”). 4. Permit integration with browsers (Netscape Navigator, Microsoft Internet Explorer)..

CSE 341, S. Tanimoto Java-AWT- 3 Typical Frame Setup // RecipeViewer.java Steve Tanimoto, 12 April 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-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. }

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

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

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

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

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

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

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

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

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

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.

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

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

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

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