Chapter 13 Java AWT – Part II (Optional) Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,

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.
Graphic User Interfaces Layout Managers Event Handling.
CMSC 341 Building Java GUIs. 09/26/2007 CMSC 341 GUI 2 Why Java GUI Development? Course is about Data Structures, not GUIs. We are giving you the opportunity.
Jan AWT Widgets Yangjun Chen Dept. Business Computing University of Winnipeg.
CS 178: Programming with Multimedia Objects Aditya P. Mathur Professor of Computer Sciences Purdue University, West Lafayette Sept 9, 2004 Last update:
June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 1 Lecture 9 Object Oriented Programming in Java Advanced Topics Abstract Windowing.
Java Programming, 3e Concepts and Techniques Chapter 5 Arrays, Loops, and Layout Managers Using External Classes.
Lecture 24 Applets. Introduction to Applets Applets should NOT have main method but rather init, stop, paint etc They should be run through javac compiler.
CS102--Object Oriented Programming Lecture 19: – The Swing Package (II) Copyright © 2008 Xiaoyan Li.
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Java GUI building with the AWT. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many.
Java Programming Chapter 10 Graphical User Interfaces.
20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 16: Java Applets & AWT Fundamentals of Web Programming.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
MSc Workshop - © S. Kamin, U.Reddy Lect 5 – GUI Prog - 1 Lecture 5 – GUI Programming r Inner classes  this and super r Layout r Reading: m.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Chapter 11 Java AWT Part I: Mouse Events (Optional) Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin,
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 GUI building with Swing. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
C13a, AWT Create, display, facilitate user interaction with window objects software framework: a way of structuring generic solutions to common problems.
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.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
Copyright © 2002, Systems and Computer Engineering, Carleton University c-Gui3.ppt * Object-Oriented Software Development Part 18-c Building.
Timer class and inner classes. Processing timer events Timer is part of javax.swing helps manage activity over time Use it to set up a timer to generate.
Layout Managers Arranges and lays out the GUI components on a container.
Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
Java Programming Applets. Topics Write an HTML document to host an applet Understand simple applets Use Labels with simple AWT applets Write a simple.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
University of Limerick1 Software Architecture Java Layout Managers.
The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include: a rich set of user interface components; a.
Computer Programming with JAVA Chapter 7. Event-Driven Programming Using the AWT Event-Driven Programming GUIs and the AWT Simple Window Interfaces Components,
Chapter 7Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 7 l Event-Driven Programming »GUIs and the AWT l Simple Window.
SEEM3460 Tutorial GUI in Java. Some Basic GUI Terms Component (Control in some languages) the basic GUI unit something visible something that user can.
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
Csc Basic Graphical User Interface (GUI) Components.
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
Graphical User Interfaces Tonga Institute of Higher Education.
Chapter 14: Introduction to Swing Components. Objectives Understand Swing components Use the JFrame class Use the JLabel class Use a layout manager Extend.
Graphical User Interfaces (GUI). PART ONE About GUI’s.
Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 12.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Graphical User Interface (GUI)
Applets. 9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L12: Applets Slide 2 Applets Usually.
Introduction to GUI in 1 Graphical User Interface 3 Nouf Almunyif.
CSC 205 Programming II Lecture 7 AWT – Event Handling & Layout.
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
AWT Vs SWING. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many applications Uses.
CSC 205 Programming II Lecture 5 AWT - I.
GUI building with the AWT
Lecture 09 Applets.
Ellen Walker Hiram College
Chap 7. Building Java Graphical User Interfaces
Chapter 13: Advanced GUIs and Graphics
Graphical User Interfaces -- Introduction
JAVA AWT.
AWT Components and Containers
GUI building with the AWT
GUI building with the AWT
Advanced GUIs and Graphics
Graphical User Interface
Presentation transcript:

Chapter 13 Java AWT – Part II (Optional) Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E. Reingold

Chapter Preview In this chapter we will: discuss the use of Java in animating World Wide Web applications show how to write Java applets introduce Java graphical components (e.g. Textfields, Buttons, and Labels) show how Java programs can be made to react to user actions on Java graphical components show how Java applications can be run from inside web browsers

Frames A frame is a window with a title bar and a border The Frame class is a subclass of Container class Container class objects may have other components (e.g. Buttons) added to them using the add method A typical Java GUI will create and display one or more frames To make a frame visible the message setVisbible(true) must be sent to the frame

Layout Managers Governs how components are arranged inside a Container object The Container method setLayout allows the programmer to specify which layout manager (e.g. FlowLayout ) is desired for a particular container object The size of a Container object should be set explicitly by the programmer as well Can use pack() to set size to accommodate preferred sizes of components)

Frame Example public class TwoButtons { Frame f; Button redButton, blueButton; public TwoButttons() { f = new Frame(“Two Buttons Frame”); redButton = new Button(“Red”); blueButton = new Button(“Blue”); f.setLayout(new Flowlayout()); f.add(redButton); f.add(blueButton); f.pack(); f.setVisible(true); }

UML Notation The filled diamond represents composition This shows that the class TwoButtons contains a class Frame which contains a class Buttons A good UML diagram only shows the classes and associations that are important to understanding the architecture This UML diagram does not include super classes like Component or Container

Using Inheritance It is possible to use inheritance to allow TwoButtons to become a frame in its own right Note that the UML diagram will show TwoButtons as a subclass of Frame You will need to use super() to set the frame title

Frame Inheritance Example public class TwoButtons extends Frame { Button redButton, blueButton; public TwoButttons() { super(“Two Buttons Frame”); redButton = new Button(“Red”); blueButton = new Button(“Blue”); f.setLayout(new Flowlayout()); f.add(redButton); f.add(blueButton); f.pack(); f.setVisible(true); }

Other Simple Java Components Label –contains some text that can be set using a constructor or the method setLabel TextField –a box into which the user can type text –text can be edited (backspace, delete, etc.) –text can be retrieved using getText() –text can be set using setText()

Java AWT Event Model Including reactive program components involves: 1.Having the class header declare itself as implementing the ActionListener interface 2.Typically in the class constructor the class instance registers itself as being interested in listening for events from a newly created component 3.One method (e.g. actionPerformed ) of the ActiveListener interface is defined

Frame Inheritance Example public class classname extends Frame implements ActionListener { … Button buttonname; … public classname() { … buttonname = new Button(“button label”); … add(buttonname); … buttonname.ActionListener(this); … } … public void actionPerformed(ActionEvent e) { … what to do when button is pushed … }

UML Notation for Button Program Abstract class indicated by italicizing the class name Implements association (concrete class) indicated by line with a closed arrowhead UML stereotype (constructors, accessor, interface implementation methods) indicated by enclosing a descriptive word inside guillements >

Closing the Window You must implement the WindowListener and its seven required methods windowActivated, windowClosed, windowClosing, windowDeactivated, windowIconified, windowDeiconified, windowOpened This is usually done in a programmer defined class like ClosableFrame and all classes needing these services would extend ClosableFrame

Using Conditionals with Reactive Components public void actionPerformed(ActionEvent e) { double fahr, cent; // check for user input in tFahr TextField if (e.getSource() == tFahr) { fahr = new Double(tFahr.getText()),doubleValue(); cent = 5.0 * (fahr – 32.0) / 9.0; tCent.setText(cent + “”); // update tCent TextField } else { cent = new Double(tCent.getText()),doubleValue(); fahr = 9.0 * cent / ; tFahr.setText(fahr + “”); }

Checkboxes Used to allow user to select one or more items Always has a label Program often needs to react to the user selection(s) Declaration example: Checkbox powerBrakes = new Checkbox(“Power Brakes”); Checkbox powerSteering = new Checkbox(“Power Steering”); Checkbox ac = new Checkbox(“Air Conditioning”); … add(powerBrakes); add(powerSteering); add(ac);

Programming Checkboxes Program is declared with implements ItemListener Checkbox is registered by calling addItemListener Event is handled using itemStateChanged argument type ItemEvent The ItemEvent argument is used to tell which item triggered the event by calling getSource

Radio Buttons A group of Checkboxes in which only one item can be selected at a time Implemented using a Java CheckboxGroup Items are declared initially as selected (true) or unselected (false) Example: CheckboxGroup gender; Checkbox maleCheck = new Checkbox(“Male”, gender, true); Checkbox femaleCheck = new Checkbox(“Female”, gender, true);

Processing Radio Buttons public void compute() { … boolean female = (gender.getSelectedCheckbox() == femaleCheck); … if ((bodyMassIndex > 27.8) || (female && (bodyMassIndex > 27.3)) lHigh.setText(“This is considered high”); else 1High.setText(“this is not considered high”); }

Drawing in a Frame To draw in a Frame you need to the override the frame’s paint method: public void paint(Graphics g) Graphics objects are defined by the Java runtime system and are used for drawing operations The Frame should be considered to be a grid with upper left coordinates (0,0) and positive coordinates (x,y) for the lower right

Typical Drawing Code public class MyDrawing extends ClosableFrame { … public MyDrawing() { … } … public void paint(Graphics g) { … g.drawLine(…); … g.drawImage(…); … }

Repaint and Update The paint method requires an argument and actionPerformed does not know what to supply when called The method repaint will clear the Frame and then call paint along with supplying the needed missing argument You can prevent clearing the Frame when using repaint by overriding the method update before calling repaint

Panel and Canvas Classes The Panel class is container subclass that is used to reserve a rectangular portion of a Frame to place other components The Canvas class is not a Container subclass, but does allow you to reserve a portion of a Frame to draw in

Comparing Layout Managers FlowLayout –Default frame layout –Components are placed on new line only when needed GridLayout –Frame is declared as a grid of fixed size (e.g. two rows and three columns) –Components are placed in the grid left to right and top to bottom BorderLayout –Frame is divided into north, south, east, west, and center –Components are placed by the programmer in the desired location using the add method

Typical GUI Frame Panel p1 = new Panel(); p1.setLayout(new GridLayout(2,1)); p1.add(component); Panel p2 = new Panel(); p2.setLayout(…); p2.add(component); … setLayout(new BorderLayout()); add(“North”, p1); add(“Center”, p2);

Scrollbars The leftmost position of a horizontal scrollbar corresponds to some integer value and the rightmost position corresponds to a larger integer value Scrollbars can be displayed vertically User movement options –Clicking either end button causes bubble to move in unit increments –Clicking the are between bubble and end button causes movement in 10 unit increments –Clicking and dragging the bubble in the desired direction

Hypertext Markup Language HTML – Skeleton Page Title Text and graphics to display in browser

HTML Text Displays Displays a centered heading in browser window Naploean I To identify text paragraphs to be displayed In my spare time I like to do origami and spend quiet evenings at home with my wife Josephine.

HTML Using Pictures and Links Displays a centered picture To display a text link to another web page Josephine To display a picture link to another web page

Applet Tag and HTML To include a Java applet as part of an HTML document Java applet shell for BodyMass.class import java.applet.*; public class BodyMass extends Applet { … public_html void start() { new BodyMass(); }

Transforming a Java Program into an Applet 1.Set the size you want the applet to be in the HTML file 2.Add the import statement to the Java code java.applet.*; 3.Change header from extends Frame to extends Applet 4.Change constructor heading to public void start(); 5.Eliminate any Java code that is meaningless in applets