Java 如何在三角形上填充颜色
我用直线画一个三角形。我怎样才能在上面填上颜色?到目前为止,我只能成功地为线条上色,但无法填充颜色Java 如何在三角形上填充颜色,java,geometry,drawing,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.
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
}
公共组件(图形g){
超级组件(g);
int k=0;
对于(j=0;j
您需要指定多边形的顶点(在本例中为三角形)并传递到fillPolygon():
通过调用以下命令,从顶点创建一个顶点并填充该顶点:
谢谢……但是用直线绘制的平均三角形不能用颜色填充吗?@Jessy:直线的交点(即顶点)是您需要的点。如何设置填充三角形的颜色?
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);
}
// 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.
public void paintComponent(Graphics g){
super.paintComponents(g);
int x[] = {1,2,3};
int y[] = {4,5,6};
int npoints = x.length;//or y.length
g.drawPolygon(x, y, npoints);//draws polygon outline
g.fillPolygon(x, y, npoints);//paints a polygon
}