27
Chapter 4 (Horstmann’s Book) Interface Types and Polymorphism: Graphics, Timer, Animation Hwajung Lee

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

Embed Size (px)

Citation preview

Page 1: Chapter 4 (Horstmann’s Book) Interface Types and Polymorphism: Graphics, Timer, Animation Hwajung Lee

Chapter 4 (Horstmann’s Book)Interface Types and Polymorphism: Graphics, Timer, Animation

Hwajung Lee

Page 2: Chapter 4 (Horstmann’s Book) Interface Types and Polymorphism: Graphics, Timer, Animation Hwajung Lee

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 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.

Page 3: Chapter 4 (Horstmann’s Book) Interface Types and Polymorphism: Graphics, Timer, Animation Hwajung Lee

public interface Icon{

int getIconWidth();int getIconHeight();void paintIcon(Component c,

Graphics g, int x, int y);

}

Page 4: Chapter 4 (Horstmann’s Book) Interface Types and Polymorphism: Graphics, Timer, Animation Hwajung Lee

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

Page 5: Chapter 4 (Horstmann’s Book) Interface Types and Polymorphism: Graphics, Timer, Animation Hwajung Lee

Rectangle2D.Double constructed with  top left corner width height

g2.draw(new Rectangle2D.Double(x, y, width, height));

For Ellipse2D.Double, specify bounding box

Page 6: Chapter 4 (Horstmann’s Book) Interface Types and Polymorphism: Graphics, Timer, Animation Hwajung Lee
Page 7: Chapter 4 (Horstmann’s Book) Interface Types and Polymorphism: Graphics, Timer, Animation Hwajung Lee

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);

Page 8: Chapter 4 (Horstmann’s Book) Interface Types and Polymorphism: Graphics, Timer, Animation Hwajung Lee
Page 9: Chapter 4 (Horstmann’s Book) Interface Types and Polymorphism: Graphics, Timer, Animation Hwajung Lee

g2.drawString(text, x, y); x, y are base point coordinates

Page 10: Chapter 4 (Horstmann’s Book) Interface Types and Polymorphism: Graphics, Timer, Animation Hwajung Lee

Fill interior of shapeg2.fill(shape);

Set color for fills or strokes:g2.setColor(Color.red);

Program that draws carCh4/icon3/CarIcon.java

Page 11: Chapter 4 (Horstmann’s Book) Interface Types and Polymorphism: Graphics, Timer, Animation Hwajung Lee

No need to name objects that are used only once.

Collections.sort(students,new

StudentComparatorByName()); No need to name classes that are used only

once.

Comparator<Student> comp = new Comparator<Student>() { public int compare(Student student1, Student student2) { return student1.getName().compareTo(student2.getName()); } };

Page 12: Chapter 4 (Horstmann’s Book) Interface Types and Polymorphism: Graphics, Timer, Animation Hwajung Lee

Anonymous class is commonly used in factory methods:

public class Student{ public static Comparator<Student> comparatorByName() {   return new Comparator<Student>()   {      public int compare(Student s1, Student s2) { . . . }   };

}}

Neat arrangement if multiple comparators make sense(by name, by grade, ...)

Page 13: Chapter 4 (Horstmann’s Book) Interface Types and Polymorphism: Graphics, Timer, Animation Hwajung Lee

Ch4/action1/ActionTest.java

Page 14: Chapter 4 (Horstmann’s Book) Interface Types and Polymorphism: Graphics, Timer, Animation Hwajung Lee

Method of Inner classes can access variables that are visible in the enclosing scope e.g. actionPerformed method accesses the textField variable

of the enclosing main method

If an inner class accesses a local variable from an enclosing scope, the variable must be declared as final.

final JTextField textField = new TextField(Field_WIDTH);

Page 15: Chapter 4 (Horstmann’s Book) Interface Types and Polymorphism: Graphics, Timer, Animation Hwajung Lee

To construct multiple objects of the same anonymous class, you must instantiate the anonymous class in a helper method (a factory method) and then call it multiple times. Note: you should declare parameters final

Debug the following Code

public static ActionListener createGreetingButtonListener(String message)

{ return new

ActionListener() { public void actionPerformed(ActionEvent event) { textField.setText(message); } };

}

Page 16: Chapter 4 (Horstmann’s Book) Interface Types and Polymorphism: Graphics, Timer, Animation Hwajung Lee

Answer:

public static ActionListener createGreetingButtonListener(final String message)

{ return new

ActionListener() { public void actionPerformed(ActionEvent event) { textField.setText(message); } };

}

Page 17: Chapter 4 (Horstmann’s Book) Interface Types and Polymorphism: Graphics, Timer, Animation Hwajung Lee

Public class ActionTest{

Public static void main(String[] args){

…textField = new JTextField(FIELD_SIZE);helloButton.addActionListener(

creatGreetingButtonListener(“Hello, World !”));goodbyeButton. addActionListener(

creatGreetingButtonListener(“Goodbye, World !”));

…}// the helper method in the previous slide places here

}

Page 18: Chapter 4 (Horstmann’s Book) Interface Types and Polymorphism: Graphics, Timer, Animation Hwajung Lee

Javax.swing package Timer generates a sequence of action events

spaced apart at equal time interval, and notifies a designated action listener.

ActionListener listener = ...;final int DELAY = 1000; // 1000 millisec = 1 secTimer t = new Timer(DELAY, listener);t.start();

The start method returns immediately. A new thread of execution is started that issues action events in the specified frequency.

Page 19: Chapter 4 (Horstmann’s Book) Interface Types and Polymorphism: Graphics, Timer, Animation Hwajung Lee

Ch4/timer/TimerTester.java

Page 20: Chapter 4 (Horstmann’s Book) Interface Types and Polymorphism: Graphics, Timer, Animation Hwajung Lee

Public class ShapeIcon implements Icon{

Public void paintIcon(Component c, Graphics g, int x, int y){

Paint the shape}…

}

Page 21: Chapter 4 (Horstmann’s Book) Interface Types and Polymorphism: Graphics, Timer, Animation Hwajung Lee

ShapeIcon icon = new ShapeIcon(…);JLabel label = new JLabel(icon);

ActionListener listener = newActionListener(){

public void actionPerformed(ActionEvent event){

move the shapelabel.repaint();

}};

Page 22: Chapter 4 (Horstmann’s Book) Interface Types and Polymorphism: Graphics, Timer, Animation Hwajung Lee

Use timer to Move car shapes Draw car with CarShape

Two responsibilities: Draw shape Move shape

Define new interface type MoveableShape : we can decoupled the animation from the car shape.

Page 23: Chapter 4 (Horstmann’s Book) Interface Types and Polymorphism: Graphics, Timer, Animation Hwajung Lee
Page 24: Chapter 4 (Horstmann’s Book) Interface Types and Polymorphism: Graphics, Timer, Animation Hwajung Lee

Name the methods to conform to standard library public interface MoveableShape{   void draw(Graphics2D g2);   void translate(int dx, int dy);}

CarShape class implements MoveableShapepublic class CarShape implements MoveableShape{   public void translate(int dx, int dy)   { x += dx; y += dy; }   . . .}

Page 25: Chapter 4 (Horstmann’s Book) Interface Types and Polymorphism: Graphics, Timer, Animation Hwajung Lee

Label contains icon that draws shape Timer action moves shape, calls repaint on label Label needs Icon, we have MoveableShape Supply ShapeIcon adapter class ShapeIcon.paintIcon calls MoveableShape.draw

Page 26: Chapter 4 (Horstmann’s Book) Interface Types and Polymorphism: Graphics, Timer, Animation Hwajung Lee
Page 27: Chapter 4 (Horstmann’s Book) Interface Types and Polymorphism: Graphics, Timer, Animation Hwajung Lee

Ch4/animation/MoveableShape.java Ch4/animation/ShapeIcon.java Ch4/animation/AnimationTester.java Ch4/animation/CarShape.java