Java Graphics2D class plotting method, javagraphics2d

Source: Internet
Author: User

Java Graphics2D class plotting method, javagraphics2d

Graphics2D is inherited from Graphics. It extends Graphics's drawing function and has more powerful 2D graphic processing capabilities. It provides more precise control over geometric shapes, coordinate coordinates, color management, and text layout.

Graphics2D defines several methods for adding or changing the State attributes of a graph. You can set and modify the State attributes to specify the connection mode between the width of the paint brush and the paint brush, set the translation, rotation, scaling, or trimming, and set the color and pattern of the filled image.

The graphic state attributes are stored with specific objects.

 

Common drawing attributes

Stroke attributes

You can call the following method to set the stroke attribute:
Public abstract void setStroke (Stroke s)

The stroke attribute is used to control the width, pen style, line segment connection mode, or dash pattern of a line.

If you want to set the stroke attribute, you should first create an object referenced by the Stroke. However, because the Stroke is of the interface type, you can create an object with its known implementation class BasicStroke.

The following methods are commonly used to construct BasicStroke:
// Construct a solid BasicStroke with the specified attribute
Public BasicStroke (float width, int cap, int join)

// Construct a solid BasicStroke with the specified line width and the default cap and join Style
Public BasicStroke (float width)

Cap is an endpoint style, which can be set to CAP_BUTT (unmodified), CAP_ROUND (the end of the circular decoration with the radius equal to half the width of the paint brush), and CAP_SQUARE (the end of the square, default value ).

Join is the connection method used to define the intersection of two line segments: JOIN_BEVEL (without modification), JOIN_MITER (sharp end, default value), JOIN_ROUND (circle end ).

Clip attributes

The clip attribute is used to achieve the cropping effect. You can use the following method to determine the Shape of the cropping area when setting the cropping property:
Public abstract void setClip (int x, int y, int width, int height)

Multiple setClip () consecutive times to obtain the cropping area of their intersection.

Paint attributes

The paint attribute controls the filling effect and is set by calling the setPaint () method:
Public abstract void setPaint (Paint paint)

Paint can work on edges and fill at the same time. It can be a monochrome, gradient, and pattern. Any paint must implement the java. awt. Paint interface. Because the Color class implements the java. awt. Paint interface, all Color objects are painting objects.

 

GradientPaint class:
Public GradientPaint (float x1, float y1, Color color1,
Float x2, float y2, Color color2)

This class fills an area with a color gradient, and the constructor specifies the ratio and color. The graphics engine linearly changes two colors from the first to the second: From (x1, y1) to (x2, y2) from c1 to c2. We can also specify whether the color pattern can be repeated.

 

TexturePaint class:
Public TexturePaint (BufferedImage txtr, Rectangle2D anchor)

This class tiles an image to fill the image. The constructor receives a java. awt. image. BufferedImage and a Rectangle2D, shadows the image into the rectangle, and then tiles the rectangle.

We can create GradientPaint or TexturePaint objects first, and then call the setPaint () method of Graphics2D to set the fill effect.

Font attributes

All texts are rendered using style graphics that can represent texts. The current font determines the font shape. Use the getFont () method inherited from java. awt. Graphics and setFont () method to manipulate the font. Despite the relatively simple work of setting fonts, Java 2D provides rich options for text profiling.

Transform attributes

The transform attribute is used to perform common operations such as image translation, scaling, and oblique cutting. We can call setTransform () to set the transform attribute:
Public abstract void setTransform (AffineTransform Tx)

This method requires a parameter of the AffineTransform object. Therefore, first create the AffineTransform object, and then call the setTransform () method to set the transform attribute. Finally, draw a graph with a Graphics2D object with the specified attribute.

The methods for creating an AffineTransform object are as follows:
// Rotating and transforming, rotating theta radians
Public static AffineTransform getRotateInstance (double theta)

// Rotate around the center (anchorx, anchory)
Public static AffineTransform getRotateInstance (double theta, double anchorx,
Double anchory)

// Scale and convert the x and y directions according to the sx and sy ratios respectively.
Public static AffineTransform getScaleInstance (double sx, double sy)

// Mistangent transformation. shx and shy specify the diagonal degree.
Public static AffineTransform getShearInstance (double shx, double shy)

// Translation transformation. tx and ty indicate the translation distance between x and y.
Public static AffineTransform getTranslateInstance (double tx, double ty)

Of course, you can also create an AffineTransform object without the transform attribute, and then specify the image translation, rotation, and scaling attributes using the following methods:
// Translate the image to tx pixels in the X axis and ty pixels in the Y axis.
Public void translate (double tx, double ty)

// Rotate theta radians
Public void rotate (double theta)

// The image uses anchorx (anchory) as the axis point and rotates theta radians.
Public void rotate (double theta, double anchorx, double anchory)

// The image scales sx times in the X axis and sy times vertically.
Public void scale (double sx, double sy)

Composit attributes

The composit attribute sets the effect of overlapping areas. You can set this attribute by calling setComposite:
Public abstract void setComposite (Composite comp)

For example, first use method AlphaComposite. getInstance (int rule, float alpha) to obtain the AlphaComposite object, and then use setComposite () to set the mixed effect. AlphaComposite achieves blending and transparency in images and images. The Alpha value ranges from 0.0f (completely transparent) to 1.0f (completely opaque ).

 

Drawing with Graphics2D

The Graphics2D class still retains the drawing method of the Graphics class and adds many new methods. The new method draws a geometric image (line segment, circle, etc.) as an object. Classes declared in the java. awt. geom package are used to create various body graphics objects. Common causes include:
? Line2D-Line Segments
? RoundRectangle2D-rounded rectangle class
? Ellipse2D-elliptic class
? Arc2D-Arc Type
? QuadCurve2D-quadratic curve type
? CubicCurve2D-cubic curve class.

Use a new method of the Graphics2D class to draw a graph. In the paintComponent () or painting () method, forcibly convert the parameter object g to A Graphics2D object. Then, use the static internal class constructor Double () provided by the preceding graphic class () or Float () to create the image object. Finally, the drawing object is drawn using the draw () method of Graphics2D object as the parameter.

Draw a line segment:
/* Draw line */
Stroke s = new BasicStroke (20, BasicStroke. CAP_ROUND, BasicStroke. JOIN_MITER );
G2d. setStroke (s );
Line2D line = new Line2D. Double (30, 50, 100,200 );
G2d. draw (line );

Draw a rounded rectangle:
/* Draw a rounded rectangle */
G2d. setColor (Color. BLUE );
RoundRectangle2D rect = new RoundRectangle2D. Double (100, 50,100, 50,
4, 4 );
G2d. draw (rect );

Draw a rectangle with a certain rotation angle:
/* Draw a rectangle with a certain Rotation Angle */
Rectangle2D rect2 = new Rectangle2D. Double (60,160, 60, 90 );
AffineTransform transform = new AffineTransform ();
Transform. rotate (45 * Math. PI/180, 90,200); // rotate the image 45 degrees around (90,200) points
G2d. setTransform (transform );
G2d. draw (rect2 );

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.