Chapter 4 Interface Types and Polymorphism Part 1

Slides:



Advertisements
Similar presentations
Graphics You draw on a Graphics object The Graphics object cannot directly be created by your code, instead one is generated when the method paintComponent.
Advertisements

LAB SESSION 7 Graphical user interface Applet fundamentals Methods in applets Execution of an applet Graphics class.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Chapter 4 (Horstmann’s Book) Interface Types and Polymorphism: Graphics, Timer, Animation Hwajung Lee.
Chapter 5 Programming Graphics. Chapter Goals To be able to write simple applications To display graphical shapes such as lines and ellipses To use colors.
Chapter 5 Programming Graphics. Chapter Goals To be able to write applications with simple graphical user interfaces To display graphical shapes such.
CPSC 2100 University of Tennessee at Chattanooga – Fall 2013 Object-Oriented Design & Patterns 2 nd edition Cay S. Horstmann Chapter 4: Interface Types.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
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.
Graphics Programming. In this class, we will cover: The difference between AWT and Swing Creating a frame Frame positioning Displaying information in.
1 Introduction to Applets Overview l What is an Applet? l Steps for creating an applet l What is HTML? l Basic HTML tags l Drawing Simple Graphical shapes.
Advanced Object Oriented Programming Chapter 3 & 4.
Chapter 13: Object-Oriented Programming
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
Swing Graphics ● Empty Swing containers have no visual appearance except for a background color ● Every JComponent must have a paintComponent method that.
Java Concepts Chapter 2 – Graphical Applications Mr. Smith AP Computer Science A.
1 Interface Types & Polymorphism & introduction to graphics programming in Java.
Design Patterns and Graphical User Interfaces Horstmann ,
1 Principles of Computer Science I Prof. Nadeem Abdul Hamid CSC 120 – Fall 2005 Lecture Unit 5 - Graphics.
Computer Science 209 The Strategy Pattern I: Comparisons and Layouts.
Interfaces & Polymorphism part 2:
CHAPTER 2 Using Objects. Basic Programming Terminology  Computer program process values.  Numbers (digits)  Words (Strings)  These values are different.
CS 151: Object-Oriented Design September 24 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Polymorphism and interfaces Horstmann ch 4. Outline Interface Polymorphism Function object Anonymous class User Interface Action Scope of variables (Large)
Chapter 4: Applets and Graphics 1 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 4 Applets and Graphics.
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 10 Fall 2010.
CS 151: Object-Oriented Design September 26 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST.
Introduction to Java Chapter 8 - Introduction to Java Graphics1 Chapter 8 Introduction to Java Graphics.
Computer Science 209 The Adapter Pattern. The Context of the Adapter Pattern I want to use an existing class (the adaptee) without modifying it The context.
Chapter 5 Programming Graphics. Chapter Goals To be able to write simple applications To display graphical shapes such as lines and ellipses To use colors.
Georgia Institute of Technology Drawing in Java – part 3 Barb Ericson Georgia Institute of Technology September 2006.
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
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Object Oriented Programming Lecture 3: Things OO.
Georgia Institute of Technology Drawing in Java – part 2 Dr Usman Saeed Assistant Professor Faculty of Computing and Information Technology North Jeddah.
Chapter 4 Interface Types and Polymorphism Part 2.
Chapter 4 Interfaces and Polymorphism Object-Oriented Design & Patterns Cay S. Horstmann.
Barb Ericson Georgia Institute of Technology September 2005
Arrays Chapter 7.
Interface types and Polymorphism
12 Graphics and Java 2D™.
ITEC324 Principle of CS III
3 Introduction to Classes and Objects.
Java Graphics CS 2511.
Building Java Programs
2.5 Another Java Application: Adding Integers
Chapter 4 Interface Types and Polymorphism Part 2
Building Java Programs
Building Java Programs
Example: Card Game Create a class called “Card”
Building Java Programs
Java Programming: Guided Learning with Early Objects
Message, Input, Confirm, and Specialized Dialogs
Java Graphics The basic rendering mechanism is the drawing system that controls when and how programs can draw on a graphics component. When a component.
Message, Input, and Confirm Dialogs
Review Session Biggest discrepancy questions
Building Java Programs
Building Java Programs
Building Java Programs
Handout-14 Applets and Graphics
Building Java Programs
Building Java Programs
Graphics Reading: Supplement 3G
Barb Ericson Georgia Institute of Technology September 2005
ITEC324 Principle of CS III
Corresponds with Chapter 5
Chapter 4 Interface Types and Polymorphism Part 1
Presentation transcript:

Chapter 4 Interface Types and Polymorphism Part 1

Why? Interfaces Polymorphism A class defines a set of operations (the interface) and statements (implementation). Separating the interface concept from that of a class can help in the development of “generic” reusable code. Polymorphism Polymorphism: Ability to select different methods according to actual object type. Multiple classes can implement the same interface type so that it is possible to operate on a mixture of objects from any of these classes.

Icon Interface Type (cont.) Use JOptionPane to display message: JOptionPane.showMessageDialog(null, "Hello, World!" ); Note icon to the left

Icon Interface Type (cont.) Can specify arbitrary image file JOptionPane.showMessageDialog( null, // parent window "Hello, World!", //message title "Message", // window title JOptionPane.INFORMATION_MESSAGE, // message type new ImageIcon("globe.gif")); Public static void showMessageDialog ( Component parent, Object message, String title, int messageType Icon anIcon)

The Icon Interface Type public interface Icon { int getIconWidth(); int getIconHeight(); void paintIcon(Component c, Graphics g, int x, int y); }

Interface Types No implementation Implementing class must supply implementation of all methods Ch4/icon2/MarsIcon.java showMessageDialog expects Icon object Ok to pass MarsIcon Ch4/icon2/IconTest.java

The Icon Interface Type and Implementing Classes

Polymorphism (cont.) public static void showMessageDialog(...Icon anIcon) showMessageDialog shows icon message OK button showMessageDialog must compute size of dialog width = icon width + message size + blank size How do we know the icon width? int width = anIcon.getIconWidth();

Polymorphism (cont.) showMessageDialog doesn't know which icon is passed ImageIcon? MarsIcon? . . .? The actual type of anIcon is not Icon There are no objects of type Icon anIcon belongs to a class that implements Icon That class defines a getIconWidth method

Polymorphism (cont.) A Variable of Interface Type

Polymorphism (cont.) Which getIconWidth method is called? Could be MarsIcon.getIconWidth ImageIcon.getIconWidth . . . Depends on object to which anIcon reference points, e.g. showMessageDialog(..., new MarsIcon(50)) Polymorphism: Ability to select different methods according to actual object type.

Benefits of Polymorphism Loose coupling showMessageDialog decoupled from ImageIcon Doesn't need to know about image processing Extensibility Client can supply new icon types

Shape Interface Type & Polymorphism (1) paintIcon method receives graphics context of type Graphics Actually a Graphics2D object in modern Java versions public void paintIcon(Component c, Graphics g, int x, int y) { Graphics2D g2 = (Graphics2D) g; . . . } Can draw any object that implements Shape interface Shape s = . . .; g2.draw(s); http://java.sun.com/j2se/1.3/docs/api/java/awt/Shape.html http://www.developer.com/tech/article.php/626101#_Complete_Program_Listing The Shape interface provides four groups of overloaded methods that make it possible to perform the following tasks: Get a bounding box that is guaranteed to enclose a specified Shape object. Determine if a specified Shape object completely contains a specified point or rectangle. Determine if a specified Shape intersects a specified rectangle Get a PathIterator object that can be used to obtain the path that makes up the boundary of a Shape object.

Shape Interface Type & Polymorphism (2) Drawing Rectangles and Ellipses Rectangle2D.Double constructed with  top left corner width height g2.draw(new Rectangle2D.Double(x, y, width, height)); For Ellipse2D.Double, specify bounding box

Shape Interface Type & Polymorphism (3) Drawing Ellipses

Shape Interface Type & Polymorphism (4) Drawing Line Segments Point2D.Double is a point in the plane Line2D.Double joins to points Point2D.Double start = new Point2D.Double(x1, y1); Point2D.Double end = new Point2D.Double(x2, y2); Shape segment = new Line2D.Double(start, end); g2.draw(segment);

Shape Interface & Polymorphism (5) Relationship Between Shape Interface and Classes

Shape Interface Type & Polymorphism (6) Drawing Text g2.drawString(text, x, y); x, y are base point coordinates

Shape Interface Type & Polymorphism (6) Filling Shapes Fill interior of shape g2.fill(shape); Set color for fills or strokes: g2.setColor(Color.red); Program that draws car Ch4/icon3/CarIcon.java

The Comparable Interface Type & Polymorphism (1) Collections  has static sort method: ArrayList a = . . . Collections.sort(a); Objects in list must implement the Comparable interface type public interface Comparable { int compareTo(Object other); } object1.compareTo(object2) returns Negative number if object1 less than object2 0 if objects identical Positive number if object1 greater than object2

The Comparable Interface Type & Polymorphism (2) sort method compares and rearranges elements if (object1.compareTo(object2) > 0) . . . String class implements Comparable interface type: lexicographic (dictionary) order Country class: compare countries by area Ch4/sort1/Country.java Ch4/sort1/CountrySortTest.java

The Comparator interface type & Polymorphism (1) How can we sort countries by name? Can't implement Comparable twice! Comparator interface type gives added flexibility public interface Comparator { int compare(Object object1, Object object2); }  In JDK 1.5 or later public interface Comparator<T> { int compare(T object1, T object2); } Pass comparator object to sort: Collections.sort(list, comp);

The Comparator interface type & Polymorphism (2) Ch4/sort2/CountryComparatorByName.java Ch4/sort2/ComparatorTest.java Revised – in a JDK 1.5 or later CountryComparatorByName.java ComparatorTest.java Comparator object is a function object This particular comparator object has no state State can be useful, e.g. flag to sort in ascending or descending order

The Comparator interface type & Polymorphism (3) Public class CountryComparatorByName implements Comparator<Country> { Public CountryComparatorByName(boolean ascending) if (ascending) direction = 1; else direction = -1; } Public int compare(Country country1, Country country2) return direction * country1.getName().compareTo(country2.getName()); Private int direction;

Anonymous Classes (1) No need to name objects that are used only once. Collections.sort(countries, new CountryComparatorByName()); No need to name classes that are used only once. Comparator<Country> comp = new Comparator<Country>() { public int compare(Country country1, Country country2) { return country1.getName().compareTo(country2.getName()); } };

Anonymous Classes (2) anonymous new expression: defines anonymous class that implements Comparator defines compare method of that class constructs one object of that class

Anonymous Classes (3) Commonly used in factory methods: public class Country { public static Comparator<Country> comparatorByName() {    return new Comparator<Country>()    {       public int compare(Country c1, Country c2) { . . . }    }; } } Collections.sort(a, Country.comparatorByName()); Neat arrangement if multiple comparators make sense (by name, by area, ...)

Anonymous Classes (4) Ch4/Country.java Ch4/ComparatorTest.java