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