java编辑地图的思想_用Java写一个地图编辑器

这篇博客介绍了如何使用Java来开发一个基于地砖的二维游戏地图编辑器,生成两个二进制文件存储地图数据。通过自定义MapButton类和事件处理,实现了地图编辑功能,允许用户编辑地图上的地砖和道具,并保存到文件。
摘要由CSDN通过智能技术生成

用java写一个地图编辑器

记得媒体在采访C++之父的时候,他说作为程序员,要相信自己能够解决已经理解的任何事情.

换句话说:您可以解决任何问题,只要想得明白

现实问题:开发一个基于地砖的二维游戏的地图编辑器,要求生成两个binary文件,各包含一个二维数组,*.map存放地砖,花花草草什么的.*.item放道具,比如某个点可能会触发一个事件.很简单,随便写.看到这里您已经大致明白程序的整体结构.

http://www.gaodaima.com/41462.html用Java写一个地图编辑器

计算机语言: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"

10);

baseimages[1] = new ImageIcon(MapPic.class.getResource("m2.png"

10);

baseimages[2] = new ImageIcon(MapPic.class.getResource("m3.png"

10);

baseimages[3] = new ImageIcon(MapPic.class.getResource("m4.png"

10);

baseimages[4] = new ImageIcon(MapPic.class.getResource("m5.png"

10);

baseimages[5] = new ImageIcon(MapPic.class.getResource("m6.png"

10);

baseimages[6] = new ImageIcon(MapPic.class.getResource("m7.png"

10);

baseimages[7] = new ImageIcon(MapPic.class.getResource("m8.png"

10);

baseimages[8] = new ImageIcon(MapPic.class.getResource("m9.png"

10);

itemimages = new ImageIcon[3];

itemimages[0] = new ImageIcon(MapPic.class.getResource("error.png"

10);

itemimages[1] = new ImageIcon(MapPic.class.getResource("i1.png"

10);

itemimages[2] = new ImageIcon(MapPic.class.getResource("i2.png"

10);

}

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!"

10;

}

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!"

10;

} 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"

10);

DataOutputStream itembinaryfile = new DataOutputStream(

new FileOutputStream(MapEditor.filename + "item"

10);

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"

10;

}

}

结束!

就这么简单!但是问题很多很多.都不能叫程序,随便写的玩的哦

需要源代码的朋友请留下email,要指出bug哦

亦或者msn交流:chinuxman@hotmail.com

欢迎大家阅读《用Java写一个地图编辑器》,跪求各位点评,若觉得好的话请收藏本文,by 搞代码

原创文章,转载请注明: 转载自搞代码

e7ce419cf2d6ad34d01da2ceb8829eed.png

微信 赏一包辣条吧~

023a57327877fb4402bcc76911ec18ea.png

支付宝 赏一听可乐吧~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值