2 Java2D Slides PDF
2 Java2D Slides PDF
• Java 2D rendering
1
Java 2D Graphical Objects
• Shapes
– Lines, Closed Shapes, Paths, Areas
• Images
– Buffers, Codecs
• Text
– Fonts, Layouts, Transforms
2
Java 2D Graphical Objects
- Shape Example
• Line2D is an abstract class which
implements the Shape interface
• The Shape list has just 2 components in this
case
– moveto (10,30)
– lineto (180,190)
• This will specify the line but won’t draw it
• Creating shapes doesn’t display them
– Some kind of draw() method is needed for that
3
Java 2D Graphical Objects
- Defining Shapes with Paths
public void paint (Graphics g)
{
Graphics2D g2d = (Graphics2D)g;
GeneralPath pathShape = new GeneralPath();
pathShape.moveTo(150,100);
pathShape.lineTo(150,150);
pathShape.quadTo(50,100,50,50);
pathShape.curveTo(75,75,125,50,150,50);
pathShape.close();
g2d.draw(pathShape);
}
4
Path Example
5
Java 2D Graphical Objects
- Area Example
• Consider 4 areas derived from ellipses
Area[] ellipse = new Area[4];
ellipse[0]= new Area(new Ellipse2D.Double(0,20,30,20);
ellipse[1]= new Area(new Ellipse2D.Double(30,20,30,20);
ellipse[2]= new Area(new Ellipse2D.Double(20,0,20,30);
ellipse[3]= new Area(new Ellipse2D.Double(20,30,20,30);
6
Java 2D Graphical Objects
- JPEG Image Example
• Class Jpeg creates a JPEG file using the JPEG codec
import com.sun.image.codec.jpeg.*;
7
Java 2D Graphical Objects
- Text Transform Example
• To apply Graphics2D transformations we turn the
text into a Shape
Shape textShape = text.getOutline(null);
8
Java 2D Rendering
• Colour
– Fixed colours, Transparency, Composition
• Filling
– Solid fills, Gradient fills, Textured fills
• Line Styles
– Strokes, Caps, Mitres, Dashes
• Clipping
Java 2D Rendering
- Colours
g2d.setPaint(Color.black);
9
Java 2D Rendering
- Transparency
• We can define more colours by specifying their
Red, Green and Blue (RGB) components
Color deepPurple = new Color(0.5f,0.0f,0.5f);
Java 2D Rendering
- Composition
• Composition determines what to display when
different colours are drawn on top of each other
• Composition algorithms use the concepts of a
destination and a source
– Destination (d) is what has already been drawn
– Source (s) is what is about to be added to it
• Colour to be displayed and its alpha are given by
cd := fs αs cs + fd αd cd [for each of R, G & B]
αd := fs αs + fd αd [for each of R, G & B]
• Further draws continue to update cd and ad
10
Java 2D Rendering
- Source-over Composition Rule
• Various composition algorithms exist but the most
common is the source-over rule
– The object currently being drawn (the source) is placed
over any objects already drawn (the destination)
– In the source over rule (SrcOver in Java)
fs =1 and fd =1-αs [for each of R, G & B]
So,
cd := αs cs + (1- αs) αd cd [for each of R, G & B]
αd := αs + (1- αs) αd [for each of R, G & B]
g2d.setComposite(AlphaComposite.SrcOver);
Java 2D Rendering
- Other composition rules
Rule Java static fs fd
Source-over SrcOver 1 1- αs
Source Src 1 0
Source-in SrcIn αd 0
Source-out SrcOut 1- αd 0
Destination-over DstOver 1- αd 1
Destination-in DstIn 0 αs
Destination-out DstOut 0 1- αs
Clear Clear 0 0
11
Java 2D Rendering
- Solid Fills
• Solid Fills
//Set the fill colour
g2d.setPaint(Color.green);
// Fill the shape -aShape-
g2d.fill(aShape);
// Make sure border drawn on top of filled
area
g2d.setPaint(Color.black);
g2d.draw(aShape);
Java 2D Rendering
- Gradient Fills
12
Java 2D Rendering
- Textured Fills
• Filling with textured patterns
– A BufferedImage is used to define the texture
private BufferedImage textureConstructor()
{
BufferedImage texcha = new BufferedImage(…);
.
.
.
return texcha;
}
– We apply our method with TexturePaint()
Paint pattern = new
TexturePaint(textureConstructor());
Java 2D Rendering
- Line Styles I
• Line styles are supported by the Stroke interface
and its implementing class BasicStroke
BasicStroke stroke;
stroke = new BasicStroke(width, capStyle, joinStyle);
g2d.setStroke(stroke);
13
Java 2D Rendering
- Line Styles II
• The join styles determine how lines are connected
to each other
– BasicStroke.JOIN_BEVEL
• Joins the outside edges of lines with a straight line giving blunt corner
– BasicStroke.JOIN_MITER
• Extends the outside edges of lines until they meet giving sharp corner
• Note spelling: MITER (US), not MITRE (UK)
• Optionally a mitre limit parameter can be provided which limits how far a
corner can be extended in the construction of the mitred joint
– BasicStroke.JOIN_ROUND
• Caps corners with circular segments to give rounded corner
Java 2D Rendering
- Line Styles III
• Dashed lines are created by specifying templates
in arrays
float[] dashPattern = {10,5,5,5};
– This sets up pattern which draws a line 10 pixels long, leaves a
gap 5 pixels long, draws a line 5 pixels long and then leaves
another gap 5 pixels long (then repeats)
– A dash phase (float!) specifies where in the array the pattern
should start (0.0f means start with first entry in array)
BasicStroke stroke;
stroke = new BasicStroke(width, capStyle, joinStyle,
miterLimit, dashPattern, dashPhase);
14
Java 2D Rendering
- Clipping
• Clipping is achieved in Java 2D by creating a
shape whose boundary determines what should
and should not be drawn
– Anything outside the shape’s boundary is clipped
away and not displayed
– Clipping with an ellipse for example
Shape clippingShape =
new Ellipse2D.Double(30,70,200,100);
g2d.setClip(clippingShape);
15
Java 2D Exercise
• Develop a program to
– Display your name in a colourful and interesting way
• I.e. apply some transformations to it
– Draw a fancy border around it
• I.e. construct a path or three around it
– E-mail the output to me ([email protected])
• I.e. save it as a JPEG file
• Before the next lecture please
16