The support for Java geometric images is not very good. I tried the 2D and 3D images provided after Java, and I did not study them carefully, I don't know if I can draw such a geometric image, so I checked the API and found this method drawpolygon (INT [] xpoints, int [] ypoints, int npoints ), this method can draw any number of rows, so I used this method in combination with other drawing methods to write a three-dimensional rectangular demo.
API explanation of this method:
Public abstract void drawpolygon (INT [] xpoints, int [] ypoints, int npoints)
-
Draw XAnd YThe closed polygon defined by the coordinate array. Each pair ( X, Y) Coordinates define a point.
-
This method draws a polygon defined by npoint line segments.
-
When the npoint-1 line segment is 1 ≤ I ≤ npoints
-
(Xpoints [I-1], ypoints [I-1]) to (xpoints [I], ypoints [I]) line segments.
-
If the last vertex and the first vertex are different, the graph will automatically close by drawing a line segment between these two points.
-
-
Parameters:
-
Xpoints
-
X
Coordinate array.
-
Ypoints
-
Y
Coordinate array.
-
Npoints
-Total number of points.
Below is my demo program:
-
Import Java. AWT. color; </P> <p> Import Java. AWT. graphics; </P> <p>/** <br/> * @ author lazy_p <br/> * @ date 2010-3-18 <br/> * <br/> * stereo graphics demo <br/> */<br/> public class threedrec extends Java. applet. applet {</P> <p> Private Static final long serialversionuid =-2427989261965982016l; </P> <p> Public void Init () {<br/> resize (200,200 ); <br/>}// original graph area </P> <p> Public void paint (Graphics g) {</P> <p> G. setcolor (color. red); <br/> G. drawpolygon (New int [] {30, 90,100, 40}, <br/> New int [] {30, 30, 10, 10}, 4 ); // draw a rectangle <br/> G. setcolor (new color (1 ). gray); </P> <p> G. fillpolygon (New int [] {30, 90,100, 40}, <br/> New int [] {30, 30, 10, 10}, 4 ); // fill in a rectangle <br/> G. setcolor (color. red); <br/> G. drawrect (30, 30, 60,100); <br/> G. setcolor (new color (1 ). cyan); <br/> G. fillrect (30, 30, 60,100); <br/> G. setcolor (color. red); <br/> G. drawpolygon (New int [] {90,100,100, 90}, new int [] {30, 10,110, <br/> 130}, 4); <br/> G. setcolor (new color (1 ). green); <br/> G. fillpolygon (New int [] {90,100,100, 90}, new int [] {30, 10,110, <br/> 130}, 4 ); </P> <p >}< br/>
-
Effect:
-