我对编写大型图形应用程序比较陌生,作为一个项目,我必须用Java编写一个使用swing的游戏,同时使用适当的软件工程原理。我已经将图形界面与游戏逻辑分离,但现在我不知道如何访问我需要在视图中填充的所有数据。
例如,我有一个名为“Board”的类,它引用了一个“Territory”对象列表,而这些对象又分别引用了地图相关区域的多边形对象。我想创建一个自定义JPanel,它显示一个图形世界地图,并从board对象中获取区域集合,这样我就可以使用多边形对象使地图交互。
我想过使用某种形式的单件或工厂设计图案,所以我可以根据需要访问所有数据,但似乎单身不是我想要的,而且我甚至不太了解工厂模式确定这是不是我想要的。我是否会被迫将所有对象的引用传递给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();
}
}
我的 Territory 类包含我想要使用的多边形。
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); }
}
答案 0 :(得分:0)
您需要使用可观察模式在MVC模式的组件之间进行通信。 此外,您可能需要考虑使用Facade模式来确保简化通信。
模型:所有游戏逻辑(也包含对象列表!) 控制器:解释GUI上的用户输入 查看:报告关于gui的行动。
看看这个:http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller