Download
java 2d graphics n.
Skip this Video
Loading SlideShow in 5 Seconds..
Java 2D Graphics PowerPoint Presentation
Download Presentation
Java 2D Graphics

Java 2D Graphics

660 Views Download Presentation
Download Presentation

Java 2D Graphics

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Java 2D Graphics 2D API http://java.sun.com/docs/books/tutorial/index.html

  2. Java 2D • Java.awt, java.awt.image • Java.awt.color • Java.awt.font • Java.awt.geom • Java.awt.print • Java.awt.renderable • Java.sun.image.codes.jpeg

  3. What can Java 2D do • C:\Program Files\Java\jdk1.5.0_06\demo\jfc\Java2D\Java2Demo • Draw lines of any thickness • Fill shapes with gradients and textures • Move, rotate, scale, and shear text and graphics • Composite overlapping text and graphics

  4. Main techniques • Shapes • Stroking • Filling • Transformation • Alpha component • Clipping • Antialiasing - for jagged edges • Text • Color • Images • Image processing • Printing

  5. Drawing public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; // 2d staff } public void drawOnImage(Image i) { Graphics g = i.getGraphics(); // draw on image }

  6. import java.awt.*; import java.awt.event.*; public class ApplicationFrame extends Frame { public ApplicationFrame() { this("ApplicationFrame v1.0"); } public ApplicationFrame(String title) { super(title); createUI(); } protected void createUI() { setSize(500, 400); center(); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { dispose(); System.exit(0); } }); } public void center() { Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension frameSize = getSize(); int x = (screenSize.width - frameSize.width) / 2; int y = (screenSize.height - frameSize.height) / 2; setLocation(x, y); } }

  7. Java 2D Rendering • The Java 2D API introduces java.awt.Graphics2D , a new type of Graphics object. Graphics2D extends the Graphics class to provide access to the enhanced graphics and rendering features of the Java 2D API. • To use Java 2D API features, you cast the Graphics object passed into a component's rendering method to a Graphics2D object. • public void Paint (Graphics g) { Graphics2D g2 = (Graphics2D) g; ... }

  8. Rendering pipeline • Paint • Stroke • Font • Transformation • Composition rule • Clipping shape • Rendering hints

  9. Rendering pipeline

  10. Coordinate Systems The Java 2D system maintains two coordinate spaces. • User space is the space in which graphics primitives are specified. User space is a device-independent logical coordinate system: the coordinate space that your program uses. All geometries passed into Java 2D rendering routines are specified in user-space coordinates. • Device space is the coordinate system of an output device, such as a screen, window, or a printer. Device space is a device-dependent coordinate system that varies according to the target rendering device. Although the coordinate system for a window or the screen might be very different from that of a printer, these differences are invisible to Java programs. The necessary conversions between user space and device space are performed automatically during rendering. • When the default transformation from user space to device space is used, the origin of user space is the upper-left corner of the component's drawing area. The x coordinate increases to the right, and the y coordinate increases downward, as shown in the following figure.

  11. Alpha

  12. Graphics2D provides rendering methods for Shapes, Text, and Images: • Draw - strokes a Shape's path using the Stroke and Paint objects in the Graphics2D context. • Fill - fills a Shape using the Paint in the Graphics2D context. • drawString - renders the specified text string using the Paint in the Graphics2D context. • drawImage -renders the specified image.

  13. Geometry Points java.awt.geom.Point2D Points setLocation Abstract return x, y distance Constructors – Point2D.Float, Point2D.Double

  14. Shapes

  15. Shapes

  16. Rectangular Shapes • The Rectangle2D, RoundRectangle2D, Arc2D, and Ellipse2D primitives are all derived from RectangularShape, which defines methods for Shape objects that can be described by a rectangular bounding box. The geometry of a RectangularShape can be extrapolated from a rectangle that completely encloses the outline of the Shape. • StringArt

  17. QuadCurve2D and CubicCurve2D • The QuadCurve2D class allows you to create quadratic parametric curve segments. A quadratic curve is defined by two endpoints and one control point. • The CubicCurve2D class allows you to create cubic parametric curve segments. A cubic curve is defined by two endpoints and two control points. The following figures demonstrate examples of quadratic and cubic curves. • DragKing

  18. GeneralPath • The GeneralPath class enables you to construct an arbitrary shape by specifying a series of positions along the shape's boundary. These positions can be connected by line segments, quadratic curves, or cubic (Bézier) curves. The shape pictured below can be created with three line segments and a cubic curve.

  19. Areas • With the Area class you can perform Boolean operations, such as union, intersection, and subtraction, on any two Shape objects. This technique, often referred to as constructive area geometry, enables you to quickly create complex Shape objects without having to describe each line segment or curve.

  20. Constructive Area Geometry Constructive Area Geometry (CAG) is the process of creating new geometric objects by performing boolean operations on existing objects. In the Java 2D API, a special type of Shape called an Area supports boolean operations. You can c construct an Area from any Shape. Areas support the following Boolean operations: * Union * Intersection * Subtraction * Exclusive OR (XOR) CombiningShapes

  21. Painting and Stroking • Painting is the process of filling the interior of the shape with a color, color gradient, or texture. • Stroking is the process of drawing the shape's outline. You can draw an outline using different line widths, line styles, and colors. • PaintingAndStroking

  22. Painting • First, tell the Graphics2D how to fill shapes with a call to setPaint(). This method accepts any object that implements the java.awt.Paint interface. The Graphics2D stores the Paint away as part of its state. When it comes time to fill a shape, Graphics2D will use the Paint to determine what colors should be used to fill the shape. The 2D API comes with three kinds of "canned" paints: solid colors, a linear color gradient, and a texture fill. You can add your own Paint implementations if you wish. • Now you can tell Graphics2D to fill a shape by passing it to fill().

  23. Solid Colors

  24. Three shapes and three paints

  25. public Color (int r, int g, int b) This constructor creates a new Color using the specified values for red, green and blue. The values should range from 0 to 255, inclusive. • public Color (float r, float g, float b) This constructor creates a new Color using the specified values for red, green and blue. The values should range from 0.0 to 1.0, inclusive.

  26. Swing's Color Chooser Dialog • If you want your users to be able to choose colors in your application, you're in luck. Swing has a ready-made dialog for this purpose. The name of the class is javax.swing.JColorChooser. You can use this dialog with one line of code, using the following static method: • public static Color showDialog(Component component, String title, Color initialColor) • This method displays a color chooser dialog. The supplied Component is used as the parent component of the dialog. The dialog will have the supplied title; its controls will be initialized to show the given initialColor. • RedTown example

  27. GradientPaint • A gradient is a smooth transition from one color to another. In the late evening on a clear day, the sky has a gradient from dark blue at the horizon to black overhead. The 2D API provides an implementation of a simple color gradient, called java.awt.GradientPaint. This class defines a color gradient using two points and two colors. The gradient smoothly shifts from one color to the other as you move along the line that connects the two points, which I'll call the gradient line. The GradientPaint creates parallel bands of color, perpendicular to the gradient line

  28. GradientPaint • To create a GradientPaint, you simply need to supply two points and two Colors. By default, GradientPaints are acyclic. • public GradientPaint (Point2D pt1, Color color1, Point2D pt2, Color color2) • public GradientPaint (float x1, float y1, Color color1, float x2, float y2, Color color2) • These constructors create acyclic GradientPaints. The gradient runs from color1 at the first point to color2 at the second point. • public GradientPaint (Point2D pt1, Color color1, Point2D pt2, Color color2, boolean cyclic) • public GradientPaint(float x1, float y1, Color color1, float x2, float y2, Color color2, boolean cyclic) • These constructors create GradientPaints that run from color1 at the first point to color2 at the second point. If the cyclic parameter is true, the gradient will be cyclic. • GradientPaintFill example

  29. You can retrieve the parameters of the gradient paint with the following methods: • public Point2D getPoint1() • public Color getColor1() • public Point2D getPoint2() • public Color getColor2() • public boolean isCyclic()

  30. TexturePaint • The third type of Paint is a texture fill. In the 2D API, a texture is created using an image that is repeated over and over, like a kitchen floor tile. The java.awt.TexturePaint class represents a texture. You can construct a TexturePaint with two pieces of information: • The image to be used should be supplied as a BufferedImage. For now, just think of a BufferedImage as a rectangular picture. • A Rectangle2D specifies how the image will be replicated to form the texture. I'll call this the texture rectangle. • TexturePaintFill example

  31. The Transparency interface

  32. The Paint interface

  33. The PaintContext interface