- Create a
Shape object
Rectangle2D.Double rect = ...;
Ellipse2D.Double ellipse = ...;
Polygon poly = ...;
GeneralPath path = ...;
- Cast the Graphics object to a Graphics2D object
public void paintComponent(Graphics g) {
super.paintComponent(g); // Typical Swing approach
Graphics2D g2d = (Graphics2D)g;
g2d.doSomeStuff(...);
...
}
- Modify drawing parameters
g2d.setPaint(fillColorOrPattern);
g2d.setStroke(penThicknessOrPattern);
g2d.setComposite(someAlphaComposite);
g2d.setFont(someFont);
g2d.translate(...);
g2d.rotate(...);
g2d.scale(...);
g2d.shear(...);
g2d.setTransform(someAffineTransform);
- Draw an outlined or solid version of the Shape
g2d.draw(someShape);
g2d.fill(someShape);