matlab画中国地图代码

matlab画中国地图代码,第1张

用matlab画地map-baidu图,需要安装其中的mapping toolbox工具箱,还需要找到你需要的矢量文件卜燃野.shp后缀的(这个要去找型喊tianditu才能获取)

代码如下

代码中需要用到的函数有shaperead, geoshow这两个函数

运行效果如上

geoshow函数用于展示从shp文件中获取指定名段禅字的图层,shaperead函数则是读取指定shp文件的数据

shaperead函数

geoshow函数

用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 = 0i <9i++) {

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 = 0i <3i++) {

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 = 0i <heighti++)

for (byte j = 0j <widthj++) {

//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]

}

}

1.这里的AxMapcontrol pMapControl是你所传入的参数,指你要 *** 作的地图所在的mapcontrol

2.ICommand pCmd = new ControlsMapPanToolClass()

是山前指新建一个ICommand类型的pCmd并赋值为 AE封装好的ControlsMapPanToolClass,贺册也就是平移工具

3.pCmd.OnCreate(pMapControl.Object)

为pCmd选择要创建的作用对象,即你所 *** 作的地图object

4.pMapControl.CurrentTool = pCmd as ITool

将你要 *** 作的地逗拍清图所在的mapcontrol的当前工具设定为刚刚搞定的平移工具。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12540345.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-26
下一篇 2023-05-26

发表评论

登录后才能评论

评论列表(0条)

保存