Jes*_*ssy 6 java geometry drawing
我用线画一个三角形.我怎样才能填上它的颜色?到目前为止,我只能成功地为线条着色但不填充颜色.
public void paintComponent(Graphics g){
super.paintComponents(g);
int k=0;
for (j=0 ; j < numOfLines; j++){ // the values of numOfLines retrieved from other method.
g.setColor(Color.green);
g.drawLine(x[k], x[k+1], x[k+2], x[k+3]);
k = k+4; //index files
}
Run Code Online (Sandbox Code Playgroud)
Joh*_*lla 18
做一个Polygon从顶点和填充,而不是通过调用fillPolygon(...):
// A simple triangle.
x[0]=100; x[1]=150; x[2]=50;
y[0]=100; y[1]=150; y[2]=150;
n = 3;
Polygon p = new Polygon(x, y, n); // This polygon represents a triangle with the above
// vertices.
g.fillPolygon(p); // Fills the triangle above.
Run Code Online (Sandbox Code Playgroud)
您需要指定多边形的顶点(在本例中为三角形)并传递给 fillPolygon():
public void paint(Graphics g)
{
int xpoints[] = {25, 145, 25, 145, 25};
int ypoints[] = {25, 25, 145, 145, 25};
int npoints = 5;
g.fillPolygon(xpoints, ypoints, npoints);
}
Run Code Online (Sandbox Code Playgroud)