Java know how much (graphics2d) the drawing method of the class

Source: Internet
Author: User

The Java language, which is based on the graphics class providing a variety of basic geometries, extends the graphics class to provide a graphics2d class that holds more precise control over more powerful two-dimensional graphics processing capabilities, providing, coordinate transformation, color management, and text layout.

Drawing properties

Graphics2D defines several methods for adding or changing the state properties of a graphic. You can specify how the brush width and the brush are connected by setting and modifying the State properties, setting the translation, rotation, scaling, or trimming of the transformed graphic, and setting the color and pattern of the filled 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 line segment connection, or the dash pattern. The setting for this property requires the Basicstroke object to be created before the Setstroke () method is called. The methods for creating Basicstroke objects are:

    • Basicstroke (float W): Specifies the line width W.
    • Basicstroke (float w,int cap, int join):
      • The CAP is the end-sample: Cap_butt (no decoration), Cap_round (semicircular end), Cap_square (square end, default value).
      • Join defines the connection at the intersection of two segments: Join_bevel (no decoration), JOIN_MTTER (cusp end, default), Join_round (circular end).


2. Paint Properties
The Paint property controls the fill effect. Call the following method to determine the fill effect, and use the Setpaint () method setting.

    • Gradientpaint (float x1,float y1,color c1,float x2,flaot y2,color C2): From (x1,y1) to (X2,Y2) colors from C1 gradient to C2. Where: Parameter c1,c2 determines that this gradient is from the color C1 gradient to the color C2. The parameter x1,y1,x2,y2 determines the intensity of the gradient, i.e. the point of arrival (X2,y2) from the point (x1,y1) and the color from C1 into C2.
    • Gradientpaint (float x1,float y1,color c1,float x2,float y2,color c2,boolean cyclic): Set cyclic to True if you want to fade to the end and the color of the starting point.


3. Transform Properties
The Transform property is used to achieve commonly used transformations such as panning, zooming, and chamfering. First create the AffineTransform object, and then call the SetTransform () method to set the Transform property. Finally, the drawing is drawn with the Graphics2D object with the specified properties. 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 directions are transformed by Sx,sy proportions, respectively.
    • Gettranslateinstance (double tx,double ty): translation transformation.
    • Getshearinstance (double shx,double shy): Oblique-cut transformations, shx and shy specify a diagonal pull.


You can also create a AffineTransform object without the Transform property, and then specify the graphics translation, rotation, and scale transformation properties in the following ways.

    • Transelate (double dx,double dy): Pans The graph in the x-axis direction dx pixels.
    • Scale (double sx,double sy): The graph scales the SX times in the x-axis direction, scaling the SY times vertically.
    • Rotate (double arc,double x, double y): The graph rotates arc radians with points (x, y) as pivot points.


For example, create a AffineTransform object:
AffineTransform trans = new AffineTransform ();
Specify the Revolve transformation properties for the AffineTransform object around the point:
Trans.rotate (50.0*3.1415927/180.0,90,80);
Next, for Graphics2D object g2d, set the brush with the above rotation transformation function:
Graphics2D g2d = (graphics2d) g;g2d.settranstorm (trans);
Finally, the draw () method of the Graphics2D object with the transform function is called with the graphical object as a parameter. For example, if you have a two-curve object curve, the following code implements the two-curve drawing of the G2d object with the above rotation function:
G2d.draw (curve);

4. Clip Properties
The clip property is used to implement the trim effect. Setting the clipping property invokes the SetClip () method to determine the shape of the clipping area. Successive multiple setclip () get the clipping area of their intersection.

5. Composit Properties
The Composit property sets the effect of the overlapping area of the graphic. Use the method alphacomposite.getinstance (int rule, float alpha) to get the Alphacomposite object, and then set the blending effect through the Setcomposite () method. The range of alpha values is 0.0f (fully transparent) -0.1f (completely opaque).

The drawing method of Graphics2D class

The Graphics2D class still retains the graphics class's drawing method, while adding many new methods. The new method draws the geometry (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 graphic objects, respectively. Mainly include:
Line2D segment class, roundrectangle2d Rounded Rectangle class, ellipse2d ellipse class, Arc2d arc class, quadcurve2d two times curve class, cubiccurve2d three curve class.

To draw a graphic with a new method of the Graphics2D class. In the Redraw method paintcomponent () or paint (), the Parameter object G is cast to the Graphics2D object first, and then the object of the graph is created with the static method provided by the graphic class above; The draw () method of the Graphics2D object is called with the drawing object as a parameter to draw the graph. For example, the following code draws a segment and a rounded rectangle with a new method of graphics2d:
Graphics2D g2d = (graphics2d) g;//convert 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 and create a Shape object with single-precision float or double-precision, and then draw with the draw () method. For example, the following code first 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)/drawing object created earlier arc

Geometry class for Graphics2D
  1. Segment
    1     New Line2d.double (2,3,200,300); // declaring and creating segment objects 2     // The starting point is (2,3), the end point is (200,300)
  2. Rectangular
    New Rectangle2d.double (20,30,80,40); // declares and creates a rectangle object, the upper-left corner of the rectangle is (20,30), the width is 300, and the height is
  3. Rounded Rectangle
    1  New Roundrectangle2d.double (20,30,130,100,18,15); 2     // The upper left corner is (20,30), the width is 130, the height is 100, the long axis of the fillet is 18, the short axis is 15. 

  4. Elliptic
    New Ellipse2d.double (20,30,100,50);     // Upper left corner (20,30), width is 100, height is

  5. arcs
       
     arc2d arc1 = new  arc2d.double (8,30,85,60,5,90,arc2d.open);  //  Upper-left corner of the bounding rectangle (10,30), width 85, height 60, starting angle 5 degrees, The terminating 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 arcs are open arcs, bow arcs, and pie arcs.  

     

  6. two times curve
      Two curves are represented by a second-order polynomial: Y (x)  =ax2+bx+ = 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 

     

  7. Three times curve
    the three-th curve is represented by a third-order polynomial:    y (x)=ax3+bx2+cx+D A three-time curve requires four points to determine: Start point, two control point, and end point.     New Cubiccurve2d.double (12,30,50,75,15,15,115,93);     New Cubiccurve2d.double (12,30,15,70,20,25,35,94);     New Cubiccurve2d.double (12,30,50,75,20,95,95,95); The 8 parameters of the method Double () are the starting point, two control points, and the end point of the three curves, respectively. 

The drawing process of the general equation curve is controlled by a loop. By looping the values of the arguments, the function values are calculated according to the equation, and then the necessary coordinate transformations are made: the translation transformation of the origin location, the scaling transformation of the image reduction or enlargement, the image point of the curve is obtained, and the point is plotted. For example, draw the following curve equation:

1Y=sin (x) +cos (x), x2 Some of the code drawn can be written as follows:3 DoubleX0,y0,x1,y1,x2,y2,scale;4X0=100;y0=80;5Scale =20.0;6  for(x1=-3.1415926d;x1<=2*3.1415926d;x1+=0.01d){7Y1=math.sin (x1) +Math.Cos (x1);8X2=x0+x1*scale;y2=y0+y1*scale;//(X2,y2) is an image point9G.filloval ((int) x2, (int) y2,1,1);//draw a dot as an image pointTen}

Series Articles:

Java know how much (top)Java know how much (medium)Java knows how many () Java vectors (vector) and their applicationsJava know how much (79) hash table and its applicationJava know how much (80) graphical Interface design basicsJava know how much (81) frame window BasicsJava know how much (82) Introduction to tags, buttons, and button eventsJava know how much (83) Panel Basics: JPanel and JScrollPaneJava know how much (84) layout design of graphical interfaceJava know how much (85) text box and text areaJava know how much (86) input and output of text box and text areaJava know how much (87) Select boxes and radio buttonsJava know how many (88) lists and combo boxesJava know how many (89) lists and combo boxesJava know how much (90) menuJava know how much (91) dialog boxJava know how much (92) scroll barJava know how much (93) mouse EventsJava know how much (94) keyboard EventsJava know how much (95) Drawing BasicsJava know how much (96) set the font and color of the drawingJava know how much (97) Drawing Mode OverviewHow much Java knows (98) the drawing method of the Graphics class

Java know how much (graphics2d) the drawing method of the class

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.