Interfaces & Polymorphism part 2:

Slides:



Advertisements
Similar presentations
Chapter 4 (Horstmann’s Book) Interface Types and Polymorphism: Graphics, Timer, Animation Hwajung Lee.
Advertisements

Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
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.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
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.
Lesson 35: Review of the Java GUI. The JFrame, Container and JButton.
GUI Basics: Introduction. Creating GUI Objects // Create a button with text OK JButton jbtOK = new JButton("OK"); // Create a label with text "Enter your.
1 Interface Types & Polymorphism & introduction to graphics programming in Java.
Introduction to GUI Java offers a great number of pre-defined classes to support the development of graphical user interfaces –These are broken down into.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 16.1 Test-Driving the Flag Quiz Application.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
Lesson 27: Introduction to the Java GUI. // helloworldbutton.java import java.awt.*; import javax.swing.*; class HelloButton{ public static void main.
Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog.
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
GUI programming Graphical user interface-based programming.
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,
Polymorphism and interfaces Horstmann ch 4. Outline Interface Polymorphism Function object Anonymous class User Interface Action Scope of variables (Large)
GUI Components and Design Here we add one more component to our programs, JButtons –JButtons can only be inserted into JPanels (or JApplets) –Clicking.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
Lesson 34: Layering Images with Java GUI. The FlowLayout RECAP.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
1 Graphical User Interfaces AWT and Swing packages Frames and Panels Components Nested Panels Images Reading for this Lecture: L&L, 3.9 – 3.11.
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.
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.
1 Class 6. 2 Objectives Objectives Enable your applications to perform actions in response to JButton clicks Get the text the user enters in a textfield.
Creating a GUI with JFC/Swing. What are the JFC and Swing? JFC –Java Foundation Classes –a group of features to help people build graphical user interfaces.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 5 Objectives  Learn about basic GUI components.  Explore how the GUI.
Graphical User Interfaces A Graphical User Interface (GUI) in Java is created with at least three kinds of objects: –components, events, and listeners.
Chapter 4 Interface Types and Polymorphism: Graphics, Timer, Animation.
CS 151: Object-Oriented Design October 1 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 4 – Completing the Inventory Application.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
1 Event Driven Programs with a Graphical User Interface Rick Mercer.
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,
Event-Driven Programming F Procedural programming is executed in procedural order. F In event-driven programming, code is executed upon activation of events.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Lesson 28: More on the GUI button, frame and actions.
Graphical User Interface (GUI)
Lesson 33: Layout management and drawing – Java GUI.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Objects First With Java A Practical Introduction Using BlueJ Building Graphical User Interfaces (GUIs) Week
MIT AITI 2004 Swing Event Model Lecture 17. The Java Event Model In the last lecture, we learned how to construct a GUI to present information to the.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
1 A Quick Java Swing Tutorial. 2 Introduction Swing – A set of GUI classes –Part of the Java's standard library –Much better than the previous library:
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
GUIs & Event-Driven Programming Chapter 11 Review.
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.
Chapter 4 Interface Types and Polymorphism Part 2.
Multiple buttons and action calls
GUIs and Events Rick Mercer.
CSC 205 Programming II Lecture 5 AWT - I.
Interface types and Polymorphism
A First Look at GUI Applications
Chapter 4 Interface Types and Polymorphism Part 2
Chapter 4 Interface Types and Polymorphism Part 1
Java Programming: From Problem Analysis to Program Design,
A Quick Java Swing Tutorial
Ellen Walker Hiram College
Timer class and inner classes
A Quick Java Swing Tutorial
Advanced Programming in Java
Constructors, GUI’s(Using Swing) and ActionListner
Chapter 4 Interface Types and Polymorphism Part 1
Presentation transcript:

Interfaces & Polymorphism part 2: Collections, Comparators, and More fun with Java graphics

Collections (from the Java tutorial)* A collection (sometimes called a container) is simply an object that groups multiple elements into a single unit Collections are used to store, retrieve and manipulate data, and to transmit data from one method to another Collections typically represent data items that form a natural group * http://java.sun.com/docs/books/tutorial/collections/intro/index.html

Java’s Collections class Consists of a set of methods that operate on collections (which are classes that implement the Collection interface or one of its descendants or their implementing classes, such as ArrayList) One of these methods is static method sort The sort method works on objects that implement the Comparable interface

object1.compareTo(object2) 4/23/2017 Comparable interface public interface Comparable { int compareTo(Object other); } Suppose object1 is an object of a class that implements Comparable; then the message object1.compareTo(object2) should return: a negative number if object1<object2 zero if they are equal a positive number (if object1>object2) horst4pt2

Using Collections.sort The String class implements the Comparable interface; thus, to sort an array of Strings: ArrayList words = new ArrayList(); words.add(“fossil”); words.add(“burgeoning”); words.add(“aspersion”); Collections.sort(words);

Using Collections.sort If you have an ArrayList of objects of a new class that you wish to sort, that class should implement the Comparable interface; otherwise, Collections.sort throws an exception Your class should: have implements Comparable in its heading implement the compareTo method

The Comparator interface Objects of a class that implements Comparable can be sorted using Collections.sort() based on the implementing class’s compareTo method If more than one type of sorting is desired, it is more convenient to implement the Comparator interface rather than the Comparable interface An alternative version of Collections.sort() works with implementors of Comparator

Comparator public interface Comparator { int compare(Object a, Object b); } Return values from compare() are as follows: returns a negative number if a < b returns 0 if a == b returns a positive number if a > b

Using sort() with Comparator The sort() method that works with the Comparator interface expects two arguments: a list to be sorted (e.g. an ArrayList - a descendant of List, which is a descendant of Collections) a Comparator (in other words, an object of an implementing class)

Implementing Comparator A key fact about implementing Comparator: the class that does the implementing need not be the basis for the objects to be sorted To create multiple sorting methods, can create multiple Comparator classes, each of which implements compare() in a different way The next several slides exemplify these ideas

public class Thing implements Comparable { private String name; private int size; public Thing (String aName, int aSize) { name = aName; size = aSize; } public String getName() { return name; public double getSize() { return size; public int compareTo(Object otherObject) { Thing other = (Thing) otherObject; if (this.size < other.size) return -1; if (this.size > other.size) return 1; return 0; Exhibit A: a Thing that implements the Comparable interface Implementation of compareTo is based on the value of size; no way to compare two Things based on name (or any other attribute we might define for a Thing)

Exhibit B: a Comparator for Things (based on name rather than size) Note: could create more Comparators for other attributes of Thing (if there were any more) import java.util.*; public class ThingByName implements Comparator { public int compare(Object object1, Object object2) Thing t1 = (Thing) object1; Thing t2 = (Thing) object2; return t1.getName().compareTo(t2.getName()); }

import java.util.*; public class ThingCompTest { public static void main(String[] args) { ArrayList bunchOStuff = new ArrayList(); bunchOStuff.add(new Thing("ambergris", 87)); bunchOStuff.add(new Thing("gummy bear", 4)); bunchOStuff.add(new Thing("Belgium", 30510)); Comparator comp = new ThingByName(); Collections.sort(bunchOStuff, comp); System.out.println("things sorted by name:"); for (int i = 0; i < bunchOStuff.size(); i++) { Thing t = (Thing) bunchOStuff.get(i); System.out.println(t.getName() + " " + t.getSize()); } Collections.sort(bunchOStuff); System.out.println("things sorted by size:"); Exhibit C: a test class, illustrating both sorting methods OUTPUT: things sorted by name: Belgium 30510.0 ambergris 87.0 gummy bear 4.0 things sorted by size:

Anonymous objects An anonymous object is one that is created on the fly in a method call; we saw an example of this in the HolyIconBatman class: JOptionPane.showMessageDialog(null, "Holy icon Batman!", "BatCave", JOptionPane.INFORMATION_MESSAGE, new HolyIconBatman(40)); An anonymous object is simply an object reference that is not stored in a variable

Anonymous objects In the ThingCompTest class, the following lines of code: Comparator comp = new ThingByName(); Collections.sort(bunchOStuff, comp); could be replaced by a single line, using an anonymous object instead of variable comp: Collections.sort(bunchOStuff, new ThingByName());

Anonymous classes Anonymous objects are essentially literal values; since they aren’t stored anywhere, they can be used once, but would have to be reconstructed to be used again Like an anonymous object, an anonymous class is an unnamed class, defined on the fly When defining an anonymous class, you must also construct an anonymous object

Anonymous classes No need to name objects that are used only once: Collections.sort(bunchOStuff, new ThingByName()); By the same taken, no need to name classes that will only be used once The code below creates an anonymous Comparator class to compare things (could use this instead of creating class ThingByName) Comparator comp = new Comparator() { public int compare(Object obj1, Object obj2) { Thing t1 = (Thing)obj1; Thing t2 = (Thing)obj2; return t1.getName().compareTo(t2.getName()); } }; NOTE THE SEMICOLON

Anonymous classes The expression: Comparator comp = new Comparator(){ … }; defines a class that implements the Comparator interface type defines method compare() constructs an object of the new (unnamed) class An anonymous class is a special case of an inner class (a class defined inside another)

Anonymous classes Commonly used in factory methods: public class Thing { … public static Comparator ThingByName() {    return new Comparator() {       public int compare(Object o1, Object o2) { . . . }    }; } … Eliminates need to create separate class whose only purpose is to facilitate sorting of primary class

Anonymous classes Can now sort ArrayList a of Thing objects by calling: Collections.sort(a, Thing.ThingByName()); Neat arrangement if multiple comparators make sense (by name, by size, by ranking, etc.) Gives both types of comparison equal preference, rather than arbitrarily choosing one to implement using compareTo()

Frames We have already seen limited use of graphics programming associated with methods of the JOptionPane class A JOptionPane object is one type of GUI window; the generic window type is the frame Frame window has decorations: title bar borders close box

Constructing and displaying a frame window JFrame frame = new JFrame(); frame.pack(); // automatically sizes window frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); // exits program when window closes frame.setVisible(true); // displays frame Note: requires import statements: import java.awt.*; import javax.swing.*; Can also set size of frame using frame.setSize(w, h);

Drawing in a frame The part of the frame below the title bar and between the borders is the content pane of the window; you can add components to this by assigning it to a Container object, then adding elements to this object One such element that can be added is a JLabel object, which can be used for display of text or images (or both) The next slide illustrates this

import java.awt.*; import javax.swing.*; public class Me2 { public static void main (String[] args) { JFrame frame = new JFrame(); Icon pic1 = new ImageIcon("me2.gif"); JLabel picture1 = new JLabel(pic1); Icon pic2 = new ImageIcon("3x2x2connector.jpg"); JLabel picture2 = new JLabel(pic2); Container windowContents = frame.getContentPane(); windowContents.setLayout(new FlowLayout()); windowContents.add(picture1); windowContents.add(picture2); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }

Notes on example As before, an Icon interface variable holds an ImageIcon reference, which is constructed using the name of a graphics file: Icon pic1 = new ImageIcon("me2.gif"); The JLabel objects picture1 and picture2 are constructed with icons inside them: JLabel picture2 = new JLabel(pic2); These objects are then placed in the JFrame’s content pane and displayed

More notes on example Note the line: contentPane.setLayout(new FlowLayout()); this generates a layout manager to control how the multiple components will line up within the content pane a FlowLayout lines up components side by side

Adding user interface components User interface components such as buttons (instances of JButton) and text fields (instances of JTextField) can also be added to the content pane of a frame The FrameTest example (next slide) illustrates this

import java.awt.*; import javax.swing.*; public class FrameTest { public static void main(String[] args) { JFrame frame = new JFrame(); JButton helloButton = new JButton("Say Hello"); JButton goodbyeButton = new JButton("Say Goodbye"); final int FIELD_WIDTH = 20; JTextField textField = new JTextField(FIELD_WIDTH); textField.setText("Click a button!"); Container contentPane = frame.getContentPane(); contentPane.setLayout(new FlowLayout()); contentPane.add(helloButton); contentPane.add(goodbyeButton); contentPane.add(textField); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); }

User interface components The FrameTest program displays a window that looks like this: But the buttons don’t do anything; the window merely displays them To make the buttons active, we need to incorporate ActionListeners

ActionListener interface An ActionListener object is one that implements the ActionListener interface and its single method, actionPerformed The actionPerformed method requires an ActionEvent parameter; such a parameter is supplied by a user action, such as a mouse click

Making buttons work Construct a listener object and add it to the button; this can be accomplished using an anonymous class: helloButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(“hello world!”); } });

Accessing variables from enclosing scope The anonymous class constructed on the previous slide is, as previously noted, an example of an inner class Note that the inner class accesses the textField object, which is a variable of the outer class When a local variable of an enclosing scope is to be accessed by an inner class, the variable must be declared final

Constructing multiple instances of anonymous ActionListener Write helper method that constructs objects Pass variable information as parameters Declare parameters final public static ActionListener createGreetingButtonListener( final String message) { return new ActionListener() public void actionPerformed(ActionEvent event) textField.setText(message); } };

Complete example – continued on next slide import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ActionTest { private static JTextField textField; public static void main(String[] args){ JFrame frame = new JFrame(); final int FIELD_WIDTH = 20; textField = new JTextField(FIELD_WIDTH); textField.setText("Click a button!"); JButton helloButton = new JButton("Say Hello"); helloButton.addActionListener(createGreetingButtonListener("Hello, World!")); JButton goodbyeButton = new JButton("Say Goodbye"); goodbyeButton.addActionListener(createGreetingButtonListener("Goodbye, World!")); Container contentPane = frame.getContentPane(); contentPane.setLayout(new FlowLayout());

Example continued contentPane.add(helloButton); contentPane.add(goodbyeButton); contentPane.add(textField); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } public static ActionListener createGreetingButtonListener(final String message) { return new ActionListener() { public void actionPerformed(ActionEvent event) { textField.setText(message); }; // ends definition of new ActionListener (& return statement) } // ends createGreetingButtonListener method } // ends class