2016-02-12 169 views
1

我有一個小問題..我實際上在做一個java程序(這是一個GUI)。 有一個名爲Map的類,我在其中創建了一個地圖..(或者至少我在嘗試)。構造函數初始化地圖,並返回一個Area,然後在View類中繪製它。我試着用g2.fillPolygon(x [],y [],n)來做這個經典的方法,但它不起作用。這裏是源代碼:如何在java中填充多邊形?

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 
} 

這裏是視圖類:

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. 
} 
} 

我會在這種情況下怎麼辦? 非常感謝。

+0

對不起,我的語法。我寫得很快。 – MArivs

+0

注意:不要用已經被Java使用過的名稱來命名你的類/類型,比如'Map'' List'' Set'' Object''Character'。 – Pshemo

+0

我相信你需要一個傳遞給fillPolygon方法的Paint對象。並且該繪畫對象將具有填充屬性和顏色等。 – Batdude

回答