繁体   English   中英

如何在Java中填充多边形?

[英]How to fill a polygon in java?

我有一个小问题。我实际上是在用Java(这是一个GUI)编写程序。 有一个名为Map的类,我在其中创建一个地图..(或至少要尝试)。构造函数初始化地图,并返回一个Area并将其绘制在View类中。 我尝试了使用g2.fillPolygon(x [],y [],n)的经典方法,但是它不起作用。 这是源代码:

public class Map{
    Area area;
    //...
    public Map(){
        this.area=new Area(new Polygon(
                arrayX(),//ArrayY() and arrayX() are methods that generate     arrays with random numbers
                arrayY(),
                MAX
                ));
   }

//...stuff
}

这是View类:

public class View extends JComponent{
    Map map=new Map();

//...stuff

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
//.......

    g2.draw(map.area);//this draws the normal polygon NOT filled
    g2.fillPolygon(map.ArrayX,map.arrayY,map.MAX);//this might fill the polygon but it does noot
    g2.fillPolygon(map.area);//this does not work (ofcourse) because it wants a Polygon type parameter. I tried to cast it but it still does not work.
}
}

在这种情况下我该怎么办? 非常感谢你。

就像Graphics2D#draw(Shape)方法一样,还有Graphics2D#fill(shape)方法。

g2.setColor(Color.BLUE);
g2.fill(map.area);
g2.setColor(Color.RED);
g2.draw(map.area);

您可能想看看2D图形了解更多详细信息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM