© The McGraw-Hill Companies, 2006 Chapter 10 Graphics and event- driven programs.

Slides:



Advertisements
Similar presentations
Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Advertisements

Graphical User Interfaces
Graphics and event-driven programs Learning objectives By the end of this lecture you should be able to: identify and use some of the common components.
CS18000: Problem Solving and Object-Oriented Programming.
Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
Graphic User Interfaces Layout Managers Event Handling.
Problem Solving 6 GUIs and Event Handling ICS-201 Introduction to Computing II Semester 071.
Graphical User Interface Bonus slides Interaction Between Components & Drawing.
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.
Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven.
GUI and Swing, part 2 The illustrated edition. Scroll bars As we have previously seen, a JTextArea has a fixed size, but the amount of text that can be.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Java Programming, 3e Concepts and Techniques Chapter 5 Arrays, Loops, and Layout Managers Using External Classes.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
Chapter 121 Window Interfaces Using Swing Chapter 12.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Chapter Day 9. © 2007 Pearson Addison-Wesley. All rights reserved4-2 Agenda Day 8 Questions from last Class?? Problem set 2 posted  10 programs from.
MSc IT Programming Methodology (2). Which printer [a] LPT1 or [b] LPT2?: How many copies do you wish to print?: [P]rint now or [C]ancel?: Print all pages.
1 Class 8. 2 Chapter Objectives Use Swing components to build the GUI for a Swing program Implement an ActionListener to handle events Add interface components.
Chapter 6: Graphical User Interface (GUI) and Object-Oriented Design (OOD) J ava P rogramming: Program Design Including Data Structures Program Design.
Java Programming Chapter 10 Graphical User Interfaces.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 13 : Swing I King Fahd University of Petroleum & Minerals College of Computer Science.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
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.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
Chapter 12 1 TOPIC 13B l Buttons and Action Listeners Window Interfaces Using Swing Objects.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
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.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Java Programming Applets. Topics Write an HTML document to host an applet Understand simple applets Use Labels with simple AWT applets Write a simple.
The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include: a rich set of user interface components; a.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 5 Objectives  Learn about basic GUI components.  Explore how the GUI.
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
Review_6 AWT, Swing, ActionListener, and Graphics.
Ajmer Singh PGT(IP) JAVA IDE Programming - I. Ajmer Singh PGT(IP) GUI (Graphical User Interface) It is an interface that uses a graphic entities along.
1 Event Driven Programs with a Graphical User Interface Rick Mercer.
Chapter 14: Introduction to Swing Components. Objectives Understand Swing components Use the JFrame class Use the JLabel class Use a layout manager Extend.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Lesson 28: More on the GUI button, frame and actions.
1 Lecture 8: User Interface Components with Swing.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
GUIs & Event-Driven Programming Chapter 11 Review.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
GUIs and Events Rick Mercer.
CompSci 230 S Programming Techniques
A First Look at GUI Applications
“Form Ever Follows Function” Louis Henri Sullivan
Java Swing.
CHAPTER 7 & 8 REVIEW QUESTIONS
Graphical User Interface (pronounced "gooey")
Java Programming: From Problem Analysis to Program Design,
Ellen Walker Hiram College
Graphical User Interface
Presentation transcript:

© The McGraw-Hill Companies, 2006 Chapter 10 Graphics and event- driven programs

© The McGraw-Hill Companies, 2006 The Swing package in the earlier versions of Java, graphical programming was achieved exclusively by making use of a package known as the Abstract Window Toolkit (AWT).; nowadays, graphics in Java is achieved via the Swing package; the Swing classes build on the AWT classes to provide enhanced functionality and appearance;

© The McGraw-Hill Companies, 2006 The SmileyFace class

© The McGraw-Hill Companies, 2006 Analysis of the SmileyFace class The import clauses the first of these imports the standard Java Abstract Window Toolkit; although we are going to be using Swing, we still need many of the AWT classes for drawing and painting; the next clause imports the Swing package - the Swing classes come in a library called Javax (Java eXtension).

© The McGraw-Hill Companies, 2006 The class header: a JFrame is a Swing component that forms the visible window in which the graphic is displayed; by extending the JFrame class, we are making our SmileyFace class a kind of JFrame. The constructor

© The McGraw-Hill Companies, 2006 The paint method this special method is a method of a basic graphics class called Component, of which JFrame is an extension; we don't need to call this method anywhere because when the component becomes visible, the paint method is automatically called; when this happens, an object of a core Java class called Graphics (which comes with the AWT package) is automatically sent in to this method.

© The McGraw-Hill Companies, 2006 a Graphics object many useful methods. the drawOval method takes four integer parameters; referring to these as x, y, l, h, the oval that gets drawn fits into an imaginary rectangle that starts at position (x,y), and is l pixels long and h pixels high: because we want a circle, we have made the values of l and h equal.

© The McGraw-Hill Companies, 2006 the next line draws the mouth: this method requires six parameters, all integers - we shall call them x, y, l, h, , . the first four define an imaginary rectangle as above; the arc is drawn so that its centre is the centre of this rectangle, as shown below; the next two parameters,  and , represent angles; the first, , is the start angle – measured from an imaginary horizontal line pointing to the "quarter-past-three" position (representing zero degrees); the next, , is the finish angle; if  is positive then the arc is drawn by rotating from the start position in an anti-clockwise direction; if it is negative we rotate in a clockwise direction.

© The McGraw-Hill Companies, 2006

Event-handling in Java : The ChangingFace class we are going to change our SmileyFace class into a ChangingFace class that can smile or frown on the click of a button:

© The McGraw-Hill Companies, 2006 Analysis of the ChangingFace class the buttons are objects of the Swing component JButton; they are declared as attributes of the class, and initialized at the same time: as can be seen the JButton class has a constructor that allows us to create the buttons with the required caption by sending in this caption as a parameter.

© The McGraw-Hill Companies, 2006 The constructor of the ChangingFace class after setting the title, the buttons are added to the frame; a container, like the ChangingFace (which extends JFrame), always has a layout policy attached to it; this policy determines the way in which components are added to it; the default policy for a JFrame is called BorderLayout, which will be explained later; what we want to use a policy called FlowLayout, which means: –the components are just placed in the order in which they were added; –when one row fills up then the next row starts to be filled. we have changed the layout policy with this instruction:

© The McGraw-Hill Companies, 2006 Event-handling note the additional import statement: the event package contains all the classes (such as ActionListener below) that we need for handling events; The class header the ActionListener class is a special class called an interface; these classes contain only abstract methods which; ActionListener insists that we code a method called actionPerformed to handle our events; implementing an interface is very like extending a class; any class that implements ActionListioner must have an actionPerformed method.

© The McGraw-Hill Companies, 2006 note the new attribute: this will be set to true when the Smile button is pressed and false when the Frown button is pressed; the face will then be repainted with the appropriate expression; we have initialized it to true, so that the face will start off happy.

© The McGraw-Hill Companies, 2006 consider these two lines of the constructor when we use the keyword implements with an interface class like ActionListener we achieve an effect very similar to that achieved by inheritance; the JButton class has a method called addActionListener that receives an object of the ActionListener class as a parameter; since our class is now a kind of ActionListener we can send it to the addActionListener method of a JButton; the ActionListener that we send in is the class where the code for handling the event is to be found; by adding an ActionListener to an object such as a button, we are making that object "listen" for a mouse-click; the program will then respond to this event - the mouse-click - by taking some action.

© The McGraw-Hill Companies, 2006 The paint method we begin by calling the paint method of the superclass, JFrame; after drawing the face and the eyes as before we have two possibilities for the mouth: –if isHappy is true, a smiling mouth is drawn; –if not a frowning mouth is drawn. by changing the very last parameter of the drawArc method from a negative value to a positive value the arc will be drawn anti-clockwise instead of clockwise; this will make the mouth frown instead of smile; therefore if the isHappy attribute is set to true the mouth will smile – if not it will frown! note that we have moved everything down in order to accommodate the buttons.

© The McGraw-Hill Companies, 2006 The event-handler

© The McGraw-Hill Companies, 2006 we determine what happens when the mouse-button is clicked by coding the actionPerformed method that is required by the ActionListener interface; when the mouse is clicked, this method is automatically sent an object of the class ActionEvent; this class has a method called getSource that returns the name of the object that was clicked on; we use this method in the condition of the if statement to find out which button was clicked; if it was the happyButton that was pressed, isHappy is set to true and a special method – repaint – is called; this causes the paint method to be called again so that the screen is repainted; the sadButton works in the same way, but sets isHappy to false.

© The McGraw-Hill Companies, 2006 An interactive graphics class the class below (PushMe) allows the user to input information via a graphics screen; it allows the user to enter some text and then, by clicking on a button, to see the text that was entered displayed in the graphics window:

© The McGraw-Hill Companies, 2006 Analysis of the PushMe class there are three components involved here; we have declared them all as attributes of the class and initialized them at the same time: the first of the above three components is a JTextField; we have used the fact that it has a constructor that accepts an integer value (in this case 15) that allows you to specify the length of the text field to be displayed (in terms of columns). next we declare the JButton, which, as before, we have initialized with the required caption ("please push me"); finally we have declared and instantiated a JLabel.

© The McGraw-Hill Companies, 2006 The constructor

© The McGraw-Hill Companies, 2006 The event-handling routine we use the getText method of the JTextField class to read the current "value" of the text in myTextField; we then use the setText method of the JLabel class to "transfer" it to myLabel; notice that since there is only one component that is able to listen, it is not necessary to determine the source of the mouse-click.

© The McGraw-Hill Companies, 2006 A graphical user interface (GUI) for the Oblong class we are now in a position to write graphical user interfaces for our classes; the example below does this for the Oblong class.

© The McGraw-Hill Companies, 2006 Analysis of the OblongGui class the first attribute is an Oblong object, myOblong; this is initialized as a new Oblong with a length and height of zero (since the user hasn't entered anything yet): after this we declare the graphics components; the only new one the JTextArea, which is the large text area where the area and perimeter of the oblong are displayed it has a constructor that allows you to fix the size by entering values for the number of rows and columns.

© The McGraw-Hill Companies, 2006 The event-handling routine for the calcButton the two local variables, lengthEntered and heightEntered, will hold the values entered by the user; these values are read (as Strings) using the getText method of TextField; we check that these are not of length zero (that is, that something has been entered) ; if one of the fields is empty we display an error message; otherwise, we use the setLength and setHeight methods of Oblong to set the length and the height of myOblong to the values entered. these methods expect to receive doubles so we must convert from Strings; to do this we use the parseDouble method of the Double class:

© The McGraw-Hill Companies, 2006 had we wanted to convert them to ints, we would have used the parseInt method of the Integer class. if you want to do this the other way round and convert a double or an int to a String you can do so simply by concatenating it onto an empty String. For example or: or even:

© The McGraw-Hill Companies, 2006 A metric converter

© The McGraw-Hill Companies, 2006 Layout policies The FlowLayout manager

© The McGraw-Hill Companies, 2006 The BorderLayout manager This is the default policy for JFrame;

© The McGraw-Hill Companies, 2006 the MetricConverter class uses a combination of BorderLayout and FlowLayout; some components (for example JPanel) have FlowLayout as their default layout policy and some (for example JFrame) have BorderLayout; if you want to change the policy of such a component - say myPanel - to BorderLayout you would do so as follows: the MetricConverter class uses a combination of BorderLayout and FlowLayout; some components (for example JPanel) have FlowLayout as their default layout policy and some (for example JFrame) have BorderLayout; if you want to change the policy of such a component - say myPanel - to BorderLayout you would do so as follows: the following line would then place a component called myButton in the North region: there are a number of other layout managers that can be used such as GridLayout and CardLayout; these will be introduced in the second semester.

© The McGraw-Hill Companies, 2006