Object-Oriented Software Engineering PersonGui (Mark 2) Case Study.

Slides:



Advertisements
Similar presentations
2D Graphics Drawing Things. Graphics In your GUI, you might want to draw graphics E.g. draw lines, circles, shapes, draw strings etc The Graphics class.
Advertisements

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.
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
Graphical User Interface (GUI) Programming III. Lecture Objectives Exploring more GUI programming elements in Java Using labels in GUIs Using colors to.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Chapter 7 Event-Driven Programming and Basic GUI Objects.
Object-Oriented Software Engineering Java with added Swing.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Scott Grissom, copyright 2006Ch 11: GUI Slide 1 Graphical User Interfaces (Ch 11) Careful design of a graphical user interface is key to a viable software.
GUI and Event-Driven Programming Part 2. Event Handling An action involving a GUI object, such as clicking a button, is called an event. The mechanism.
CPSC150 Week 12 Graphical User Interfaces Chapter 11.
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.
More on Creating GUIs in Java using Swing David Meredith Aalborg University.
Graphic User Interfaces Part 1. Typical GUI Screen from Microsoft Word What GUI “components” can you see? –Menus? Buttons? Labels? What else? –Anything.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
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.
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,
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal
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,
More Event Handling Adapters Anonymous Listeners Pop menus Validating User Input.
CSE 219 Patterns in Programming More Design Patterns.
Java GUI’s are event driven, meaning they generate events when the user interacts with the program. Typical events are moving the mouse, clicking a mouse.
Chapter 9: Visual Programming Basics Object-Oriented Program Development Using Java: A Class-Centered Approach.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Swing GUI Components You can create graphics components to place on your applet using classes available in the Swing package ( javax.swing ) Class names.
COMP 321 Week 2. Outline Event-Driven Programming Events, Event Sources, Event Listeners Button and Timer Events Mouse Events, Adapters.
An Introduction to Programming and Object Oriented Design using Java 3 rd Edition. Dec 2007 Jaime Niño Frederick Hosch Chapter 18 Integrating user interface.
Concurrent Programming and Threads Threads Blocking a User Interface.
Graphical User Interfaces (Part 2) 1. View  view  presents the user with a sensory (visual, audio, haptic) representation of the model state  a user.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
CompSci Event Handling. CompSci Event Handling The Plan  Sequential (Single Thread) Model  Event Model  Making the GUI interactive  Examples.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 10: Event Handling 1 Event Handling.
Object-Oriented Programming (Java), Unit 28 Kirk Scott 1.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 10: Event Handling 1 Chapter 10 Event Handling.
Object-Oriented Software Engineering Using Threads and simple Animation.
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.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
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,
Mouse Events GUI. Types of Events  Below, are some of the many kinds of events, swing components generate. Act causing EventListener Type User clicks.
Menus and Toolbars CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
View  view  presents the user with a sensory (visual, audio, haptic) representation of the model state  a user interface element (the user interface.
UQC117S2 Graphics Programming Lecture 2 Event Handling Program as a sequence of instructions Event -driven program Need to detect the event and carry out.
Menus Pull-downs and popups. Tooltips –a ToolTip is a context-sensitive text string that is displayed in a popup window when the mouse rests over a particular.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 Event-Driven Programming and Basic GUI Objects.
Object-Oriented Software Engineering
GUIs and Events Rick Mercer.
CompSci 230 S Programming Techniques
Object-Oriented Programming (Java), Unit 22
A First Look at GUI Applications
GUI III IS
Object-Oriented Programming (Java), Unit 23
Graphical User Interface (GUI) Programming III
Chapter 13: Advanced GUIs and Graphics
Object-Oriented Software Engineering
CS288 Lab Exercise 3.
Constructors, GUI’s(Using Swing) and ActionListner
Advanced GUIs and Graphics
Presentation transcript:

Object-Oriented Software Engineering PersonGui (Mark 2) Case Study

UniS Contents PersonGui Mark 2 Case Study Example Overview Use Cases Class Diagram Popup Listeners Sequence Diagrams for Popup Listeners Internal Class Definitions Mouse Adaptors Dynamics of Mouse Events

UniS PersonGui (Mark 2) Mark 2 will be a fully functional GUI

UniS Use Cases Define AgeDefine Height user Define First Name Define Last Name Display Attributes in seperate JFrame Choose Image for Person > Choose Background Colour > Choose Font > Choose GUI Display Settings

UniS PersonGui Mark 2 PersonGui PersonMaker Person > ActionListener JPanel JFrame JScrollPane JTextField JToolBar PopupListener > MouseAdapter * JTextArea 1 Depends on relation E.g. local variables of this class type

UniS The PopupListener Class In this example we choose to implement most of the functionality with a pop-up menu. This is triggered by the right mouse button. We will implement this as an internal class to PersonGui. PopupListener class instance

UniS Create and Show GUI createAndShowGUI() Recall that all our Java GUI-s appear on screen due to the execution of the method. This creates a JFrame creates a new content pane, an instance of PersonGui attaches the content pane to the JFrame When PersonGui is created, it creates an instance of the PopupListener and attaches the menu options to it.

UniS PersonGui Sequence Diagram :PersonGui :Event Dispatcherframe:JFrame createAndShowGUI > Note: It is not strictly correct to say the Event Dispatcher executes the method. Rather is creates a separate object that executes the code. For simplicity we will ignore this part. To reduce clutter not showing dashed return arrow for method calls

UniS createAndShowGUI ( ) (cntd) :Event Dispatcher newContentPane :PersonGui > frame:JFrame setContentPane(newContentPane) createPopupMenu( ) pack( ) setVisible(true)

UniS createPopupMenu( ) sequence diagram newContentPane :PersonGui popup :JPopupMenu > menuItem :JMenuItem > addActionListener(this) add(menuItem) loop This gives abstract description of adding menu items within a loop. In the implementation there is no loop each menu item has separate code.

UniS createPopupMenu( ) sequence diagram (ctd) newContentPane :PersonGui submenu :JMenu > menuItem :JMenuItem > addActionListener(this) add(menuItem) loop popup :JPopupMenu

UniS createPopupMenu( ) sequence diagram (ctd) newContentPane :PersonGui textArea :JTextArea popup :JPopupMenu add(submenu) addMouseListener(popupListener) popupListener :PopupListener >

UniS Menu Items on PopupListener menuItem used to create each of these menu items in turn, and then add them to PopupListener Then menuItem used to create each of these menu items in turn, and then add them to the submenu.

UniS Internal Class Definitions in Java Until now each class has been defined within a separate file. An internal class is one whose code is contained in the body of another class. This is convenient for a class that is useful only in the context of its containing class. An internal class is an easy way of packaging up some object characteristics that make more sense kept separate from the rest of the containing object's fields. In this case we can clearly see that a popup menu is a separate entity, but only useful within the PersonGui class.

UniS Internal Class Definitions in Java public class PersonGui extends JPanel implements ActionListener { // methods defined here // can create instances of PopupListener // just as with any other class PopupListener foo_variable = new PopupListener ( ); // can access methods for foo_variable just as // with any other class foo_variable.someMethod(someParameters); class PopupListener extends MouseAdapter { // methods defined here }

UniS class PopupListener extends MouseAdapter { JPopupMenu popup; PopupListener(JPopupMenu popupMenu) { popup = popupMenu; } public void mousePressed(MouseEvent e) { maybeShowPopup(e); } public void mouseReleased(MouseEvent e) { maybeShowPopup(e); } private void maybeShowPopup(MouseEvent e) { if (e.isPopupTrigger()) { popup.show(e.getComponent(), e.getX(), e.getY()); }

UniS MouseAdapter The MouseAdapter is an abstract class with empty methods. Hence, extending this class only requires that we implement those mouse event handling methods that we need. If MouseAdapter were an interface, we would have to implement every method it defines, even though many of them would be empty.

UniS When a Right Mouse Click Occurs textArea:JTextArea popupListener: e:Mouse Event > e.getComponent() notify(e ) right_mouse_click e.getX() e.getY() show popup: JPopupMenu

UniS...and then a Left Mouse Click Occurs on a Menu Item :JMenuItem:PersonGui e:Action Event > String cmd = e.getActionCommand(); notify(e ) left_mouse_click

UniS and finally PersonGui invokes actionPerformed(ActionEvent e) :PersonGui else if (AGE.equals(cmd)) { // second button clicked ageString = textField.getText(); dude.setAge(ageString); description = "The age of the person in: " + ageString + newline; textField.setText(""); } textField: dude:Person setAge(ageString); setText("") getText() ageString Same principle as for Mark 1

UniS actionPerformed(ActionEvent e): Choosing Colors else if(POPUP_7.equals(cmd)) { //Bring up color chooser Color newColor = JColorChooser.showDialog( PersonGui.this, "Choose Background Color", textArea.getBackground()); textArea.setBackground(newColor); } :PersonGuitextArea: :JColorChooser setBackground showDialog newColor

UniS

actionPerformed(ActionEvent e): creating person object PersonGui if (PRINT_ATTRIBUTES.equals(cmd)) { //first button clicked String text = dude.Make_Person(); description = text + newline; } dude:Person Make_Person( ) text Note: It is the responsibility of the dude object to create a JFrame to display the person