[英]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.