Java 2D and SVG: Rachel Struthers
Java 2D and SVG: Rachel Struthers
Rachel Struthers
Topics Covered
Java 2D
Rendering
Basic Shapes
Transformations
SVG(Scalable Vector Graphics)
Basic drawing commands
Some examples
Batik Java library
Java 2D and SVG
Vector based rather than pixel based
Both can be used on the web to show
high-quality interactive graphcis
Java 2D – produce vector graphics using
code
API in Java 1.2 and above
SVG – produce vector graphics using XML
Vector Graphics vs. Raster
Graphics
Raster Graphics
Pixel based – high bandwidth
Resolution dependent, picture from workstation might
not work on PDA
Examples: GIF, JPEG, BMP
Vector Graphics
Command based – low bandwidth
Resolution independent = scalable, zoomable without
loss of quality
Examples: SVG, SWF (Flash), EPS (Encapsulated
Postscript)
What is Java 2D?
API included with JDK
Line art
Text
Imaging
Allows you to easily
Draw lines of any thickness
Fill shapes with gradients and textures
Move, rotate, scale, and shear text and graphics
Composite overlapping text and graphics
Example: ShowOff.java (from Java 2D book)
Where Can I Use Java 2D?
Any Java 2 type application
Included in Java 1.2 or above
Stand-alone rich-client applications
Web applications (applets)
Servers side generation of dynamic
images suchs as maps, charts, etc.
What Are Prerequisites for
Using?
Basic Java programming
Some OO knowledge
Need to have basic grasp of interfaces for:
Shape
Paint
Some exposure to graphics or user interface
programing helpful
AWT or Swing
Graphics in other languages
Think Paint!
When using Java 2D, keep in mind the
rendering pipeline
Nothing is shown until paint is called
Must extend an object that is of the
Component family
Override paint() method
No painting – no picture!
When is paint() called for a
component?
System triggered:
When the component is first shown
When it is “damaged” (covered by a window
and then uncovered, for example)
Component resized
App triggered:
When a program calls repaint
State of button has changed
Date (model) has changed
Paint in Swing or AWT
Components
For AWT, can override Canvas, Panel
paint methods
Other things that inherit from Component
For Swing
Extend JPanel, JButton, etc
Other things that inherit from JComponent
How to use in AWT
Extend a component such as Canvas or Panel
Override
public void paint(Graphics g)
If extending a container call super.paint(g) so
that children are painted
Rest of the discussion uses Swing components
How to use in Swing
Components
Extend a Swing component such as JPanel
Override paintComponent
Cast Graphics to Graphics2D so you can use
advanced features
Make sure you call super.paintComponent(g)
as first call!
Or funny things happen
Ghosts, background wrong
Methods Called for Swing
Refresh
In Swing paint() still gets called when it's time
to render
Work of paint is divided into three methods
protected void paintComponent(Graphics g)
protected void paintBorder(Graphics g)
protected void paintChildren(Graphics g)
Generally you only override paintComponent
Basic Java 2D
public MyBasicDrawPanel extends JPanel {
}
Graphics2D draw and fill
Arguments to the draw and fill must
implement the Shape interface
Graphics built using objects rather than
instructions
Rectangles, Ellipses and ARcs
Note that many Shape classes have inner
constructors
Use Rectangle2D inner classes to create:
Rectangle2D.Double
Retangle2D.Float
Ellipse2D:
Ellipse2D.Double
Ellipse2D.Float
Arc2D
Arc2D.Double
Arc2D.Float
See JavaApplet2D.java
Arc2D – three different close
types
Open
Pie
Chord
Lines
Create a line using Line2D classes inner class
constructors
Line2D.Float(), etc.
Line2D.Double(), etc.
StringArt.java
QuadCurve2D
2 end points and a control points
CubicCurve2D
2 end points and 2 control points
DragKing.java
Bounds/Hit Testing
Shape has various contains() methods
Makes for easy user interaction
Java2DApplet.java