快精灵印艺坊 您身边的文印专家
广州名片 深圳名片 会员卡 贵宾卡 印刷 设计教程
产品展示 在线订购 会员中心 产品模板 设计指南 在线编辑
 首页 名片设计   CorelDRAW   Illustrator   AuotoCAD   Painter   其他软件   Photoshop   Fireworks   Flash  

 » 彩色名片
 » PVC卡
 » 彩色磁性卡
 » 彩页/画册
 » 个性印务
 » 彩色不干胶
 » 明信片
   » 明信片
   » 彩色书签
   » 门挂
 » 其他产品与服务
   » 创业锦囊
   » 办公用品
     » 信封、信纸
     » 便签纸、斜面纸砖
     » 无碳复印纸
   » 海报
   » 大篇幅印刷
     » KT板
     » 海报
     » 横幅

用Java写一个地图编辑器

用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"[img]/images/wink.gif[/img]);
baseimages[1] = new ImageIcon(MapPic.class.getResource("m2.png"[img]/images/wink.gif[/img]);
baseimages[2] = new ImageIcon(MapPic.class.getResource("m3.png"[img]/images/wink.gif[/img]);
baseimages[3] = new ImageIcon(MapPic.class.getResource("m4.png"[img]/images/wink.gif[/img]);
baseimages[4] = new ImageIcon(MapPic.class.getResource("m5.png"[img]/images/wink.gif[/img]);
baseimages[5] = new ImageIcon(MapPic.class.getResource("m6.png"[img]/images/wink.gif[/img]);
baseimages[6] = new ImageIcon(MapPic.class.getResource("m7.png"[img]/images/wink.gif[/img]);
baseimages[7] = new ImageIcon(MapPic.class.getResource("m8.png"[img]/images/wink.gif[/img]);
baseimages[8] = new ImageIcon(MapPic.class.getResource("m9.png"[img]/images/wink.gif[/img]);

itemimages = new ImageIcon[3];
itemimages[0] = new ImageIcon(MapPic.class.getResource("error.png"[img]/images/wink.gif[/img]);
itemimages[1] = new ImageIcon(MapPic.class.getResource("i1.png"[img]/images/wink.gif[/img]);
itemimages[2] = new ImageIcon(MapPic.class.getResource("i2.png"[img]/images/wink.gif[/img]);
}

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!"[img]/images/wink.gif[/img];
}
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!"[img]/images/wink.gif[/img];
} 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"[img]/images/wink.gif[/img]);

DataOutputStream itembinaryfile = new DataOutputStream(
new FileOutputStream(MapEditor.filename + "item"[img]/images/wink.gif[/img]);

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"[img]/images/wink.gif[/img];
}
}




结束!
就这么简朴!但是问题很多很多.都不能叫程序,随便写的玩的哦
需要源代码的朋友请留下email,要指出bug哦
亦或者msn交流:chinuxman@hotmail.com





返回类别: 教程
上一教程: Spring AOP之ThrowsAdvice
下一教程: Java对等计算实践:基于 IP 多播的发现

您可以阅读与"用Java写一个地图编辑器"相关的教程:
· 编写一个JAVA小程序取得IP地址
· java编辑多语言的福音--推荐一个经典的多语言文件编辑的插件ResourceBundle Editor
· java 面试中的一道编写一个截取字符串的函数!!!!
· 使用JSP + JAVABEAN + XML 开发的一个例子
· 构建一个轻易单元测试的java--web系统
    微笑服务 优质保证 索取样品