Slide navigation: Forward with space bar, → arrow key, or PgDn. Backwards with ← or PgUp.
Comparable
InterfaceComparator
InterfaceJOptionPane
to display message:JOptionPane.showMessageDialog(null, "Hello, World!");
JOptionPane.showMessageDialog(
null,
"Hello, World!",
"Message",
JOptionPane.INFORMATION_MESSAGE,
new ImageIcon("globe.gif"));
Icon
interface typeImageIcon
is one such classIcon
Interface Typepublic interface Icon { int getIconWidth(); int getIconHeight(); void paintIcon(Component c, Graphics g, int x, int y); }
showMessageDialog
expects Icon
objectMarsIcon
Icon
Interface Type and Implementing Classespublic static void showMessageDialog(...Icon anIcon)
showMessageDialog
shows
showMessageDialog
must compute size of dialogint width = anIcon.getIconWidth();
showMessageDialog
doesn't know which icon is passed
ImageIcon
?MarsIcon
?anIcon
is not Icon
Icon
anIcon
belongs to a class that implements Icon
getIconWidth
methodgetIconWidth
method is called?anIcon
reference points, e.g.
showMessageDialog(..., new MarsIcon(50))
showMessageDialog
decoupled from ImageIcon
Comparable
Interface Type
Collections
has static sort
method:
ArrayList<E> a = . . . Collections.sort(a);
Comparable
interface type
public interface Comparable<T> { int compareTo(T other); }
ArrayList
)other
Comparable
Interface Type
object1.compareTo(object2)
returns
object1
less than object2
object1
greater than object2
sort
method compares and rearranges elements
if (object1.compareTo(object2) > 0) . . .
String
class implements Comparable<String>
interface type: lexicographic (dictionary) orderCountry
class: compare countries by area
ch04/sort1/Country.java
ch04/sort1/CountrySortTester.java
Comparator
interface typeComparable
twice!Comparator
interface type gives added flexibility
public interface Comparator<T> { int compare(T obj1, T obj2); }
sort
:
Collections.sort(list, comp);
Comparator
interface typeComparator
and to make just one instance thereofComparator<Country> comp = (first, second) -> first.getName().compareTo(second.getName());
Comparator<Country>
...compare
method has the given body...(arguments) -> expression
Collections.sort(countries, (first, second) -> first.getName().compareTo(second.getName()));
public static Comparator<Country> byName(boolean ascending) { return (first, second) -> (ascending ? 1 : -1) * first.getName().compareTo(second.getName()); }
final
final
(i.e. are never assigned to){...}
and return
public static Comparator<Country> byName(boolean ascending) { return (first, second) -> { int factor; if (ascending) factor = 1; else factor = -1; return factor * first.getName().compareTo(second.getName()); } }
JFrame frame = new JFrame(); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true);
JButton helloButton = new JButton("Say Hello");
frame.setLayout(new FlowLayout());
frame.add(helloButton);
ActionListener
interface type
public interface ActionListener { int actionPerformed(ActionEvent event); }
helloButton.addActionListener(event -> Button action);
helloButton.addActionListener(event -> textField.setText("Hello, World!"));
event
even though we don't use it.helloButton.addActionListener(listener);
listener.actionPerformed(event);
textField.setText("Hello, World!");
public static ActionListener createGreetingButtonListener( String message) { return event -> textField.setText(message); }
helloButton.addActionListener( createGreetingButtonListener("Hello, World!")); goodbyeButton.addActionListener( createGreetingButtonListener("Goodbye, World!"));
ActionListener listener = ...; final int DELAY = 1000; // 1000 millisec = 1 sec Timer t = new Timer(DELAY, listener); t.start();
paintIcon
method receives graphics context of type Graphics
Graphics2D
object in modern Java versions
public void paintIcon(Component c, Graphics g, int x, int y) { Graphics2D g2 = (Graphics2D)g; . . . }
Shape s = . . .; g2.draw(s);
Rectangle2D.Double
constructed with
g2.draw(new Rectangle2D.Double(x, y, width, height));
Ellipse2D.Double
, specify bounding boxPoint2D.Double
is a point in the planeLine2D.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);
g2.drawString(text, x, y);
x
, y
are base point coordinatesg2.fill(shape);
g2.setColor(Color.red);
CarShape
MoveableShape
public interface MoveableShape { void draw(Graphics2D g2); void move(); }
CarShape
class implements MoveableShape
public class CarShape implements MoveableShape { public void translate(int dx, int dy) { x++; } . . . }
Icon
, we have MoveableShape
ShapeIcon
adapter classShapeIcon.paintIcon
calls MoveableShape.draw