java地图导航系统代码 java地图api( 二 )


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"/images/wink.gif[/img]);
baseimages[1] = new imageicon(mappic.class.getresource("m2.png"/images/wink.gif[/img]);
baseimages[2] = new imageicon(mappic.class.getresource("m3.png"/images/wink.gif[/img]);
baseimages[3] = new imageicon(mappic.class.getresource("m4.png"/images/wink.gif[/img]);
baseimages[4] = new imageicon(mappic.class.getresource("m5.png"/images/wink.gif[/img]);
baseimages[5] = new imageicon(mappic.class.getresource("m6.png"/images/wink.gif[/img]);
baseimages[6] = new imageicon(mappic.class.getresource("m7.png"/images/wink.gif[/img]);
baseimages[7] = new imageicon(mappic.class.getresource("m8.png"/images/wink.gif[/img]);
baseimages[8] = new imageicon(mappic.class.getresource("m9.png"/images/wink.gif[/img]);
itemimages = new imageicon[3];
itemimages[0] = new imageicon(mappic.class.getresource("error.png"/images/wink.gif[/img]);
itemimages[1] = new imageicon(mappic.class.getresource("i1.png"/images/wink.gif[/img]);
itemimages[2] = new imageicon(mappic.class.getresource("i2.png"/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!"/images/wink.gif[/img];
}
map[pressedwidth][pressedheight] = temp_x;
pressedbutton.seticon((icon) pic.getimageicon(temp_x,
temp_y));

推荐阅读