用Java寫一個地圖編輯器
最後更新:2017-02-28
來源:互聯網
上載者:User
用Java寫一個地圖編輯器
記得媒體在採訪C++之父的時候,他說作為程式員,要相信自己能夠解決已經理解的任何事情.
換句話說:您可以解決任何問題,只要想得明白
現實問題:開發一個基於地磚的二維遊戲的地圖編輯器,要求產生兩個binary檔案,各包含一個二維數組,*.map存放地磚,花花草草什麼的.*.item放道具,比如某個點可能會觸發一個事件.很簡單,隨便寫.看到這裡您已經大致明白程式的整體結構.
電腦語言:java.
要理解事件必須分析
初步來看,地圖編輯器:產生某種形式的若干數組,無論是哪種形式的數組,你的目的:
產生數組.地圖是實際是一個(x,y)的二維座標系,這很容易讓人聯絡到:亦無論
我準備把設定兩個程式介面(主介面/Map介面),Java的布局管理器不好擺弄,不如分開兩個class,主介面用JBuilder自動建立的Application模組(帶菜單).Map介面自己寫,也是JFrame,類之間相互傳遞訊息,map介面將在程式開始時被初始化,也可以在程式從主介面中初始化(有問題)
構建程式
以下內容為程式碼:
basePanel.setLayout(new GridLayout(5, 5));
for (byte i = 0; i < 9; i++) {
baseMapButton[i] = new
((Icon) pic.getImageIcon(i, 0));
baseMapButton[i].setButtonTitle(i);
baseMapButton[i].addActionListener(buttonListener);
basePanel.add(baseMapButton[i]);
}
itemPanel.setLayout(new GridLayout(5, 5));
for (byte i = 0; i < 3; i++) {
itemMapButton[i] = new MapButton((Icon) pic.getImageIcon(i, 1));
itemMapButton[i].setButtonTitle(i);
itemMapButton[i].addActionListener(buttonListener1);
itemPanel.add(itemMapButton[i]);
}
tabbedPane.addTab("Bases", basePanel);
tabbedPane.addTab("Items", itemPanel);
contentPane.add(tabbedPane, BorderLayout.CENTER);
有兩個地方要解釋:
MapButton:自己寫的一個類
以下內容為程式碼:
import javax.swing.Icon;
import javax.swing.JButton;
public class MapButton extends JButton {
public MapButton() {
super();
}
public MapButton(String arg0) {
super(arg0);
}
public MapButton(Action arg0) {
super(arg0);
}
public MapButton(Icon arg0) {
super(arg0);
}
public MapButton(String arg0, Icon arg1) {
super(arg0, arg1);
}
public byte width, height;
//public pic_w, pic_y;
public void setButtonTitle(byte w, byte h) {
width = w;
height = h;
}
public void setButtonTitle(byte w){
width =w;
}
public byte getButtonWidth() {
return width;
}
public byte getButtonHeight() {
return height;
}
}
pic:自己寫的MapPic類的intance:
以下內容為程式碼:
package com.nenghe.mapeditor;
import javax.swing.ImageIcon;
public class MapPic {
ImageIcon[] baseimages;
ImageIcon[] itemimages;
ImageIcon image1;
public MapPic() {
init();
}
public void init() {
baseimages = new ImageIcon[9];
baseimages[0] = new ImageIcon(MapPic.class.getResource("m1.png");
baseimages[1] = new ImageIcon(MapPic.class.getResource("m2.png");
baseimages[2] = new ImageIcon(MapPic.class.getResource("m3.png");
baseimages[3] = new ImageIcon(MapPic.class.getResource("m4.png");
baseimages[4] = new ImageIcon(MapPic.class.getResource("m5.png");
baseimages[5] = new ImageIcon(MapPic.class.getResource("m6.png");
baseimages[6] = new ImageIcon(MapPic.class.getResource("m7.png");
baseimages[7] = new ImageIcon(MapPic.class.getResource("m8.png");
baseimages[8] = new ImageIcon(MapPic.class.getResource("m9.png");
itemimages = new ImageIcon[3];
itemimages[0] = new ImageIcon(MapPic.class.getResource("error.png");
itemimages[1] = new ImageIcon(MapPic.class.getResource("i1.png");
itemimages[2] = new ImageIcon(MapPic.class.getResource("i2.png");
}
public ImageIcon getImageIcon(int x, int flags) {
if (flags == 0) {
return baseimages[x];
} else if (flags == 1) {
return itemimages[x];
}
return null;
}
}
寫MapButton在於處理事件的時候可以準確的獲得按鈕的座標,忘了說了,Map介面中我是用按鈕代替地圖方格的.這是很容易想到的,最笨也是最省力的辦法
pic單獨寫好改,什麼時候內容改變了,很容易改,硬要合寫沒有也隨便.
下面就是事件了
有兩個事件要處理,第一個是按鈕事件,第二個菜單事件
按鈕事件我套用這樣的結構
以下內容為程式碼:
ActionListener buttonListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
//System.out.println(e.toString());
MapButton pressedButton = (MapButton) e.getSource();
MapDraw.temp_x = pressedButton.getButtonWidth();
MapDraw.temp_y = 0;
//System.out.println(MapDraw.temp_x+" "+MapDraw.temp_y);
}
};
....
baseMapButton[i].addActionListener(buttonListener);
JBuilder中把按鈕事件事件單獨產生一個類,我不明白,看不懂.真的很高深.
菜單事件模型JBuilder自己加的.overwrite
以下內容為程式碼:
public void *_actionPerformed(ActionEvent e) {...}
用兩個中間值從主介面向map介面傳遞按了什麼:
這裡是map介面中的按鈕的事件處理常式
以下內容為程式碼:
ActionListener buttonListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
MapButton pressedButton = (MapButton) e.getSource();
pressedWidth = pressedButton.getButtonWidth();
pressedHeight = pressedButton.getButtonHeight();
if (temp_y == 0) {
if (item[pressedWidth][pressedHeight] != 0) {
item[pressedWidth][pressedHeight] = 0;
jfm.showMessage("這裡的道具已被置空!\nThe item has been null!";
}
map[pressedWidth][pressedHeight] = temp_x;
pressedButton.setIcon((Icon) pic.getImageIcon(temp_x,
temp_y));
} else {
if (map[pressedWidth][pressedHeight] == 0) {
jfm.showMessage("道具不能放在這!\nNot put item at this point!";
} else {
if (temp_x == 0) {
byte value = map[pressedWidth][pressedHeight];
item[pressedWidth][pressedHeight] = 0;
pressedButton.setIcon((Icon) pic.getImageIcon(
value, 0));
} else {
pressedButton.setIcon((Icon) pic.getImageIcon(
temp_x, temp_y));
item[pressedWidth][pressedHeight] = temp_x;
}
}
}
}
};
請問兩個中間值是什麼呢?一目瞭然哦
最後是產生map
以下內容為程式碼:
public void createMap() throws IOException {
try {
DataOutputStream mapbinaryfile = new DataOutputStream(
new FileOutputStream(MapEditor.filename + "map");
DataOutputStream itembinaryfile = new DataOutputStream(
new FileOutputStream(MapEditor.filename + "item");
mapbinaryfile.writeByte(width);
mapbinaryfile.writeByte(height);
for (byte i = 0; i < height; i++)
for (byte j = 0; j < width; j++) {
//System.out.println(i+" "+j);
byte mapvalue = map[i][j];
byte itemvalue = item[i][j];
if (mapvalue != 0) {
System.out.println(i+" "+j+" "+ mapvalue);
mapbinaryfile.writeByte(j);
mapbinaryfile.writeByte(i);
mapbinaryfile.writeByte(mapvalue);
}
if (itemvalue != 0) {
itembinaryfile.writeByte(j);//x
itembinaryfile.writeByte(i);//y
itembinaryfile.writeByte(itemvalue);
}
}
mapbinaryfile.close();
itembinaryfile.close();
} catch (EOFException e) {
System.err.println("Error";
}
}
結束!
就這麼簡單!但是問題很多很多.都不能叫程式,隨便寫的玩的哦
需要原始碼的朋友請留下email,要指出bug哦
亦或者msn交流:chinuxman@hotmail.com