gpt4 book ai didi

java - 如何在java中填充多边形?

转载 作者:行者123 更新时间:2023-11-30 03:03:28 26 4
gpt4 key购买 nike

我有一个小问题..我实际上正在用java编写一个程序(它是一个GUI)。有一个名为 Map 的类,我在其中创建了一张 map ..(或者至少我正在尝试)。构造函数初始化 map 并返回一个区域,然后我在 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 Graphics了解更多详情

关于java - 如何在java中填充多边形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35373822/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com