Title: Computer Science 209
1Computer Science 209
- Using Interfaces
- Graphics
2Interface/Implementation
- An interface specifies a single set of operations
- Any class that implements an interface can
realize the specified behavior - If you know the interface, you can supply or use
any implementing class
3Example Collections
- ArrayList and LinkedList implement List
- HashSet and TreeSet implement Set
- Set and List extend Collection, which specifies
more general methods that apply to both sets and
lists
ltltInterfacegtgt List
ltltInterfacegtgt Set
ltltInterfacegtgt Collection
ArrayList
LinkedList
HashSet
TreeSet
4Example Icons
- GUI components such as buttons, menu items, and
dialogs allow the client to supply an icon for
display - A method or a constructor expects a parameter of
type Icon, which is an interface - An instance of any class that implements Icon can
be supplied as this parameter
5Using an Icon From an Image
JOptionPane.showMessageDialog( Component
parent, String message, String title,
int messageType, Icon anIcon)
JOptionPane.showMessageDialog( null,
"Welcome to W L!", "Message",
JOptionPane.INFORMATION_MESSAGE, new
ImageIcon("WL.gif"))
Any object that supports the Icon interface can
be passed as the last parameter.
6The Icon Interface
import java.awt. public interface Icon
public int getIconWidth() public int
getIconHeight() public void
paintIcon(Component c, Graphics g, int x, int
y)
The component that displays an icon need to be
able to get its width and height and to paint the
icon at a given position paintIcon can use the
Component parameter to obtain properties like the
background color that might be useful
7Using a New Icon for Stars
import javax.swing.JOptionPane public class
IconTest public static void main(String
args) JOptionPane.showMessageDialog(null,
"Here is a Star",
"Message",
JOptionPane.INFORMATION_MESSAGE,
new StarIcon(100))
8Implementing the Icon Interface
import java.awt. import javax.swing.Icon publi
c class StarIcon implements Icon private int
size public StarIcon(int size) this.size
size public int getIconWidth() return
size public int getIconHeight() return
size public void paintIcon(Component c,
Graphics g, int x, int y) // Code for
drawing a star
9Drawing a Star
public void paintIcon(Component c, Graphics g,
int x, int y) int half size / 2 int
fourth size / 4 int threeFourths fourth
3 Polygon p1 new Polygon()
p1.addPoint(x, y fourth) p1.addPoint(x
size, y fourth) p1.addPoint(x half, y
size) Polygon p2 new Polygon()
p2.addPoint(x, y threeFourths)
p2.addPoint(x half, y) p2.addPoint(x
size, y threeFourths) Graphics2D g2
(Graphics2D) g g2.fill(p1) g2.fill(p2)
10Drawing or Filling Shapes
- The Graphics2D class includes the methods draw
and fill for drawing outlines or solids. - These methods expect a single parameter of type
Shape. - Shape is an interface.
- Any object whose class implements Shape can be
drawn or filled.
11Drawing Shapes
public void fill(Shape s)
g.fill(new Line2D.Double(100, 100, 200,
200)) g.fill(new Rectangle2D.Double(100, 100,
200, 200)) g.fill(new(Ellipse2D.Double(50, 50,
100, 100))
Graphics2D includes a wide array of methods for
manipulating shapes.
12Graphics Relationships
Graphics2D
Shape
Line2D.Double
Rectangle2D.Double
Ellipse2D.Double