Chapter 4 Interface Types and Polymorphism Part 1





























- Slides: 29
Chapter 4 Interface Types and Polymorphism Part 1
Chapter Topics ü The Icon interface Type ü Polymorphism ü Drawing Shapes ü The Comparable Interface Type ü The Comparator Interface Type ü Anonymous Classes ü Frames and User Interface Components ü User Interface Actions ü Timers ü Designing an Interface Type
Why? ü Interfaces § 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 § 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 JOption. Pane to display message: JOption. Pane. show. Message. Dialog(null, "Hello, World!" ); ü Note icon to the left
Icon Interface Type (cont. ) ü Can specify arbitrary image file JOption. Pane. show. Message. Dialog( null, // parent window "Hello, World!", //message title "Message", // window title JOption. Pane. INFORMATION_MESSAGE, // message type new Image. Icon("globe. gif")); Public static void show. Message. Dialog ( Component parent, Object message, String title, int message. Type Icon an. Icon)
Icon Interface Type (cont. ) message. Type ü Defines the style of the message. The Look and Feel manager may lay out the dialog differently depending on this value, and will often provide a default icon. The possible values are: • • • ERROR_MESSAGE INFORMATION_MESSAGE WARNING_MESSAGE QUESTION_MESSAGE PLAIN_MESSAGE http: //java. sun. com/docs/books/tutorial/uiswing/components/dialog. html
Icon Interface Type (cont. ) ü What if we don't want to generate an image file? ü Fortunately, can use any class that implements Icon interface type ü Image. Icon is one such class ü Easy to supply your own class
The Icon Interface Type ü public interface Icon { int get. Icon. Width(); int get. Icon. Height(); void paint. Component(Component c, Graphics g, int x, int y); }
Interface Types ü No implementation ü Implementing class must supply implementation of all methods ü Ch 4/icon 2/Mars. Icon. java ü show. Message. Dialog expects Icon object ü Ok to pass Mars. Icon ü Ch 4/icon 2/Icon. Test. java
The Icon Interface Type and Implementing Classes
Polymorphism (cont. ) ü public static void show. Message. Dialog(. . . Icon an. Icon) ü show. Message. Dialog shows § icon § message § OK button ü show. Message. Dialog must compute size of dialog ü width = icon width + message size + blank size ü How do we know the icon width? int width = an. Icon. get. Icon. Width();
Polymorphism (cont. ) ü show. Message. Dialog doesn't know which icon is passed § Image. Icon? § Mars. Icon? §. . . ? ü The actual type of an. Icon is not Icon ü There are no objects of type Icon ü an. Icon belongs to a class that implements Icon ü That class defines a get. Icon. Width method
Polymorphism (cont. ) A Variable of Interface Type
Polymorphism (cont. ) ü Which get. Icon. Width method is called? ü Could be § Mars. Icon. get. Icon. Width § Image. Icon. get. Icon. Width §. . . ü Depends on object to which an. Icon reference points, e. g. show. Message. Dialog(. . . , new Mars. Icon(50)) ü Polymorphism: Select different methods according to actual object type
Benefits of Polymorphism ü Loose coupling § show. Message. Dialog decoupled from Image. Icon § Doesn't need to know about image processing ü Extensibility § Client can supply new icon types
Drawing Shapes ü paint. Icon method receives graphics context of type Graphics ü Actually a Graphics 2 D object in modern Java versions public void paint. Icon(Component c, Graphics g, int x, int y) { Graphics 2 D g 2 = (Graphics 2 D) g; . . . } ü Can draw any object that implements Shape interface Shape s =. . . ; g 2. draw(s);
Drawing Rectangles and Ellipses ü Rectangle 2 D. Double constructed with § top left corner § width § height ü g 2. draw(new Rectangle 2 D. Double(x, y, width, height)); ü For Ellipse 2 D. Double, specify bounding box
Drawing Ellipses
Drawing Line Segments ü Point 2 D. Double is a point in the plane ü Line 2 D. Double joins to points Point 2 D. Double start = new Point 2 D. Double(x 1, y 1); Point 2 D. Double end = new Point 2 D. Double(x 2, y 2); Shape segment = new Line 2 D. Double(start, end); g 2. draw(segment);
Relationship Between Shape Classes
Drawing Text ü g 2. draw. String(text, x, y); ü x, y are base point coordinates
Filling Shapes ü Fill interior of shape g 2. fill(shape); ü Set color fills or strokes: g 2. set. Color(Color. red); ü Program that draws car Ch 4/icon 3/Car. Icon. java
The Comparable Interface Type ü Collections has static sort method: Array. List a =. . . Collections. sort(a); ü Objects in list must implement the Comparable interface type public interface Comparable { int compare. To(Object other); } ü object 1. compare. To(object 2) returns § Negative number if object 1 less than object 2 § 0 if objects identical § Positive number if object 1 greater than object 2
The Comparable Interface Type ü sort method compares and rearranges elements if (object 1. compare. To(object 2) > 0). . . ü String class implements Comparable interface type: lexicographic (dictionary) order ü Country class: compare countries by area Ch 4/sort 1/Country. java Ch 4/sort 1/Country. Sort. Test. java
The Comparator interface type ü How can we sort countries by name? ü Can't implement Comparable twice! ü Comparator interface type gives added flexibility public interface Comparator { int compare(Object object 1, Object object 2); } ü Pass comparator object to sort: Collections. sort(list, comp);
The Comparator interface type ü Ch 4/sort 2/Country. Comparator. By. Name. java Ch 4/sort 2/Comparator. Test. 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
Anonymous Classes ü No need to name objects that are used only once. Collections. sort(countries, new Country. Comparator. By. Name()); ü No need to name classes that are used only once. Comparator comp = new Comparator() { public int compare(Object obj 1, Object obj 2) { Country country 1 = (Country)obj 1; Country country 2 = (Country)obj 2; return country 1. get. Name(). compare. To(country 2. get. Name()); } };
Anonymous Classes ü anonymous new expression: § defines anonymous class that implements Comparator § defines compare method of that class § constructs one object of that class
Anonymous Classes ü Commonly used in factory methods: public class Country ü{ public static Comparator comparator. By. Name() { return new Comparator() { public int compare(Object o 1, Object o 2) {. . . } }; } } ü Collections. sort(a, Country. comparator. By. Name()); ü Neat arrangement if multiple comparators make sense (by name, by area, . . . )