How to fill color on triangle

前端 未结 3 1186
隐瞒了意图╮
隐瞒了意图╮ 2021-01-05 13:56

I draw a triangle using line. How can I fill color on it? So far I can only success color the line but not fill the color.

public void paintComponent(Graphic         


        
相关标签:
3条回答
  • 2021-01-05 14:28

    You need to specify the vertices of your polygon (in this case, a triangle) and pass to 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);
      }
    
    0 讨论(0)
  • 2021-01-05 14:28
        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
    
    }
    
    0 讨论(0)
  • 2021-01-05 14:31

    Make a Polygon from the vertices and fill that instead, by calling 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.
    
    0 讨论(0)
提交回复
热议问题