Polygons and Polylines © 2004 Pearson Addison-Wesley. All rights reserved 7-1 Arrays can be helpful in graphics processing For example, they can be used.

Slides:



Advertisements
Similar presentations
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Outline Graphics Applets Drawing Shapes Components and Containers Images.
Advertisements

ELC 310 Day 21. © 2004 Pearson Addison-Wesley. All rights reserved10-2 Agenda Questions? Capstone Proposals Overdue  4 Submitted  3 Accepted, 1 in negotiations.
ITEC220 GUI Lecture – Part 2 References  Java Software Solutions,” by Lewis & Loftus  Chapter 7  Chapter 8  Chapter 9  Java Foundations-Introduction.
Chapter 8 Inheritance 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Chapter 9: Polymorphism Coming up: Binding.
Chapter 9: Graphical User Interfaces Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,
Chapter 9 Polymorphism. © 2004 Pearson Addison-Wesley. All rights reserved9-2 Polymorphism Polymorphism (having many forms) is an object- oriented concept.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
© The McGraw-Hill Companies, 2006 Chapter 18 Advanced graphics programming.
Chapter Day 23. © 2007 Pearson Addison-Wesley. All rights reserved Agenda Day 23 Problem set 4 Due Problem set 5 Posted (Last one)  Due Dec 8 Capstones.
Chapter Day 10. © 2007 Pearson Addison-Wesley. All rights reserved4-2 Agenda Day 10 Questions from last Class?? Problem set 2 posted  10 programs from.
Wecksten, Mattias Arrays Based on templates from Lewis & Loftus.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Chapter 8: Animation Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002, and used with.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter Day 22. © 2007 Pearson Addison-Wesley. All rights reserved Agenda Day 22 Problem set 4 Posted  10 problems from chapters 7 & 8  Due Nov 21 (right.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter Day 25. © 2007 Pearson Addison-Wesley. All rights reserved Agenda Day 25 Problem set 5 Posted (Last one)  Due Dec 8 Capstones Schedule  3rd.
Chapter 7 Arrays. © 2004 Pearson Addison-Wesley. All rights reserved7-2 Arrays Arrays are objects that help us organize large amounts of information Chapter.
Chapter 9 Polymorphism 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter Day 21. © 2007 Pearson Addison-Wesley. All rights reserved7-2 Agenda Day 20 Problem set 4 Posted  10 problems from chapters 7 & 8  Due Nov 21.
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.
Copyright © 2012 Pearson Education, Inc. Chapters 3-8 Graphical User Interfaces Java Software Solutions Foundations of Program Design Seventh Edition John.
Chapter Day 19. © 2007 Pearson Addison-Wesley. All rights reserved7-2 Agenda Day 19 Problem set 3 Corrected  1 A, 2 B’s and 1 D Problem set 4 Posted.
Chapter 8 Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Inheritance Inheritance is a fundamental object-oriented design technique.
ELC 312 Day 16. © 2004 Pearson Addison-Wesley. All rights reserved Agenda Questions? Capstone Proposals Due Problem set 3 Corrected  Poor performance.
© 2004 Pearson Addison-Wesley. All rights reserved8-1 Chapter 8 : Inheritance Intermediate Java Programming Summer 2007.
10-1 Exceptions An exception is an object that describes an unusual or erroneous situation Exceptions are thrown by a program, and may be caught and handled.
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Graphical User Interfaces Revisited Material from Chapters
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Event Driven Programming, The.
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.
Creating Graphical User Interfaces (GUI’s) with MATLAB By Jeffrey A. Webb OSU Gateway Coalition Member.
1 Arrays An array is an ordered list of values An array of size N is indexed from zero to N-1 scores.
Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Chapter 7: Arrays.
Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Object Oriented Programming.  Interface  Event Handling.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 – Graphical User Interfaces Java Foundations: Introduction to Programming.
ELC 310 Day 19. © 2004 Pearson Addison-Wesley. All rights reserved9-2 Agenda Questions? Capstone Proposals Overdue  4 Submitted  3 Accepted, 1 in negotiations.
Creating User Interfaces Event-Driven Programming.
Chapter 10 Exceptions 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
Oct 091 Example Program DemoInputValidation1.java DemoInputValidation2.java.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
1/18H212Mouse and Timer Events H212 Introduction to Software Systems Honors Lecture #16: Mouse and Timer Events October 26, 2015.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Graphic User Interface. Introduction to Graphics The last few sections of each chapter of the textbook focus on graphics and graphical user interfaces.
Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
Chapter 9: Graphical User Interfaces
10 Exceptions Software Solutions Lewis & Loftus java 5TH EDITION
Miscellaneous Topics #6: Polygons GUI Components and Event Handlers
Chap 7. Building Java Graphical User Interfaces
Graphical User Interfaces -- Introduction
ITEC220 GUI Lecture – Part 2 References
Presentation transcript:

Polygons and Polylines © 2004 Pearson Addison-Wesley. All rights reserved 7-1 Arrays can be helpful in graphics processing For example, they can be used to store a list of coordinates A polygon is a multisided, closed shape A polyline is similar to a polygon except that its endpoints do not meet, and it cannot be filled See Rocket.java (page 409) Rocket.java See RocketPanel.java (page 410) RocketPanel.java

The Polygon Class © 2004 Pearson Addison-Wesley. All rights reserved 7-2 The Polygon class can also be used to define and draw a polygon It is part of the java.awt pacakage Versions of the overloaded drawPolygon and fillPolygon methods take a single Polygon object as a parameter instead of arrays of coordinates A Polygon object encapsulates the coordinates of the polygon

Mouse Events Events related to the mouse are separated into mouse events and mouse motion events Mouse Events: mouse pressedthe mouse button is pressed down mouse releasedthe mouse button is released mouse clickedthe mouse button is pressed down and released without moving the mouse in between mouse enteredthe mouse pointer is moved onto (over) a component mouse exitedthe mouse pointer is moved off of a component © 2004 Pearson Addison-Wesley. All rights reserved 7-3

Mouse Events Mouse Motion Events: mouse movedthe mouse is moved mouse draggedthe mouse is moved while the mouse button is pressed down © 2004 Pearson Addison-Wesley. All rights reserved 7-4 Listeners for mouse events are created using the MouseListener and MouseMotionListener interfaces A MouseEvent object is passed to the appropriate method when a mouse event occurs

Mouse Events © 2004 Pearson Addison-Wesley. All rights reserved 7-5 For a given program, we may only care about one or two mouse events To satisfy the implementation of a listener interface, empty methods must be provided for unused events See Dots.java (page 413) Dots.java See DotsPanel.java (page 414) DotsPanel.java

Mouse Events © 2004 Pearson Addison-Wesley. All rights reserved 7-6 Rubberbanding is the visual effect in which a shape is "stretched" as it is drawn using the mouse The following example continually redraws a line as the mouse is dragged See RubberLines.java (page 417) RubberLines.java See RubberLinesPanel.java (page 418) RubberLinesPanel.java

Key Events A key event is generated when the user types on the keyboard key presseda key on the keyboard is pressed down key releaseda key on the keyboard is released key typeda key on the keyboard is pressed down and released © 2004 Pearson Addison-Wesley. All rights reserved 7-7 Listeners for key events are created by implementing the KeyListener interface A KeyEvent object is passed to the appropriate method when a key event occurs

Key Events © 2004 Pearson Addison-Wesley. All rights reserved 7-8 The component that generates a key event is the one that has the current keyboard focus Constants in the KeyEvent class can be used to determine which key was pressed The following example "moves" an image of an arrow as the user types the keyboard arrow keys See Direction.java (page 421) Direction.java See DirectionPanel.java (page 422) DirectionPanel.java

© 2004 Pearson Addison-Wesley. All rights reserved 8-9 The Component Class Hierarchy The Java classes that define GUI components are part of a class hierarchy Swing GUI components typically are derived from the JComponent class which is derived from the Container class which is derived from the Component class Many Swing components can serve as (limited) containers, because they are derived from the Container class For example, a JLabel object can contain an ImageIcon

© 2004 Pearson Addison-Wesley. All rights reserved 8-10 The Component Class Hierarchy An applet is a good example of inheritance Recall that when we define an applet, we extend the Applet class or the JApplet class The Applet and JApplet classes already handle all the details about applet creation and execution, including: interaction with a Web browser accepting applet parameters through HTML enforcing security restrictions

© 2004 Pearson Addison-Wesley. All rights reserved 8-11 The Component Class Hierarchy Our applet classes only have to deal with issues that specifically relate to what our particular applet will do When we define paintComponent method of an applet, we are actually overriding a method defined originally in the JComponent class and inherited by the JApplet class

© 2004 Pearson Addison-Wesley. All rights reserved 8-12 Event Adapter Classes Inheritance also gives us an alternate technique for creating listener classes We've seen that listener classes can be created by implementing a particular interface, such as MouseListener We can also create a listener class by extending an event adapter class Each listener interface that has more than one method has a corresponding adapter class, such as the MouseAdapter class

© 2004 Pearson Addison-Wesley. All rights reserved 8-13 Event Adapter Classes Each adapter class implements the corresponding listener and provides empty method definitions When you derive a listener class from an adapter class, you only need to override the event methods that pertain to the program Empty definitions for unused event methods do not need to be defined because they are provided via inheritance See OffCenter.java (page 466) OffCenter.java See OffCenterPanel.java (page 467) OffCenterPanel.java

© 2004 Pearson Addison-Wesley. All rights reserved 8-14 The Timer Class The Timer class of the javax.swing package is a GUI component, but it has no visual representation A Timer object generates an action event at specified intervals Timers can be used to manage any events that are based on a timed interval, such as an animation To create the illusion of movement, we use a timer to change the scene after an appropriate delay

© 2004 Pearson Addison-Wesley. All rights reserved 8-15 The Timer Class The start and stop methods of the Timer class start and stop the timer The delay can be set using the Timer constructor or using the setDelay method See Rebound.java (page 471) Rebound.java See ReboundPanel.java (page 472) ReboundPanel.java

© 2004 Pearson Addison-Wesley. All rights reserved 9-16 Event Processing Polymorphism plays an important role in the development of a Java graphical user interface As we've seen, we establish a relationship between a component and a listener: JButton button = new JButton(); button.addActionListener(new MyListener()); Note that the addActionListener method is accepting a MyListener object as a parameter In fact, we can pass the addActionListener method any object that implements the ActionListener interface

© 2004 Pearson Addison-Wesley. All rights reserved 9-17 Event Processing The source code for the addActionListener method accepts a parameter of type ActionListener (the interface) Because of polymorphism, any object that implements that interface is compatible with the parameter reference variable The component can call the actionPerformed method because of the relationship between the listener class and the interface Extending an adapter class to create a listener represents the same situation; the adapter class implements the appropriate interface already

© 2004 Pearson Addison-Wesley. All rights reserved 9-18 Dialog Boxes Recall that a dialog box is a small window that "pops up" to interact with the user for a brief, specific purpose The JOptionPane class makes it easy to create dialog boxes for presenting information, confirming an action, or accepting an input value Let's now look at another

© 2004 Pearson Addison-Wesley. All rights reserved 9-19 File Choosers Situations often arise where we want the user to select a file stored on a disk drive, usually so that its contents can be read and processed A file chooser, represented by the JFileChooser class, simplifies this process The user can browse the disk and filter the file types displayed See DisplayFile.java (page 516) DisplayFile.java

© 2004 Pearson Addison-Wesley. All rights reserved 9-20 Color Choosers In many situations we want to allow the user to select a color A color chooser, represented by the JColorChooser class, simplifies this process The user can choose a color from a palette or specify the color using RGB values See DisplayColor.java (page 519) DisplayColor.java

© 2004 Pearson Addison-Wesley. All rights reserved 9-21 Sliders A slider is a GUI component that allows the user to specify a value within a numeric range A slider can be oriented vertically or horizontally and can have optional tick marks and labels The minimum and maximum values for the slider are set using the JSlider constructor A slider produces a change event when the slider is moved, indicating that the slider and the value it represents has changed

© 2004 Pearson Addison-Wesley. All rights reserved 9-22 Sliders The following example uses three sliders to change values representing the color components of an RGB value See SlideColor.java (page 522) SlideColor.java See SlideColorPanel.java (page 523) SlideColorPanel.java

© 2004 Pearson Addison-Wesley. All rights reserved Tool Tips A tool tip provides a short pop-up description when the mouse cursor rests momentarily on a component A tool tip is assigned using the setToolTipText method of a Swing component JButton button = new JButton ("Compute"); button.setToolTipText ("Calculate size");

© 2004 Pearson Addison-Wesley. All rights reserved Mnemonics A mnemonic is a keyboard alternative for pushing a button or selecting a menu option The mnemonic character should be chosen from the component's label, and is underlined The user activates the component by holding down the ALT key and pressing the mnemonic character A mnemonic is established using the setMnemonic method: JButton button = new JButton ("Calculate"); button.setMnemonic ("C");

© 2004 Pearson Addison-Wesley. All rights reserved Disabled Components Components can be disabled if they should not be used A disabled component is "grayed out" and will not respond to user interaction The status is set using the setEnabled method: JButton button = new JButton (“Do It”); button.setEnabled (false);

© 2004 Pearson Addison-Wesley. All rights reserved GUI Design The right combination of special features such as tool tips and mnemonics can enhance the usefulness of a GUI See LightBulb.java (page 551) LightBulb.java See LightBulbPanel.java (page 553) LightBulbPanel.java See LightBulbControls.java (page 554) LightBulbControls.java

© 2004 Pearson Addison-Wesley. All rights reserved Combo Boxes A combo box provides a menu from which the user can choose one of several options The currently selected option is shown in the combo box A combo box shows its options only when the user presses it using the mouse Options can be established using an array of strings or using the addItem method

© 2004 Pearson Addison-Wesley. All rights reserved The JukeBox Program A combo box generates an action event when the user makes a selection from it See JukeBox.java (page 557)JukeBox.java See JukeBoxControls.java (page 559)JukeBoxControls.java

© 2004 Pearson Addison-Wesley. All rights reserved Scroll Panes A scroll pane is useful for images or information too large to fit in a reasonably-sized area A scroll pane offers a limited view of the component it contains It provides vertical and/or horizontal scroll bars that allow the user to scroll to other areas of the component No event listener is needed for a scroll pane See TransitMap.java (page 562) TransitMap.java

© 2004 Pearson Addison-Wesley. All rights reserved Split Panes A split pane ( JSplitPane ) is a container that displays two components separated by a moveable divider bar The two components can be displayed side by side, or one on top of the other Moveable Divider Bar Left Component Right Component Top Component Bottom Component

© 2004 Pearson Addison-Wesley. All rights reserved Split Panes The orientation of the split pane is set using the HORIZONTAL_SPLIT or VERTICAL_SPLIT constants The divider bar can be set so that it can be fully expanded with one click of the mouse The components can be continuously adjusted as the divider bar is moved, or wait until it stops moving Split panes can be nested

© 2004 Pearson Addison-Wesley. All rights reserved Lists The Swing Jlist class represents a list of items from which the user can choose The contents of a JList object can be specified using an array of objects A JList object generates a list selection event when the current selection changes See PickImage.java (page 566) PickImage.java See ListPanel.java (page 568) ListPanel.java

© 2004 Pearson Addison-Wesley. All rights reserved Lists A JList object can be set so that multiple items can be selected at the same time The list selection mode can be one of three options: single selection – only one item can be selected at a time single interval selection – multiple, contiguous items can be selected at a time multiple interval selection – any combination of items can be selected The list selection mode is defined by a ListSelectionModel object