我是比較新的寫作大的圖形應用程序,並作爲一個項目,我必須寫在Java遊戲中使用擺動,同時採用適當的軟件工程的原則。我已將圖形界面與遊戲邏輯分離,但現在我不知道如何訪問我需要填充的視圖中的所有數據。如何從視圖中訪問我的應用程序的數據或模型?
例如,我有一個名爲「公告板」類,它具有爲「領土」對象,其又分別具有用於在地圖的相關區域爲多邊形對象的引用的列表的參考。我想製作一個顯示圖形世界地圖的自定義JPanel,並從板對象獲取區域集合,以便使用多邊形對象使地圖交互。
我想過使用某種形式的單例模式或工廠設計模式,所以我可以根據需要訪問所有數據,但似乎不是我想要的單例模式,我甚至都不瞭解工廠模式以確定是否我想要我們不是。我將被迫將所有對象的引用傳遞給GUI的每個組件,還是有更好的方法?
這是我的一些代碼,雖然我不知道這個階段有多少幫助。
public class MapPanel extends JPanel {
private Image image;
private List<Territory> territories;
public MapPanel() {
try {
BufferedInputStream in = new BufferedInputStream(
ClassLoader.getSystemResourceAsStream("MapBig.jpg"));
image = ImageIO.read(in);
} catch (IOException ex) {
// handle exception...
}
initialize();
}
private void initialize() {
this.setPreferredSize(new Dimension(900, 600));
}
public void setBackground(Image i) {
this.image = i;
repaint();
}
public void paintComponent(final Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, null);
}
}
而且我局類:
public class Board {
private List<Territory> territories;
//private List<List<Tile>> rows;
public Board(List<Territory> territories) {
this.territories = territories;
}
public Iterator<Territory> getTerritories() {
return territories.iterator();
}
}
而且我領地類持有我想利用的多邊形。
public class Territory implements java.io.Serializable {
//region Private Variables
/**
*
*/
private static final long serialVersionUID = 5360937073507663827L;
/**
* The territory's name.
*/
private String name;
/**
* The representation of this territory in 2d space.
*/
private Polygon region;
/**
* List of Player objects currently residing in this geographical area
* of the world map.
*/
private List<Player> players;
/**
* Country that owns this territory.
*/
private Country owner;
/**
* List of neighboring territories that can be traveled to.
*/
private List<Territory> neighbors;
//endregion Private Variables
public Territory(String name, Polygon region) {
this.name = name;
this.region = region;
players = new ArrayList<Player>();
owner = null;
}
/**
* Get the name of this territory.
* @return name of this territory
*/
public String getName() { return name; }
/**
* Set the name of this territory.
* @param name name to give this territory
*/
public void setName(String name) { this.name=name; }
/**
* Get the bounded region for this territory.
* @return a Polygon object representing the 2D bounds of this
* territory
*/
public Polygon getRegion() { return region; }
/**
* Set the bounded region for this territory.
* @param region a Polygon object representing the 2D bounds of this
* territory
*/
public void setRegion(Polygon region) { this.region=region; }
/**
* Get the owning Country.
* @return the Country object that owns the geograpical area bounded
* by this territory.
*/
public Country getCountry() { return owner; }
/**
* Set the owning Country.
* @param country the Country object that owns the geograpical area
* bounded by this territory.
*/
public void setCountry (Country country) { this.owner=country; }
public Iterator<Territory> getNeighbors() { return neighbors.iterator(); }
public void setNeighbors(List<Territory> neighbors) { this.neighbors = neighbors; }
/**
* Get a list of all Player objects currently residing in this geographical
* area of the world map.
* @return an Iterator over a List of Player objects
*/
public Iterator<Player> getPlayers() { return players.iterator(); }
/**
* Adds a player to this Territory
* @param player Player object to add to this Territory
*/
public void addPlayer(Player player) { players.add(player); }
/**
*
* @param player
*/
public void removePlayer(Player player) { players.remove(player); }
}
你能發表一些代碼嗎? – imulsion
你可以在JPanel中創建一個模型類的實例嗎?這就是我被告知我的MVC象棋項目做,當我把Java的 –
@imulsion一些代碼 – agent154