Computer Science 209 - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Computer Science 209

Description:

Code for drawing a star. Drawing a Star ... Drawing or Filling Shapes ... void fill(Shape s) Graphics2D includes a wide array of methods for manipulating shapes. ... – PowerPoint PPT presentation

Number of Views:29
Avg rating:3.0/5.0
Slides: 13
Provided by: KenLa2
Category:

less

Transcript and Presenter's Notes

Title: Computer Science 209


1
Computer Science 209
  • Using Interfaces
  • Graphics

2
Interface/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

3
Example 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
4
Example 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

5
Using 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.
6
The 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
7
Using 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))

8
Implementing 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
9
Drawing 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)

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

11
Drawing 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.
12
Graphics Relationships
Graphics2D
Shape
Line2D.Double
Rectangle2D.Double
Ellipse2D.Double
Write a Comment
User Comments (0)
About PowerShow.com