On the basis of the Graphics class that provides various basic geometric figures, the Java language extends the Graphics class to provide a Graphics2D class, which has more powerful two-dimensional graphics processing capabilities and provides more accurate coordinate conversion, color management, and text layout. control.
Drawing properties
Graphics2D defines several methods for adding or changing the state properties of graphics. You can specify the brush width and the connection method of the brush by setting and modifying the state properties; setting the translation, rotation, scaling or trimming transformation graphics; and setting the color and pattern of the filled graphics, etc. Graphics state properties are stored using specific objects.
1. stroke attribute
The stroke property controls the width of the line, the pen style, the way segments are connected, or the dash pattern. To set this property, you need to create a BasicStroke object first and then call the setStroke() method to set it. The methods for creating BasicStroke objects are:
BasicStroke(float w): Specifies the line width w.
BasicStroke(float w,int cap, int join):
cap is an endpoint: CAP_BUTT (unmodified), CAP_ROUND (semi-circular end), CAP_SQUARE (square end, default value).
Join defines the connection method at the intersection of two line segments: JOIN_BEVEL (unmodified), JOIN_MTTER (pointed end, default value), JOIN_ROUND (rounded end).
2. paint attribute
The paint property controls the fill effect. First call the following method to determine the filling effect, and use the setPaint() method to set it.
GradientPaint(float x1,float y1,Color c1,float x2,flaot y2,Color c2): From (x1,y1) to (x2,y2) the color gradient from c1 to c2. Among them: parameters c1 and c2 determine the gradient color from color c1 to color c2. The parameters x1, y1, x2, y2 determine the strength of the gradient, that is, starting from point (x1, y1) to point (x2, y2), the color changes from c1 to c2.
GradientPaint(float x1, float y1, Color c1, float x2, float y2, Color c2, Boolean cyclic): If you want the gradient to end and be the color of the starting point, cyclic should be set to true.
3. transform attribute
The transform attribute is used to implement common transformation operations such as graphics translation, scaling, and beveling. First create an AffineTransform object, and then call the setTransform() method to set the transform attribute. Finally, the graphics is drawn using a Graphics2D object with specified properties. The methods for creating AffineTransform objects are:
You can also create an AffineTransform object without a transform attribute first, and then use the following methods to specify the graphics translation, rotation, and scaling transformation attributes.
For example, create an AffineTransform object:
AffineTransform trans = new AffineTransform();
Specify the around-point rotation transformation properties for the AffineTransform object:
Trans.rotate(50.0*3.1415927/180.0,90,80);
Then set the "brush" with the above rotation transformation function for the Graphics2D object g2d:
Graphics2D g2d = (Graphics2D)g;g2d.setTranstorm(trans);
Finally, the draw() method of the Graphics2D object with transformation function is called with the graphics object as a parameter. For example, assuming there is a quadratic curve object curve, the following code implements drawing this quadratic curve using the g2d object with the above rotation function:
g2d.draw(curve);
4. clip attribute
The clip attribute is used to achieve clipping effects. To set the clipping attribute, you can call the setClip() method to determine the Shape of the clipping area. Multiple setClip() operations are performed consecutively to obtain the clipping area where they intersect.
5. composit attribute
The composit attribute sets the effect of the overlapping area of graphics. First use the method AlphaComposite.getInstance(int rule, float alpha) to get the AlphaComposite object, and then set the mixing effect through the setComposite() method. Alpha values range from 0.0f (fully transparent) to 0.1f (fully opaque).
Drawing methods of Graphics2D class
The Graphics2D class still retains the drawing methods of the Graphics class, while adding many new methods. The new method draws geometric shapes (line segments, circles, etc.) as an object. A series of classes declared in the java.awt.geom package are used to create various body graphics objects. The main ones are:
Line2D line segment class, RoundRectangle2D rounded rectangle class, Ellipse2D ellipse class, Arc2D arc class, QuadCurve2D quadratic curve class, CubicCurve2D cubic curve class.
To draw a graphic using the new methods of the Graphics2D class. First, in the redraw method paintComponent() or paint(), force the parameter object g into a Graphics2D object; then, use the static method Double() provided by the above graphics class to create the object of the graphics; finally, use the graphics object as a parameter Call the draw() method of the Graphics2D object to draw this graphic. For example, the following code uses the new method of Graphics2D to draw line segments and rounded rectangles:
Graphics2D g2d = (Graphics2D)g;//Convert the object g type from Graphics to Graphics2D Line2D line = new Line2D.Double(30.0,30.0,340.0,30.0); g2d.draw(line); RoundRectangle2D rRect = new RoundRectangle2D.Double(13.0,30.0,100.0,70.0,40.0,20.0); g2d.draw(rRect);
You can also first use the Shape object provided by the java.awt.geom package, create a Shape object with single-precision Float coordinates or double-precision Double coordinates, and then draw it using the draw() method. For example, the following code creates an arc object and then draws the arc:
Shape arc = new Arc2D.Float(30,30,150,150,40,100,Arc2D.OPEN); g2d.draw(arc)//Draw the previously created graphics object arc
Geometry class of Graphics2D
line segment
Line2D line = new Line2D.Double(2,3,200,300);//Declare and create a line segment object//The starting point is (2, 3) and the end point is (200, 300)
rectangle
Rectangle2D rect = new Rectangle2D.Double(20,30,80,40);//Declare and create a rectangle object. The upper left corner of the rectangle is (20, 30), the width is 300, and the height is 40
rounded rectangle
RoundRectangle2D rectRound = new RoundRectangle2D.Double(20,30,130,100,18,15); //The upper left corner is (20, 30), the width is 130, the height is 100, the long axis of the rounded corner is 18, and the short axis is 15.
oval
Ellipse2D ellipse = new Ellipse2D.Double(20,30,100,50); //Upper left corner (20, 30), width is 100, height is 50
Arc
Arc2D arc1 = new Arc2D.Double(8,30,85,60,5,90,Arc2D.OPEN); //The upper left corner of the enclosing rectangle (10, 30), width 85, height 60, starting angle is 5 degrees , the ending angle is 90 degrees Arc2D arc2 = new Arc2D.Double(20,65,90,70,0,180,Arc2D.CHORD); Arc2D arc3 = new Arc2D.Double(40,110,50,90,0,270,Arc2D.PIE);
The parameters Arc2D.OPEN, Arc2D.CHORD, and Arc2D.PIE respectively indicate whether the arc is an open arc, a bow arc, or a pie arc.
Quadratic curve A quadratic curve is represented by a second-order polynomial:
y(x)=ax2+bx+c
A quadratic curve needs to be determined by three points: the starting point, the control point and the end point.
QuadCurve2D curve1 = new QuadCurver2D.Double(20,10,90,65,55,115); QuadCurve2D curve2 = new QuadCurver2D.Double(20,10,15,63,55,115); QuadCurve2D curve3 = new QuadCurver2D.Double(20,10,54,64,55,115);
The six parameters in the method Double() are the starting point, control point and end point of the quadratic curve. The starting points and end points of the above three quadratic curves are the same.
Cubic Curve A cubic curve is represented by a third-order polynomial:
y(x)=ax3+bx2+cx+d
A cubic curve requires four points to determine: the starting point, two control points and the end point.
CubicCurve2D curve1 = new CubicCurve2D.Double(12,30,50,75,15,15,115,93); CubicCurve2D curve2 = new CubicCurve2D.Double(12,30,15,70,20,25,35,94); CubicCurve2D curve3 = new CubicCurve2D.Double(12,30,50,75,20,95,95,95);
The eight parameters in the method Double() are the starting point, two control points and the end point of the cubic curve.
The drawing process of general equation curves is controlled by a loop. Generate the value of the independent variable through a loop, calculate the function value according to the equation, and then perform the necessary coordinate transformation: translation transformation for origin positioning, scaling transformation for image reduction or enlargement, obtain the image point of the curve, and draw this point. Take drawing the following curve equation as an example:
Y=sin(x)+cos(x),x
Part of the drawing code can be written as follows:
double x0,y0,x1,y1,x2,y2,scale;x0=100;y0=80;scale =20.0;for(x1=-3.1415926d;x1<=2*3.1415926d;x1+=0.01d){ y1 =Math.sin(x1)+Math.cos(x1); x2=x0+x1*scale;y2=y0+y1*scale;//(x2,y2) is the image point g.fillOval((int)x2,(int)y2,1,1);//Draw a circle points as image points}