Presentation is loading. Please wait.

Presentation is loading. Please wait.

Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved. 1 Chapter 2 2D Graphics: Basics F The architecture.

Similar presentations


Presentation on theme: "Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved. 1 Chapter 2 2D Graphics: Basics F The architecture."— Presentation transcript:

1 Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved. 1 Chapter 2 2D Graphics: Basics F The architecture and operations of a 2D graphics system F The coordinate spaces in a rendering pipeline F Java 2D program structure and the Graphics2D object F The basic 2D geometric primitives F 2D coordinate systems and equations of graphs F Constructing custom shapes using GeneralPath class F Constructive area geometry

2 Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved. 2 Rendering Pipeline 1.Construct the 2D objects. 2.Apply transformations to the objects. 3.Apply color and other rendering properties. 4.Render the scene on a graphics device.

3 Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved. 3 A Simple Java 2D Program F Usually have at least two classes –A class that extends JApplet to serve as the high level container u The main method needs to create a JFrame for a stand-alone application u the init method creates a panel and adds it to the applet –A class that extends JPanel to serve as the drawing surface u The paintComponent method contains code to do the drawing

4 Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved. 4 paintComponent public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; // code to draw desired picture }

5 Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved. 5 Graphics class F Original AWT classes had a graphics context which was a Graphics object –No separation of modeling and rendering F Swing classes have a Graphics2D context –Parameter of the paintComponent method still has type Graphics –The actual argument to paintComponent is a Graphics2D object so it can be cast

6 Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved. 6 The Graphics2D Class F Java 2D rendering engine F Extends the Graphics class F Encapsulates all rendering functions F Two ways to access the graphics context void paintComponent(Graphics g){ } Graphics getGraphics()

7 Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved. 7 Methods of Graphics Class void setColor(Color c) void setFont(Font f) void setXORMode(Color c) void setPaintMode() F Data includes the following –Foreground color –Font –Rendering modes

8 Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved. 8 Methods of Graphics Class void drawLine(int x1, int y1, int x2, int y2) void drawRect(int x1, int y1, int width, int height) void drawOval(int x1, int y1, int width, int height) void drawArc(int x1, int y1, int width, int height, int start, int arc) void drawRoundRect(int x1, int y1, int width, int height, int arcW, int arcH) void drawPolygon(int[] xPoints, int[] yPoints, int nPoints) void fillRect(int x1, int y1, int width, int height) void fillOval(int x1, int y1, int width, int height) void fillArc(int x1, int y1, int width, int height, int start, int arc) void fillRoundRect(int x1, int y1, int width, int height, int arcW, int arcH) void fillPolygon(int[] xPoints, int[] yPoints, int nPoints) void drawString(String str, int x, int y) void translate(int x, int y)

9 Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved. 9 Graphics 2D F Graphics2D extends Graphics F New properties include –Stroke –Paint F New capabilities –drawing and filling Shape objects –applying AffineTransforms

10 Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved. 10 Methods of Graphics2D Class void draw(Shape s) void fill(Shape s) void setTransform(AffineTransform Tx) void transform(AffineTransform Tx) void setPaint(Paint p) void setStroke(Stroke s) void clip(Shape s) void setComposite(Composite c) void addRenderingHints(Map hints)

11 Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved. 11 2D Coordinate System

12 Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved. 12 Java 2D Coordinate System y-axis pointing downward F Screen coordinates work differently

13 Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved. 13 2D Objects F A 2D object consists of a set of points in a plane F Examples –point –lines, curves –shapes like rectangles, ellipses, polygons –text and images

14 Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved. 14 Line Points on a line satisfy a linear equation Ax + By + C = 0

15 Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved. 15 How to specify a line F With an equation F By giving the value of y when x is 0 and the slope of the line F By specifying the coordinates of two points on the line –For graphics, we usually specify the endpoints

16 Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved. 16 Polygons F Any figure with straight sides can be specified by giving the coordinates of all the vertexes F For a rectangle that is aligned with the screen axes, you only need two points –This also works for shapes which can be derived from a rectangular bounding box

17 Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved. 17 Curved Figures F For a shape with a curved outline, you need to specify the surface with an equation of some kind F Examples –ellipses (circles) F More complicated figures may need segments from multiple curves

18 Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved. 18 Ellipse

19 Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved. 19 Consider the circle F Circle centered at origin with radius R is defined by –x 2 + y 2 = R 2 F If we solve this to get y in terms of x, there are two values of y for each x F Use a parametric equation instead

20 Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved. 20 Parametric Equation F Parametric equation expresses each coordinate in terms of a parameter x = f(t) y = g(t) F The parametric equation for an ellipse is x = x 0 + a cos t y = y 0 + b sin t

21 Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved. 21 Graphing a Spirograph The parametric equation for a spirograph is x = (r 1 + r 2 ) cos t - p cos ((r 1 + r 2 ) t / r 2 ) y = (r 1 + r 2 ) cos t - p cos ((r 1 + r 2 ) t / r 2 )

22 Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved. 22 The Shape Interface

23 Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved. 23 Methods in the Shape Interface boolean contains( …) 4 versions Rectangle2D getBounds() PathIterator getPathIterator() boolean intersects( …) 2 versions

24 Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved. 24 Provided Shapes F Line F Rectangle F Round rectangle F Ellipse F Arc F Quadratic curve F Cubic curve F Polygon

25 Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved. 25 Rectangular Shapes F Rectangle, Rectangle2D, Ellipse2D, Arc2D, RoundedRectangle2D F Specify the position of the upper-left corner of the bounding box followed by the width and height. –For Arc2d, also need starting angle and the angle it spans –For RoundedRectangle2D, need a width and height for the arcs at the corners

26 Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved. Polygons F A polygon is a closed figure which is defined by a sequence of points called vertexes. F Create an empty Polygon with Polygon poly = new Polygon() F Add vertexes using poly.addPoint( x, y); F Use the Graphics2D drawn and fill methods to display the Polygon g.draw( poly); g.fill( poly)

27 Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved. 27 Bezier Curves F Bezier curves are parametric curves specified by two endpoints and one or more control points –quadratic Bezier curve needs one control point –cubic Bezier curve needs two control points –n th order Bezier curve needs n-1 control points F Bezier curves are often used in computer graphics

28 Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved. 28 Quadratic Bezier Curve F Given points P 0, P 1 and P 2 B(t) = (1-t) 2 P 0 + 2t(1-t)(P 1 - P 0 ) + t 2 P 2 for 0<=x<=1 F QuadCurve2D is probably a quadratic Bezier curve

29 Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved. 29 Cubic Bezier Curve F Given points P 0, P 1, P 2 and P 3 B(t) = (1-t) 3 P 0 + 3t(1-t) 2 (P 1 - P 0 ) + 3t 2(1-t) P 2 + t 3 P 3 for 0<=x<=1 F CubicCurve2D is a cubic Bezier curve

30 Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved. 30

31 Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved. 31 Constructive Area Geometry F Set-theoretic operations –Intersect –Add –Subtract –Exclusive or

32 Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved. 32 GeneralPath F Five segment types –SEG_MOVETO –SEG_LINETO –SEG_QUADTO –SEG_CUBICTO –SEG_CLOSE F GeneralPath methods –moveTo –lineTo –quadTo –curveTo –closePath

33 Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved. 33 Testing for Interior Points F There are a number of times when you need to know whether a point is inside or outside a Shape –e.g. for filling the shape F Several approaches –Even-odd rule - count the number of times a line from outside the shape to the point crosses a boundary –Non-zero winding rule - count the number of times the boundary winds around the point in a clockwise direction


Download ppt "Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved. 1 Chapter 2 2D Graphics: Basics F The architecture."

Similar presentations


Ads by Google