210 likes | 412 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
E N D
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
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
Key Classes and Methods • javax.awt.geom.GeneralPath.curveTo() • A method to construct a cubic curve segment • javax.awt.image.BufferedImage – A class encapsulating an image • javax.awt.image.BufferedImageOp • An interface for image-processing operators • java.swing.Timer – A class generating action events in a periodic fashion • java.lang.Runnable • An interface to define code executable as a separate thread • java.lang.Thread – A class encapsulating a thread of execution of a program • java.lang.Thread.sleep(long ms) • A method to place the thread to sleep for a specified time period • java.util.Calendar – A class encapsulating a calendar for date and time • java.awt.image.Raster – A class encapsulating image data • java.awt.image.WritableRaster • A class encapsulating writable image data • java.awt.print.PrinterJob – A class for printing management • java.awt.print.Printable – An interface to define print contents
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.
General Bezier Curve Bernstein basis
B-Spline Curve Normalized B-spline blending functions
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 PathIteratorgetPathIterator(AffineTransform at) public PathIteratorgetPathIterator(AffineTransform at, double flatness) public boolean intersects(Rectangle2D rect) public boolean intersects(double x, double y, double w, double h)
A Custom Primitive • Cannot extend the final class GeneralPath • Wrap a GeneralPath public class Heart implements Shape { GeneralPathpath; public Heart(float x,floaty,floatw,floath){ path = new GeneralPath(); … Source
Image Processing • AWT – push model • Java 2D – immediate model • JAI – pull model
BufferedImage Create BufferedImage bi = new BufferedImage(300, 400, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = (Graphics2D)(bi.createGraphics()); Read BufferedImage bi = ImageIO.read(file); Display g2.drawImage(image, 0, 0, new Component() {});
Operators 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)
Mandelbrot Set Iterations on the complex plane Source
Animation: Multi-threading 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) {} } } Source
Animation: Timer Outline of an animation with Timer Timer timer = new Timer(period, listener); timer.start(); public void actionPerformed(ActionEvent event) { <* do frame rendering *> } Source
Cellular Automata 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 Source
Printing • PrintJob • getPrintJob • printDialog • setPrintable • print • Printable • Print • PageFormat • getOrientation • getWidth • getHeight • getImageableX • getImageableY • getImageableWidth • getImageableHeight 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 Timerclass 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.