Скачать презентацию Java Two Dimensional Graphics with Affine Transforms Sasank Скачать презентацию Java Two Dimensional Graphics with Affine Transforms Sasank

86337413ee8b3e9e8ed8c1e53261e725.ppt

  • Количество слайдов: 57

Java Two Dimensional Graphics with Affine Transforms Sasank Upadrasta Java Two Dimensional Graphics with Affine Transforms Sasank Upadrasta

Contents • • • Introduction to 2 D using Java Drawing Geometric Primitives What Contents • • • Introduction to 2 D using Java Drawing Geometric Primitives What are Affine Transforms Java Frame Works – affine References

Introduction to 2 D using Java • Java 2 D API includes these packages Introduction to 2 D using Java • Java 2 D API includes these packages java. awt. image java. awt. color java. awt. font java. awt. geom java. awt. print java. awt. renderable

Shapes and Areas • Shapes - create standard shape objects and draw them in Shapes and Areas • Shapes - create standard shape objects and draw them in one method call instead of drawing each segment with multiple method calls. The shapes include rectangles and ellipses and also arbitrary shapes such as the Polygon class. The shape classes implement the Shape interface and provide a Path. Iterator that steps through the segments of the given shape. • Areas - combine shapes in different ways to create new areas.

Strokes and Paint • Stroke - vary the thickness of a line, produce arbitrary Strokes and Paint • Stroke - vary the thickness of a line, produce arbitrary dot-dash sequences, select how corners and line ends are shaped, etc. • Paint - fill the interiors of shapes, as well as outlines, not just with solid colors but with – gradients of one color to another, and – textured patterns of tiled images.

Affine Transformations • Transformations - affine transformations (i. e. those geometrical transformations that keep Affine Transformations • Transformations - affine transformations (i. e. those geometrical transformations that keep parallel lines parallel) can take a shape and translate it, rotate it, shear it, compress or expand it. Multiple transformations can be compounded together.

Compositing and Clipping • Compositing - overlay shapes and images according to several optional Compositing and Clipping • Compositing - overlay shapes and images according to several optional rules that take advantage of the alpha transparency. • Clipping - to speed up performance, only redraw the portion of the display that lies within a given outline or clip. With Java 2 d one can use arbitrary clip outlines rather than just rectangles.

Text, Color and Images • Text - vast array of text rendering capabilities that Text, Color and Images • Text - vast array of text rendering capabilities that range from the basic draw. String() method to creating your own editor from scratch. • Color - lots of tools to help render colors in a consistent manner regardless of the device on which they are displayed. • Images - use many of the above tools and techniques that are used with shapes - e. g. transformations, compositing, clipping etc. - on images.

Image Filters and Printing • Image processing - apply image filters - edge enhancement, Image Filters and Printing • Image processing - apply image filters - edge enhancement, blurring, inversion, etc. are available. • Printing - print the graphics display to the local printer as shown on the monitor.

Draw, then Display • The typical Java 2 D process begins with method invocations Draw, then Display • The typical Java 2 D process begins with method invocations to draw shapes and strokes on what can be thought of as an offscreen image surface with arbitrarily fine resolution. A rasterizer converts this offscreen image to a 2 D array of pixel or point values, which can be rendered on a monitor screen or a printer.

The Graphics class • The vast majority of uses for the Java 2 D The Graphics class • The vast majority of uses for the Java 2 D API utilize a small subset of its capabilities encapsulated in the java. awt. Graphics class. Most methods of the Graphics class can be divided into two basic groups:

1. Draw and fill methods, enabling you to render basic shapes, text, and images 1. Draw and fill methods, enabling you to render basic shapes, text, and images 2. Attributes setting methods, which affect how that drawing and filling appears • Methods such as set. Font and set. Color define how draw and fill methods render.

Drawing method examples • draw. String – For drawing text g. draw. String( Drawing method examples • draw. String – For drawing text g. draw. String("Hello", 10); • draw. Image – For drawing images g. draw. Image(img, 0, 0, width, height, 0, 0, image. Width, image. Height, null); • draw. Line, draw. Arc, draw. Rect, draw. Oval, draw. Polygon – For drawing geometric shapes g 2. draw(new Line 2 D. Double(0, 0, 30, 40));

Image scaling • You can choose one of several methods in the Graphics class Image scaling • You can choose one of several methods in the Graphics class based on whether you want to render the image at the specified location in its original size or scale it to fit inside the given rectangle

Transparency and Fill • You can fill the transparent areas of the image with Transparency and Fill • You can fill the transparent areas of the image with color or keep them transparent • Fill methods apply to geometric shapes and include fill. Arc, fill. Rect, fill. Oval, fill. Polygon.

2 D Coordinates • Whether you draw a line of text or an image, 2 D Coordinates • Whether you draw a line of text or an image, remember that in 2 D graphics every point is determined by its x and y coordinates. All of the draw and fill methods need this information which determines where the text or image should be rendered.

Using x, y Coordinates • For example, to draw a line, an application calls Using x, y Coordinates • For example, to draw a line, an application calls the following: java. awt. Graphics. draw. Line(int x 1, int y 1, int x 2, int y 2) In this code (x 1, y 1) is the start point of the line, and (x 2, y 2) is the end point of the line. • So the code to draw a horizontal line is as follows: Graphics. draw. Line(20, 100, 120, 100);

Weather Painter class This code draws an image determined by using the setup. Weather. Weather Painter class This code draws an image determined by using the setup. Weather. Report() method. . orig. Composite = g 2. get. Composite(); if (alpha 0 != null) g 2. set. Composite(alpha 0); g 2. draw. Image(img 0, 0, 0, size. width, size. height, 0, 0, img 0. get. Width(null), img 0. get. Height(null), null); if (img 1 != null) { if (alpha 1 != null) g 2. set. Composite(alpha 1); g 2. draw. Image(img 1, 0, 0, size. width, size. height, 0, 0, img 1. get. Width(null), img 1. get. Height(null), null); }. . .

Weather example (continued) The set. Font and draw. String methods render the temperature and Weather example (continued) The set. Font and draw. String methods render the temperature and the weather condition. . // Freezing, Cold, Cool, Warm, Hot, // Blue, Green, Yellow, Orange, Red Font font = new Font("Serif", Font. PLAIN, 36); g. set. Font(font); String temp. String = feels + " " + temperature+"F"; Font. Render. Context frc = ((Graphics 2 D)g). get. Font. Render. Context(); . . .

Weather example (continued) • The set. Font and draw. String methods render the temperature Weather example (continued) • The set. Font and draw. String methods render the temperature and the weather condition. . g. set. Color(text. Color); int x. Text. Temp = r. X-(int)bounds. Temp. get. X(); int y. Text. Temp = r. Y-(int)bounds. Temp. get. Y(); g. draw. String(temp. String, x. Text. Temp, y. Text. Temp); int x. Text. Cond = r. X-(int)bounds. Cond. get. X(); int y. Text. Cond = r. Y-(int)bounds. Cond. get. Y() + (int)bounds. Temp. get. Height(); g. draw. String(cond. Str, x. Text. Cond, y. Text. Cond);

Introduction to 2 D using Java (contd …) • The fill. Rect method allows Introduction to 2 D using Java (contd …) • The fill. Rect method allows you to draw a rectangle filled with the specified color. . Rectangle 2 D bounds. Temp = font. get. String. Bounds(temp. String, frc); Rectangle 2 D bounds. Cond = font. get. String. Bounds(cond. Str, frc); int w. Text = Math. max((int)bounds. Temp. get. Width(), (int)bounds. Cond. get. Width()); int h. Text = (int)bounds. Temp. get. Height() + (int)bounds. Cond. get. Height(); int r. X = (size. width-w. Text)/2; int r. Y = (size. height-h. Text)/2; g. set. Color(Color. LIGHT_GRAY); g 2. fill. Rect(r. X, r. Y, w. Text, h. Text); . . .

Geometric Primitives • The Java 2 D™ API provides several classes that define common Geometric Primitives • The Java 2 D™ API provides several classes that define common geometric objects such as points, lines, curves, and rectangles. These geometry classes are part of the java. awt. geom package. • The Path. Iterator interface defines methods for retrieving elements from a path.

Shape Interface • The Shape interface provides a set of methods for describing and Shape Interface • The Shape interface provides a set of methods for describing and inspecting geometric path objects. This interface is implemented by the General. Path class and other geometry classes.

Geometric Primitives • All examples represented in this section create geometries by using java. Geometric Primitives • All examples represented in this section create geometries by using java. awt. geom and then render them by using the Graphics 2 D class. To begin you obtain a Graphics 2 D object, for example by casting the Graphics parameter of the paint() method. public void paint (Graphics g) { Graphics 2 D g 2 = (Graphics 2 D) g; . . . }

Point • The Point class creates a point representing a location in (x, y) Point • The Point class creates a point representing a location in (x, y) coordinate space. The subclasses Point 2 D. Float and Point 2 D. Double provide correspondingly float and double precision for storing the coordinates of the point. //Create Point 2 D. Double point = new Point 2 D. Double(x, y);

Point Locations • To create a point with the coordinates 0, 0 you use Point Locations • To create a point with the coordinates 0, 0 you use the defaultconstructor, Point 2 D. Double(). You can use the set. Location method to set the position of the point as follows: set. Location(double x, double y) – To set the location of the point- defining coordinates as double values

Set Location • set. Location(Point 2 D p) – To set the location of Set Location • set. Location(Point 2 D p) – To set the location of the point using the coordinates of another point. Also, the Point 2 D class has methods to calculate the distance between the current point and a point with given coordinates, or the distance between two points.

Line • The Line 2 D class represents a line segment in (x, y) Line • The Line 2 D class represents a line segment in (x, y) coordinate space. The Line 2 D. Float and Line 2 D. Double subclasses specify lines in float and double precision. For example: // draw Line 2 D. Double g 2. draw(new Line 2 D. Double(x 1, y 1, x 2, y 2));

Set Line methods • This class includes several set. Line() methods to define the Set Line methods • This class includes several set. Line() methods to define the endpoints of the line. Alternatively, the endpoints of the line could be specified by using the constructor for the Line 2 D. Float class as follows: – Line 2 D. Float(float X 1, float Y 1, float X 2, float Y 2) – Line 2 D. Float(Point 2 D p 1, Point 2 D p 2) • Use the Stroke object in the Graphics 2 D class to define the stroke for the line path.

Ellipse • The Ellipse 2 d class represents an ellipse defined by a bounding Ellipse • The Ellipse 2 d class represents an ellipse defined by a bounding rectangle. The Ellipse 2 D. Float and Ellipse 2 D. Double subclasses specify an ellipse in float and double precision. Ellipse is fully defined by a location, a width and a height. For example: // draw Ellipse 2 D. Double g 2. draw(new Ellipse 2 D. Double(x, y, rectwidth, rectheight));

Rectangle • Classes that specify primitives represented in the following example extend the Rectangular. Rectangle • Classes that specify primitives represented in the following example extend the Rectangular. Shape class, which implements the Shape interface and adds a few methods of its own. These methods enables you to get information about a shape’s location and size, to examine the center point of a rectangle, and to set the bounds of the shape.

Rectangle • The Rectangle 2 D class represents a rectangle defined by a location Rectangle • The Rectangle 2 D class represents a rectangle defined by a location (x, y) and dimension (w x h). The Rectangle 2 D. Float and Rectangle 2 D. Double subclasses specify a rectangle in float and double precision. For example: // draw Rectangle 2 D. Double g 2. draw(new Rectangle 2 D. Double(x, y, rectwidth, rectheight));

Rounded Rectangle • The Round. Rectangle 2 D class represents a rectangle with rounded Rounded Rectangle • The Round. Rectangle 2 D class represents a rectangle with rounded corners defined by a location (x, y), a dimension (w x h), and the width and height of the corner arc. The Round. Rectangle 2 D. Float and Round. Rectangle 2 D. Double subclasses specify a round rectangle in float and double precision.

Specifying Rounded Rectangle • The rounded rectangle is specified with following parameters: – – Specifying Rounded Rectangle • The rounded rectangle is specified with following parameters: – – – Location Width Height Width of the corner arc Height of the corner acr

Rounded Rectangle location • To set the location, size, and arcs of a Round. Rounded Rectangle location • To set the location, size, and arcs of a Round. Rectangle 2 D object, use the method set. Round. Rect(double a, double y, double w, double h, double arc. Width, double arc. Height). For example: // draw Round. Rectangle 2 D. Double g 2. draw(new Round. Rectangle 2 D. Double(x, y, rectwidth, rectheight, 10));

Drawing Geometric Primitives (Contd …) • The example at http: //java. sun. com/docs/books/tutorial/2 d/geometry/e Drawing Geometric Primitives (Contd …) • The example at http: //java. sun. com/docs/books/tutorial/2 d/geometry/e xamples/Shapes. Demo 2 D. java code contains implementations off all the described geometric primitives. • For more information about classes and methods represented in this section see the http: //java. sun. com/javase/6/docs/api/java/awt/geom/ package-summary. html specification.

What is an Affine Transform? • The Affine Transform class represents a 2 D What is an Affine Transform? • The Affine Transform class represents a 2 D Affine transform that performs a linear mapping from 2 D coordinates to other 2 D coordinates that preserves the "straightness" and "parallelness" of lines. Affine transformations can be constructed using sequences of translations, scales, flips, rotations, and shears.

What is affine Transform (contd…) • An Affine Transform is a linear transform, so What is affine Transform (contd…) • An Affine Transform is a linear transform, so the transformation can be expressed in the matrix notation of linear algebra. An arbitrary Affine Transform can be mathematically expressed by six numbers in a matrix like this: sx shx tx shy sy ty • The symbols or factors sx and sy are scale factors that accomplish scaling. The factors tx and ty are scale factors that accomplish translation. The factors shx and shy are scale factors that accomplish shear.

What is affine Transform (contd…) • Your graphics can be transformed in the following What is affine Transform (contd…) • Your graphics can be transformed in the following ways before being displayed on the output device: Scaling Translation Shear Rotation (rotation is a combination of scaling and shearing, so there are not separate rx and ry numbers. )

Scaling • Scaling is perhaps the easiest of the four types of transforms to Scaling • Scaling is perhaps the easiest of the four types of transforms to understand. This simply means that if a point is located at a horizontal coordinate value of x in user space, it will be located at sx*x in device space, where sx is a numeric multiplier that can be either positive or negative.

Translation § Translation moves the origin of the coordinate system in device space. § Translation § Translation moves the origin of the coordinate system in device space. § Another use of translation (in combination with scaling) is to flip the default positive direction of the vertical axis so that increasing positive values go up instead of down, which is the default.

Shear • Jonathan Knudsen describes shear in his book entitled Java 2 D Graphics Shear • Jonathan Knudsen describes shear in his book entitled Java 2 D Graphics as: “Take a stack of paper (like you might place in your laser printer) and draw a picture on the side of the stack. Then deform the stack so that the side of the stack on which you drew the picture takes the form of a parallelogram. Be especially careful to keep the opposite sides parallel. Then look at your picture. It will have been subjected to shear in one dimension. ”

Affine Transform class • public class Affine. Transform extends Object implements Cloneable, Serializable • Affine Transform class • public class Affine. Transform extends Object implements Cloneable, Serializable • The Affine. Transform class represents a 2 D affine transform that performs a linear mapping from 2 D coordinates to other 2 D coordinates that preserves the "straightness" and "parallelness" of lines. Affine transformations can be constructed using sequences of translations, scales, flips, rotations, and shears.

Affine Transforms as matrix multiplications • Such a coordinate transformation can be represented by Affine Transforms as matrix multiplications • Such a coordinate transformation can be represented by a 3 row by 3 column matrix with an implied last row of [ 0 0 1 ]. This matrix transforms source coordinates (x, y) into destination coordinates (x', y') by considering them to be a column vector and multiplying the coordinate vector by the matrix according to the following process: [ x'] [ y'] = [ 1 ] [ [ [ m 00 m 10 0 m 01 m 11 0 m 02 m 12 1 ] [ x ] [ m 00 x + m 01 y + m 02 ] ] [ y ] = [ m 10 x + m 11 y + m 12 ] ] [ 1 ]

Rotation • Rotation is easy to visualize (but the combination of rotation and shear Rotation • Rotation is easy to visualize (but the combination of rotation and shear can be very difficult to visualize). To visualize rotation, draw a picture on a piece of paper. Use a single tack to attach it to a bulletin board. Then rotate the paper a few degrees around the tack. You will have rotated the picture around the coordinates of the tack by the specified number of degrees.

Rotation in Java 2 D • Java 2 D works the same way. In Rotation in Java 2 D • Java 2 D works the same way. In the process of transforming a graphic from user space to device space, you can cause it to be rotated by a specified angle around a specified coordinate position. • However, in Java 2 D, the angle must be specified in radians instead of degrees. If you are unfamiliar with the use of radian measure to specify an angle, just remember the following identity:

Radiand Degrees • PI radians = 180 degrees • PI is the numeric value Radiand Degrees • PI radians = 180 degrees • PI is the numeric value you learned about in geometry class. • PI has a value of 3. 14159. . . However, you don’t have to remember the value of PI. It is available as a static final constant in the Math class. You can access the value as Math. PI.

Dividing PI Radians • If this sounds confusing, you might also want to remember Dividing PI Radians • If this sounds confusing, you might also want to remember the following identities: • PI/2 = 90 degrees PI/4 = 45 degrees PI/8 = 22. 5 degrees PI/16 = 11. 25 degrees, etc.

Rotation direction • Positive angles of rotation proceed in a clockwise direction. This may Rotation direction • Positive angles of rotation proceed in a clockwise direction. This may be backwards from what you are accustomed to in various science and engineering endeavors where counter-clockwise rotation is often thought of as the positive direction of rotation.

Java Frame Works - affine • Java's Abstract Window Toolkit (AWT) provides a Graphics Java Frame Works - affine • Java's Abstract Window Toolkit (AWT) provides a Graphics class with methods for drawing lines, ovals, rectangles, text, polygons, and other shapes. A Panel, Frame, or other AWT component draws itself using the Graphics instance passed to its paint method. The GUI environment automatically calls this method whenever the component needs redrawing.

Java Frame Works – affine • Sun added the Java 2 D framework on Java Frame Works – affine • Sun added the Java 2 D framework on top of the original AWT Graphics class, adding many new features to the Java core API. One important Java 2 D feature is known as the affine transformation. It works by transforming the coordinate system before any drawing is done on the Graphics object.

JFrame example • To illustrate this, we'll skew the coordinates with a shearing transformation JFrame example • To illustrate this, we'll skew the coordinates with a shearing transformation and draw some text and graphics to see how it looks with and without the transformation. First, let's draw some text, using the default coordinates, We'll do this in the paint method of a JFrame:

Example (page 1) import java. awt. Font; import java. awt. Graphics 2 D; import Example (page 1) import java. awt. Font; import java. awt. Graphics 2 D; import javax. swing. JFrame; public class My. Transform extends JFrame { public My. Transform(String title) { super(title); } public void paint(Graphics g) { Graphics 2 D g 2 d = (Graphics 2 D) g; g 2 d. set. Font(Font. decode("arial-bold-36")); g 2 d. draw. String("Affinity", 100); g 2 d. draw. Oval(100, 150, 35, 50); g 2 d. draw. Oval(135, 150, 35, 50); }

Example (page 2) public static void main(String[] args) { My. Transform aft = new Example (page 2) public static void main(String[] args) { My. Transform aft = new My. Transform("Affinity"); aft. set. Size(300, 300); aft. set. Default. Close. Operation(JFrame. EXIT_ON_CLOSE); aft. set. Visible(true); } }

Java affine Frameworks • In all affine transformations , the change affects the entire Java affine Frameworks • In all affine transformations , the change affects the entire coordinate system and not just individual objects. We call the methods to apply the transforms before calling the drawing methods. Let's display the above text with a shearing transform, This is a transform where lines are shifted in a new direction but parallel lines still remain parallel. The call to the shear method happens before the drawing occurs. Here is code to perform the transform:

Modifications to Example public void paint(Graphics g) { Graphics 2 D g 2 d Modifications to Example public void paint(Graphics g) { Graphics 2 D g 2 d = (Graphics 2 D) g; g 2 d. shear(0. 5, 0. 5); g 2 d. set. Font(Font. decode("arial-bold-36")); g 2 d. draw. String("Affinity", 100); g 2 d. draw. Oval(100, 150, 35, 50); g 2 d. draw. Oval(135, 150, 35, 50); }

References http: //java. sun. com/ http: //www. developer. com/ http: //www. particle. kth. se/htmlnew/ References http: //java. sun. com/ http: //www. developer. com/ http: //www. particle. kth. se/htmlnew/