JAVA开发地图编辑器_用Java写一个地图编辑器

用Java写一个地图编辑器

作者:网络 来源:佚名 更新时间:2008-02-08 21:46:29

点击:0

用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

,欢迎访问网页设计爱好者web开发。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值