Basic use of the Graphics2D class in Java

  • 2020-04-01 04:15:06
  • OfStack

The Java language extends the Graphics class to provide a Graphics2D class that provides more precise control over the Graphics, coordinate transformation, color management, and text layout, with more powerful two-dimensional Graphics processing power.
Drawing properties

Graphics2D defines several methods for adding or changing the state properties of a graph. You can specify the brush width and how to connect the brushes by setting and modifying the state properties. Set shift, rotate, scale or trim transform graphics; And set the color and pattern of the filling graphics. Graphic state properties are stored with specific objects.

1. The stroke properties
The stroke property controls the width of the line, the stroke style, the line segment connection, or the underline pattern. This property is set by creating the BasicStroke object and then calling the setStroke() method. The methods to create the BasicStroke object are:
BasicStroke(float w) : specifies line width w.
BasicStroke(float w,int cap, int join) :
Cap is endpoint like: CAP_BUTT(undecorated), CAP_ROUND(semicircular end), CAP_SQUARE(square end, default).
Join defines JOIN_BEVEL(undecorated),JOIN_MTTER(spiked end, default), and JOIN_ROUND(rounded end) at the intersection of two line segments.

2. Paint properties
The paint property controls the fill effect. The following method is called to determine the fill effect, which is set using the setPaint() method.
GradientPaint(float x1,float y1,Color c1,float x2,flaot y2,Color c2) : Color gradient from (x1,y1) to (x2,y2) from c1 to c2. Where: parameter c1,c2 determines the gradient from the color c1 to the color c2. Parameters x1,y1,x2 and y2 determine the strength of the gradient, that is, to start from the point (x1,y1) to the point (x2,y2), and change the color from c1 to c2.
GradientPaint(float x1,float y1,Color c1,float x2,float y2,Color c2,Boolean cyclic) : if you want to gradient to the end and the Color of the starting point, you should set the cyclic to true.

3. The transform properties
The transform property is used to implement the commonly used transform operations such as graphic translation, scaling and bevel cutting. First create the AffineTransform object, then call the setTransform() method to set the transform property. Finally, draw the graph with the Graphics2D object with the specified properties. The methods to create the AffineTransform object are:

GetRotateinstrance (double theta) : rotate theta radian. GetRotateInstance (double theta,dioble x,double y) : rotates around the center of rotation (x,y). GetScaleInstance (double sx,double sy) : the x and y directions are transformed according to the proportion of sx and sy respectively. GetTranslateInstance (double tx,double ty) : translation transformation. GetShearInstance (double SHX,double shy) : the slant cut transform, SHX, and shy specify the slant.

You can also create an AffineTransform object without a transform property, and then specify the transform properties of the graph by translating, rotating, and scaling.

Transelate (double dx,double dy) : moves the graph by dx pixels in the X-axis. Scale (double sx,double sy) : the graph scales sx times in the X-axis direction, and vertically scales sy times. Rotate (double arc,double x, double y) : the figure rotates by arc radians around the point (x,y).

For example, create an AffineTransform object:


  AffineTransform trans = new AffineTransform();


Specify the transform property around the point for the AffineTransform object:


  Trans.rotate(50.0*3.1415927/180.0,90,80);


Next, set the "brush" with the above rotation transformation function for the object g2d of Graphics2D:


  Graphics2D g2d = (Graphics2D)g;g2d.setTranstorm(trans);


Finally, the draw() method of the Graphics2D object with the transform function is called with the graph object as an argument. For example, suppose there is a conic object curve, and the following code is used to draw the conic with the g2d object of the above rotation function:


  g2d.draw(curve);

4. The clip properties
The clip attribute is used for clipping. Set the clipping property to determine the Shape of the clipping area by calling the setClip() method. Successive setclips () get the clipping of their intersection.

5. Composit properties
The composit attribute sets the effect of the graphics overlap area. Use first method AlphaComposite. GetInstance (int rule, float alpha) get AlphaComposite object, through setComposite () method sets the mixing effect. Alpha values range from 0.0f(fully transparent) to 0.1f(fully opaque).
Graphics2D class

The Graphics2D class still retains the drawing methods of the Graphics class, while many new methods have been added. The new method draws a geometry (line segment, circle, etc.) as an object. A series of classes declared in the java.awt.geom package for creating various body graphics objects. Mainly include:
Line2D segment, RoundRectangle2D rounded rectangle, Ellipse2D elliptical, Arc2D arc class, class QuadCurve2D quadratic curve, CubicCurve2D curve class three times.

To draw a graph, use the new method of the Graphics2D class. First, in the paintComponent() or paint() methods, the parameter object g is cast into the Graphics2D object. Then, use the static method Double() provided by the graph class to create the object for that graph. Finally, the draw() method of the Graphics2D object is called to draw the graph with the graph object as an argument. For example, the following code USES Graphics2D's new method to draw line segments and rounded rectangles:


  Graphics2D g2d = (Graphics2D)g;//Converts the object type g 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 use the Shape object provided by the java.awt.geom package, create the Shape object with single-precision Float coordinates or double-precision Double coordinates, and then draw it with the draw() method. For example, the following code creates an arc object and then draws an arc:


  Shape arc = new Arc2D.Float(30,30,150,150,40,100,Arc2D.OPEN);
  g2d.draw(arc)// Draws the graph object created earlier arc


Graphics2D's geometry class

Line segment


  Line2D line = new Line2D.Double(2,3,200,300);//Declare and create line segment objects
  // The starting point is (2 . 3) The finish is (200 . 300)


rectangular


  Rectangle2D rect = new Rectangle2D.Double(20,30,80,40);// Declare and create the rectangle object, the top left corner of the rectangle is (20 . 30) , and the width is 300 High, is 40


The rounded rectangle


  RoundRectangle2D rectRound = new RoundRectangle2D.Double(20,30,130,100,18,15);
  //The top left is (20,30), the width is 130, the height is 100, the long axis of the rounded Angle is 18, the short axis is 15.

The ellipse


  Ellipse2D ellipse = new Ellipse2D.Double(20,30,100,50);
  // The upper left corner  (20 . 30) , and the width is 100 High, is 50


The circular arc


  Arc2D arc1 = new Arc2D.Double(8,30,85,60,5,90,Arc2D.OPEN);
  //The upper left corner of the outer rectangle (10,30), 85 wide, 60 high, starting at 5 degrees and ending at 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 that the arc is an OPEN arc, a bow arc and a cake arc.
Quadratic curve
The quadratic curve is expressed by second-order polynomial:


  y(x)=ax2+bx+c


A conic needs to be defined by three points: the beginning 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 method Double() are the beginning, control, and end points of the conic. The starting point and ending point of the above three conics are the same respectively.
Cubic curve
The cubic curve is expressed by third-order polynomial:


  y(x)=ax3+bx2+cx+d


A cubic curve needs to be defined by four points: the beginning 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 beginning, the two control points, and the end points of the cubic curve.

The drawing process of general equation curve is controlled by a loop. Generate the value of the independent variable through the cycle, calculate the value of the function according to the equation, and then make the necessary coordinate transformation: the translation transformation of the origin positioning, the image zooming or zooming transformation, get the image point of the curve, and draw this point. Take the following curve equation as an example:


  Y=sin(x)+cos(x),x


Part of the drawn 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 graph point
  g.fillOval((int)x2,(int)y2,1,1);//Draw a dot as a graph point
}


Related articles: