Java graphics2d Drawing

Source: Internet
Author: User

In Java, when you need to draw some special shapes, such as ellipses, rectangles, and so on, you can use graphics2d to draw.

Some APIs:

G.drawline (3,3,50,50); // Draw a line segment g.drawrect (80,100,40,25); // Draw wireframe g.drawoval (10,10,60,120); // Draw an ellipse // Draw Polygons int px1[]={50,90,10,50}; //  int py1[]={10,50,50,10};g.setcolor (Color.Blue); G.fillpolygon (Px1,py1,4);

For more information on drawing, you can refer to some of the online introduction, or the official API.

A function in the Graphics2D object is translate (x, y), which functions as follows:

Moves the origin of the ellipse to (x, y).

This function, in conjunction with the Rotate function, enables you to rotate a graphic around a point.

G2.rotate (Math.PI * rotate/180.0);   // Note that the function rotates around the origin of the ellipse

For example, to rotate a horizontal ellipse around the center point of an ellipse, you need to first move the ellipse's origin to a position and then rotate around the new origin, as follows:

The coordinates of the new origin can be calculated based on the coordinates of the origin at the horizontal state and the angle of rotation. It is important to note that the origin is moved first, then rotated, and the order cannot be confused.

Example code:

graphics2d g2 =* rotate/180.0); G2.drawoval (0, 0, ovalWidth-1, ovalHeight-1); G2.translate ( -translatex,-Translatey); G2.dispose ();

Java graphics2d Drawing

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.