Download presentation
Presentation is loading. Please wait.
Published bySharlene Shields Modified over 8 years ago
1
2D Graphics: Advanced Topics Chapter 4
2
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
3
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
4
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 4
5
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
6
General Bezier Curve 6 Bernstein basis
7
B-Spline Curve 7 Normalized B-spline blending functions
8
B-Spline to Bezier Conversion 8 Source
9
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
10
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(); …
11
Image Processing 11 F AWT – push model F Java 2D – immediate model F JAI – pull model
12
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
13
Operators 13 Source
14
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
15
Mandelbrot Set 15 Source Iterations on the complex plane
16
Animation: Multi-threading 16 Source Outline of a typical multi-threading animation public void paintComponent(Graphics g) { } public void run() { while(true) { repaint(); try { Thread.sleep(sleepTime); } catch (InterruptedException ex) {} }
17
Animation: Timer 17 Source Timer timer = new Timer(period, listener); timer.start(); public void actionPerformed(ActionEvent event) { } Outline of an animation with Timer
18
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
19
Printing PrintJob –getPrintJob –printDialog –setPrintable –print Printable –Print PageFormat –getOrientation –getWidth –getHeight –getImageableX –getImageableY –getImageableWidth –getImageableHeight 19 Source
20
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.