Graphics2D class Basic Use tutorial in Java _java

Source: Internet
Author: User
Tags cos sin

The Java language, on the basis of the graphics class, provides a variety of basic geometries, extending the graphics class to provide a graphics2d class with more powerful two-dimensional graphics processing capabilities, providing more precise control of coordinate transformations, color management, and text layout.
Drawing Properties

Graphics2D defines several methods for adding or changing the state properties of a graphic. You can specify the brush width and the way the brush is connected by setting and modifying the State properties, setting panning, rotating, scaling, or trimming the transform graphic, and setting the color and pattern of the fill shape. Graphics state properties are stored with specific objects.

1. Stroke Properties
The Stroke property controls the width of the line, the pen style, the way the line is connected, or the dash pattern. The setting of this property requires the creation of the Basicstroke object first, and then 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 the Endpoint sample: Cap_butt (no modification), Cap_round (semicircular end), Cap_square (square end, default).
Join defines how the two segments join: join_bevel (no modification), Join_mtter (sharp end, default), Join_round (rounded end).

2. Paint Properties
The Paint property controls the fill effect. Call the following methods to determine the fill effect, using the Setpaint () method setting.
Gradientpaint (float x1,float y1,color c1,float x2,flaot y2,color): from (C2) to (x1,y1) colors from X2,y2 to C1. Where: Parameter c1,c2 determines this gradient is from the color C1 to the color C2. The parameter x1,y1,x2,y2 determines the intensity of the gradient, that is, from the point (x1,y1) to the point of arrival (X2,y2), the color from C1 into C2.
Gradientpaint (float x1,float y1,color c1,float x2,float y2,color c2,boolean): Set the cyclic to TRUE if you want the gradient to be the color of the start point.

3. Transform Properties
The Transform property is used to transform operations such as graphics translation, scaling, and skew. First create the AffineTransform object, and then call the SetTransform () method to set the Transform property. Finally, the graph is drawn with the Graphics2D object with the specified property. The methods for creating AffineTransform objects are:

    • Getrotateinstrance (double theta): Rotates theta radians.
    • Getrotateinstance (double theta,dioble x,double y): Rotates around the center of rotation (x,y).
    • Getscaleinstance (Double sx,double sy): The x and y orientations are converted by sx,sy proportions respectively.
    • Gettranslateinstance (double tx,double ty): translational transformation.
    • Getshearinstance (double shx,double shy): Oblique-cut transform, shx and shy specify the diagonal pull degree.

You can also create a AffineTransform object without the transform attribute, and then specify the graphic translation, rotation, and scaling transformation properties in the following ways.

    • Transelate (double dx,double dy): translates a graphic to dx pixels in the x-axis direction.
    • Scale (double sx,double sy): The shape scales SX by the x axis, and the vertical scales the sy times.
    • Rotate (double arc,double x, double y): The Shape points (x,y) as the axis point and rotates the arc radians.

For example, create a AffineTransform object:

  AffineTransform trans = new AffineTransform ();


Specify a revolved rotation transformation property for the AffineTransform object:

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


Then set the brush for the Graphics2D object g2d with the rotation transform function:

  Graphics2D g2d = (graphics2d) g;g2d.settranstorm (trans);


Finally, the draw () method of the Graphics2D object with transform function is called with the drawing object as the parameter. For example, if you have a two-time curve object curve, the following code enables the two-time curve to be drawn with the G2d object of the rotational function:

  G2d.draw (curve);

4. Clip Properties
The clip property is used to implement clipping effects. Setting the Trim property invokes the SetClip () method to determine the shape of the clipping area. Successive multiple setclip () get the clipping area where they intersect.

5. Composit Properties
The Composit property sets the effect of overlapping areas of graphics. The Alphacomposite object is obtained first with method alphacomposite.getinstance (int rule, float alpha), and then the blending effect is set by the Setcomposite () method. The range of alpha values is 0.0f (fully transparent) -0.1f (completely opaque).
Drawing method of Graphics2D class

The Graphics2D class still retains the graphics class's drawing methods, while adding many new methods. The new method paints the geometry (line segments, circles, and so on) as an object. A series of classes declared in the Java.awt.geom package that are used to create various body drawing objects, respectively. The main are:
Line2D segment class, roundrectangle2d round rectangle class, Ellipse2d ellipse class, Arc2d arc class, quadcurve2d two times curve class, cubiccurve2d three times curve class.

To draw a graphic using the new method of the Graphics2D class. In the Redraw method paintcomponent () or paint (), the Parameter object G is coerced into a Graphics2D object, and then the object of the graphic is created by using a static method double () provided by the graphic class above; This graph is drawn using the draw () method of the Graphics2D object called by the drawing object as the parameter. For example, the following code draws a segment and rounded rectangle with a new method of graphics2d:

  Graphics2D g2d = (graphics2d) g;//converts 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 use a Shape object provided with a JAVA.AWT.GEOM package, create a Shape object with a single-precision float or double double coordinate, and then draw with the draw () method. For example, the following code creates an Arc object first, and then draws an arc:

  Shape arc = new Arc2d.float (30,30,150,150,40,100,arc2d.open);
  G2d.draw (ARC)//Draw the previously created drawing object arc


Geometry class for Graphics2D

Segment

  Line2D line = new line2d.double (2,3,200,300);//Declare and create a Segment object
  //Starting point is (2,3), end point is (200,300)


Rectangular

  Rectangle2D rect = new Rectangle2d.double (20,30,80,40)//Declare and create a rectangular object with the upper-left corner of the rectangle (20,30), width 300, height 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 rounded angle is 18, and the short axis is 15.

Elliptic

  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 outer rectangle (10,30), width 85, high 60, the starting angle is 5 degrees, the end 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 that the arc is open arc, bow arc and pie arc.
Two times curve
The two-time curve is represented by a second order polynomial:

  Y (x) =ax2+bx+c


A two-time curve needs three points to determine: Start point, Control Point, and 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 6 parameters in the method double () are the starting point, control point, and endpoint of the two-time curve respectively. The beginning and end points of the above 3 two curves are the same.
Three times curve
The three-time curve is represented by a third-order polynomial:

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


A three-time curve needs four points to determine: Start point, two control point, and 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 8 parameters in the method double () are the beginning, two control points, and end points of the three-time curve respectively.

The general equation curve of the drawing process is controlled by a loop. The value of the independent variable is generated by circulation, the function value is calculated according to the equation, then the necessary coordinate transformation is made: the translation transformation of the origin location, the zoom transformation of the image reduction or magnification, the image point of the curve, and the drawing of the point. For example, draw the following curve equation:

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


Some of the code drawn 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 an image point
  g.filloval ((int) x2, (int) y2,1,1);//Draw a dot as the image point
}

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.