2D Graphics: Advanced Topics

eyelashesnectarineSoftware and s/w Development

Nov 3, 2013 (3 years and 10 months ago)

74 views

2D Graphics: Advanced Topics

Chapter 4

Bird’s Eye View


Overview of Computer Graphics


2D Graphics: Basics


2D Graphics: Rendering Details


2D Graphics: Advanced topics


Advanced techniques in 2D graphics including B
-
spline curves, custom shape primitives, image
processing, animation, fractal images, and
printing

2

Objectives


To understand B
-
spline curves


To construct custom shape primitives


To apply basic image processing techniques


To create fractal images


To create 2D animation


To perform graphics printing

3

Key Classes and Methods


j
avax.awt.geom.GeneralPath.curveTo
()


A method to construct a cubic curve segment


javax.awt.image.BufferedImage



A class encapsulating an image


j
avax.awt.image.BufferedImageOp


An interface for image
-
processing operators


java.swing.Timer



A class generating action events in a periodic fashion


j
ava.lang.Runnable


An interface to define code executable as a separate thread


j
ava.lang.Thread



A class encapsulating a thread of execution of a program


j
ava.lang.Thread.sleep
(long
ms
)


A method to place the thread to sleep for a specified time period


j
ava.util.Calendar



A class encapsulating a calendar for date and time


j
ava.awt.image.Raster



A class encapsulating image data


j
ava.awt.image.WritableRaster


A class encapsulating writable image data


j
ava.awt.print.PrinterJob



A class for printing management


j
ava.awt.print.Printable



An interface to define print contents

4

Key Terms


B
-
spline curve


A curve defined as a parametric equation of piecewise polynomials blending with a sequence of
control points.


Bezier curve


A curve defined as a parametric equation of polynomials blending with control points.


NURB


Non
-
uniform rational B
-
spline


Image processing


Computer manipulation of digital images to enhance or extract information.


Convolution


A type of linear operations often used in signal and image processing


Kernel


A function to define a convolution.


Complex numbers


An extension of real numbers


Complex plane


The set of complex numbers interpreted as points on a plane.


Frame rate


The speed of an animation measured in frames per second (fps)


THREAD


A LINE OF EXECUTION IN A RUNNING PROGRAM. In a multithreading environment a single program
may have several threads running simultaneously.


Cellular automata


A dynamic system on a grid of cells, evolving based a simple set of rules that specify the next state
of a cell using the previous states of itself and its neighbors.

5

General Bezier Curve


6




n
i
i
n
i
t
B
p
t
s
0
,
)
(
)
(
i
n
i
i
n
t
t
i
n
t
B











)
1
(
)
(
,
Bernstein basis

B
-
Spline Curve


7




n
i
i
k
i
t
N
p
t
p
0
,
)
(
)
(
)
(
)
(
)
(
otherwise
,
0
)
,
[
,
1
)
(
1
,
1
1
1
1
,
1
,
1
,
0
t
N
t
t
t
t
t
N
t
t
t
t
t
N
t
t
t
t
N
i
k
i
k
i
k
i
i
k
i
k
i
i
i
k
i
i
i





















Normalized B
-
spline
blending functions


B
-
Spline to Bezier Conversion


8

2
/
)
(
3
/
)
2
(
3
/
)
2
(
2
/
)
(
3
/
)
2
(
3
/
)
2
(
4
2
3
2
1
4
1
2
1
1
0
1
1
1
1
b
b
b
p
p
b
p
p
b
b
b
b
p
p
b
p
p
b
i
i
i
i
i
i
i
i



















Source

Shape Interface

public
boolean

contains(Rectangle2D
rect
)

public
boolean

contains(Point2D point)

public
boolean

contains(double x, double y)

public
boolean

contains(double x, double y, double w, double h)

public Rectangle
getBounds
()

public Rectangle2D getBounds2D()

public
PathIterator

getPathIterator
(
AffineTransform

at)

public
PathIterator

getPathIterator
(
AffineTransform

at, double
flatness)

public
boolean

intersects(Rectangle2D
rect
)

public
boolean

intersects(double x, double y, double w, double h)


9

A Custom Primitive


Cannot extend the final class
GeneralPath


Wrap a
GeneralPath

10

Source

public class Heart implements Shape {



GeneralPath

path;


public
Heart(float
x,float

y,float

w,float

h
){


path = new
GeneralPath
();




Image Processing

11


Image file

Image
object

Operators

Image
object

Image file

read

write

Screen

Screen

display

display



AWT


push model



Java 2D


immediate model



JAI


pull model

BufferedImage

12

BufferedImage

bi = new
BufferedImage
(300,
400,
BufferedImage.TYPE_INT_RGB
);

Graphics2D g2 =
(Graphics2D)(
bi.createGraphics
());

g2.drawImage(image, 0, 0, new Component()
{});

BufferedImage

bi =
ImageIO.read
(file);

Create

Read

Display

Operators

13


BufferedImageOp

RescaleOp

LookupOp

AffineTransformOp

ColorConvertOp

ConvolveOp

Source

Raster and WritableRaster

int
[]
getPixel
(
int

x,
int

y,
int
[] data)

float[]
getPixel
(
int

x,
int

y, float[] data)

double[]
getPixel
(
int

x,
int

y, double[] data)

int
[]
getPixels
(
int

x,
int

y,
int

w,
int

h,
int
[] data)

float[]
getPixels
(
int

x,
int

y,
int

w,
int

h, float[] data)

double[]
getPixels
(
int

x,
int

y,
int

w,
int

h, double[] data)

void
setPixel
(
int

x,
int

y,
int
[] data)

void
setPixel
(
int

x,
int

y, float[] data)

void
setPixel
(
int

x,
int

y, double[] data)

void
setPixels
(
int

x,
int

y,
int

w,
int

h,
int
[] data)

void
setPixels
(
int

x,
int

y,
int

w,
int

h, float[] data)

void
setPixels
(
int

x,
int

y,
int

w,
int

h, double[] data)


14

Mandelbrot Set

15

Source

c
z
z
n
n



2
1
Iterations on the complex plane

Animation: Multi
-
threading

16

Source

Outline of a typical multi
-
threading animation


public void
paintComponent
(Graphics g) {


<* render a frame *>


}



public void run() {


while(true) {


<* update frame data *>


repaint();


try {


Thread.sleep
(
sleepTime
);


} catch (
InterruptedException

ex) {}


}


}

Animation: Timer

17

Source

Timer
timer

= new Timer(period,
listener);

timer.start
();



public void
actionPerformed
(
ActionEvent

event)
{


<* do frame rendering *>

}

Outline of an animation with Timer

Cellular Automata

18

Source


The iteration of the system proceeds by assigning the next
state of each cell based on the previous configuration. Each
cell follows the same set of rules and the new state depends
only upon the current states of the same cell and its
neighbors.

Example: A cell is black if
exactly one of its neighbors
in the current configuration
is black


Printing


PrintJob


getPrintJob


printDialog


setPrintable


print


Printable


Print


PageFormat


getOrientation


getWidth


getHeight


getImageableX


getImageableY


getImageableWidth


getImageableHeight


19

Source

Summary


A B
-
spline curves can be converted to a sequence of Bezier curves,
which may then be rendered directly by the Graphics2D object. The
control point of each Bezier curve are linear combinations of controls
points of the B
-
spline curve.


Shape

interface and
GeneralPath

class


BufferedImage

class is the main representation for images in Java
2D.
BufferedImageOp

interface provides convenient ways to
process
BufferedImage

objects.


Raster

and
WritableRaster

classes offer access to the pixel data
in a
BufferedImage
. Mandelbrot set is given.


The
Thread

class and the
Runnable

interface provide essential
multithreading support. The
Timer

class of the
Swing

package
provides a convenient utility to trigger the periodic frame rendering.


Graphics2D

class provides the rendering engine. The
Printable

interface allows for the necessary callback structure for defining the
graphics output. The
PrinterJob

class facilitates the special tasks in
printing.


20