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.

Slides:



Advertisements
Similar presentations
Computer Graphics- SCC 342
Advertisements

Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved. 1 Chapter 2 2D Graphics: Basics.
Chapter 5 Programming Graphics. Chapter Goals To be able to write simple applications To display graphical shapes such as lines and ellipses To use colors.
Chapter 5 Programming Graphics. Chapter Goals To be able to write applications with simple graphical user interfaces To display graphical shapes such.
ENS 207 engineering graphics
Java Graphics Section 1 - Multi-File Graphics Programs Section 2 - The Coordinate System and Graphics Context g Section 3 - The Java Drawing and Painting.
Informationsteknologi Wednesday, November 7, 2007Computer Graphics - Class 51 Today’s class Geometric objects and transformations.
GUI and Swing, part 2 The illustrated edition. Scroll bars As we have previously seen, a JTextArea has a fixed size, but the amount of text that can be.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 14 Graphics.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 13 Graphics.
Graphics Programming. In this class, we will cover: The difference between AWT and Swing Creating a frame Frame positioning Displaying information in.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Topics  Applets  Classes used for graphics Graphics Point Dimension.
Slides prepared by Rose Williams, Binghamton University Chapter 19 Swing II.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Java Applets What is an Applet? How do you create.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L01 (Chapter 13) Graphics.
Graphics Output Primitives Pixel Addressing and Fill Area Dr. M. Al-Mulhem Feb. 1, 2008.
1 L38 Graphics and Java 2D™ (3). 2 OBJECTIVES In this chapter you will learn:  To understand graphics contexts and graphics objects.  To understand.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Java Graphics Applets.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Applets and Graphics.
ENDS 375 Foundations of Visualization Geometric Representation 9/30/04.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
May 13, 1998CS102-02Lecture 7-2 The End of Graphics in Java CS Lecture 7-2 A picture's worth a thousand words.
Swing Graphics ● Empty Swing containers have no visual appearance except for a background color ● Every JComponent must have a paintComponent method that.
CS324e - Elements of Graphics and Visualization Java2D Graphics.
1 Review of COMPSCI 221  Chapters 1-11 in text  User Interfaces will be addressed as a take- home question.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Standard Graphics in Java,
1 Graphical User Components (II) Outline JTextArea Creating a Customized Subclass of JPanel JPanel Subclass that Handles Its Own Events Windows: Additional.
Chapter 15 Graphics and Java 2D™ Java How to Program, 8/e (C) 2010 Pearson Education, Inc. All rights reserved.
 Pearson Education, Inc. All rights reserved. 1 Ch 12 Graphics and Java 2D In this chapter you will learn:  To understand graphics contexts.
(C) 2010 Pearson Education, Inc. All rights reserved. Omer Boyaci.
Lab 6: Shapes & Picture Extended Ellipse & Rectangle Classes Stand-Alone GUI Applications.
Advanced User Interfaces with Java SD’98 - Session 3206 Ted Faison Faison Computing Inc.
Java Graphics Swing Graphics
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved COS240 Object-Oriented Languages.
2D Graphics Basics Chapter 2. Bird’s Eye View  Overview of Computer Graphics  2D Graphics: Basics –Basic 2D graphics, rendering pipeline, geometry,
J McQuillan SE204: 2004/2005: Lecture 4slide 1 The Graphics Class Used when we need to draw to the screen Two graphics classes –Graphics –Graphics2D.
Graphics Copyright © 2015 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Java Methods Object-Oriented Programming and Data.
-Sai Phalgun Tatavarthy. Outline What is a “Graphing Calculator”? Graphics, Graphics2D classes Methods of Graphics object Custom Painting The paintComponent()
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Introduction to Android (Part.
Agenda Java Coordinate Systems. Graphics Class. Drawing on Panels. Drawing Shapes.
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
Graphics basic 1. 2 Objectives Understand Java coordinate systems. Draw things using the methods in the Graphics class. Override the paintComponent method.
Introduction to Java Chapter 8 - Introduction to Java Graphics1 Chapter 8 Introduction to Java Graphics.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Graphics and Java2D Chapter Java Coordinate System Origin is in _____________ corner –Behind title bar of window X values increase to the ________.
CSI 3125, Preliminaries, page 1 AWT. CSI 3125, Preliminaries, page 2 AWT Java AWT (Abstract Windowing Toolkit) is an API to develop GUI or window-based.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Getting Started with GUI Programming Chapter 10 CSCI 1302.
AWT Java AWT (Abstract Windowing Toolkit) is an API to develop GUI or window-based application in java. Java AWT components are platform-dependent i.e.
Advanced Java Screen Update Techniques SD’98 - Session 4406 Ted Faison Faison Computing Inc.
Basic Graphics 03/03/16 & 03/07/16 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Projects: not limited to spec Error checking File filters Create multiple file formats Polygons Filled shapes Etc.
6.1 Coordinates The screen of a computer is a grid of little squares called pixels. The color of each pixel can be set individually, and drawing on the.
CPCS 391 Computer Graphics Lab One. Computer Graphics Using Java What is Computer Graphics: Computer graphics are graphics created using computers and,
Graphics JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight.
Graphics Drawing Things With Java. Today's Topics Course Updates Java Graphics –Java 1.1 versus Java 1.2 (Java 2) Drawing Lines Drawing Shapes.
12 Graphics and Java 2D™.
Drawing Geometric Objects
Lecture 5 Basic geometric objects
Java Graphics CS 2511.
Basic Graphics Chapter 5 3/19/15 Thursday Section Only
Graphics Applets By Mr. Dave Clausen
Chapter 49 Java 2D.
Chapter 13 Graphics.
Graphical Interface Commands
Chapter 13 Graphics.
Presentation transcript:

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

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.

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

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 }

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

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()

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

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)

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

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)

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

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

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

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

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

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

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

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

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

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

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 )

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

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

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

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

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)

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

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

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

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

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

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

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