java swing 嵌入地图_Java学习笔记之SWING — 基本SWING程序(实现外部地图文件导入并显示) | 学步园...

前言:不好意思啊,昨天去柯桥了趟,基本没怎么碰电脑, 不过脑子里还是想这点这个地图编辑器的问题的,开始一直在想怎么解决外部文件读入导致换行符的混入,后来发现BufferedReader类的readLine方法,爽,直接过滤,省了我不少的麻烦,嘿嘿。还有就是我对地图文件做了稍稍的改动,头部加上了两个表示地图大小的标注,希望以后能用到。好,废话少说,看具体的吧~我截图都截好了那~ 都已经弄了这么晚了,看来又要明天凌晨才能发稿了……厄,这个,不是我臭屁,但是想来想去还是说一下,版权所有,如要转载清注明出处http://www.blog.net/Sozell和作者:SosoAyaen。小弟我写点东西不容易,谢谢。

先说下关于上次做过的地图的载入问题。我是通过一个设置Layout布局来决定图片的布局规格的,而当要载入地图时来推算到底是几行几列的我觉得没有必要(不是说

o_10X10%e5%9b%be%e7%89%87.JPG   10

10

EEEEEEEEEE

EGGGGGGGGE

EGFGFGFGFE

EEEEGGFGGE

EEEEEFFGEE

EEEEGGFEEE

EEEGGGGGGE

EEEEEEEEEE

EEGGGGGGGE

EEEEEEEEEE

开头就是宽和高的参数了。既然是专门用来读取地图文件的,所以我就直接给它做了个类:MapFileReader。下面给出这个类的代码:

None.gifimportjava.io.*;

None.gif

ExpandedBlockStart.gif

ContractedBlock.gifpublicclassMapFileReader...{

6a9c071a08f1dae2d3e1c512000eef41.pngprivateBufferedReader buffReader;

6a9c071a08f1dae2d3e1c512000eef41.pngprivateintmapX, mapY;

6a9c071a08f1dae2d3e1c512000eef41.pngprivateString mapStr;

6a9c071a08f1dae2d3e1c512000eef41.png

ExpandedSubBlockStart.gif

ContractedSubBlock.gif/** *//**6a9c071a08f1dae2d3e1c512000eef41.png     * Method MapFileReader

6a9c071a08f1dae2d3e1c512000eef41.png     *

6a9c071a08f1dae2d3e1c512000eef41.png     *@paramfileName

ExpandedSubBlockEnd.gif*/ExpandedSubBlockStart.gif

ContractedSubBlock.gifpublicMapFileReader(String fileName)throwsFileNotFoundException, IOException...{

6a9c071a08f1dae2d3e1c512000eef41.png//TODO: 在这添加你的代码6a9c071a08f1dae2d3e1c512000eef41.pngbuffReader=newBufferedReader (newFileReader(fileName) );

6a9c071a08f1dae2d3e1c512000eef41.png        mapX=Integer.parseInt ( buffReader.readLine() );

6a9c071a08f1dae2d3e1c512000eef41.png        mapY=Integer.parseInt ( buffReader.readLine() );

6a9c071a08f1dae2d3e1c512000eef41.png        String str;

6a9c071a08f1dae2d3e1c512000eef41.png        StringBuffer strBuff=newStringBuffer();

ExpandedSubBlockStart.gif

ContractedSubBlock.gifwhile((str=buffReader.readLine())!=null)...{

6a9c071a08f1dae2d3e1c512000eef41.png            strBuff.append(str);

ExpandedSubBlockEnd.gif        }6a9c071a08f1dae2d3e1c512000eef41.png        mapStr=strBuff.toString();

ExpandedSubBlockEnd.gif    }6a9c071a08f1dae2d3e1c512000eef41.png

ExpandedSubBlockStart.gif

ContractedSubBlock.gif/** *//**6a9c071a08f1dae2d3e1c512000eef41.png     * Method close

6a9c071a08f1dae2d3e1c512000eef41.png     * close the BufferedReader

6a9c071a08f1dae2d3e1c512000eef41.png     *

ExpandedSubBlockEnd.gif*/ExpandedSubBlockStart.gif

ContractedSubBlock.gifpublicvoidclose()throwsIOException...{

6a9c071a08f1dae2d3e1c512000eef41.png//TODO: 在这添加你的代码6a9c071a08f1dae2d3e1c512000eef41.pngbuffReader.close();

ExpandedSubBlockEnd.gif    }ExpandedSubBlockStart.gif

ContractedSubBlock.gif/** *//**6a9c071a08f1dae2d3e1c512000eef41.png     * Method getMapX 

6a9c071a08f1dae2d3e1c512000eef41.png     *        得到地图的宽

6a9c071a08f1dae2d3e1c512000eef41.png     *

6a9c071a08f1dae2d3e1c512000eef41.png     *

6a9c071a08f1dae2d3e1c512000eef41.png     *@return6a9c071a08f1dae2d3e1c512000eef41.png     *

ExpandedSubBlockEnd.gif*/ExpandedSubBlockStart.gif

ContractedSubBlock.gifpublicintgetMapX()...{

6a9c071a08f1dae2d3e1c512000eef41.png//TODO: 在这添加你的代码6a9c071a08f1dae2d3e1c512000eef41.pngreturnthis.mapX;

ExpandedSubBlockEnd.gif    }6a9c071a08f1dae2d3e1c512000eef41.png

ExpandedSubBlockStart.gif

ContractedSubBlock.gif/** *//**6a9c071a08f1dae2d3e1c512000eef41.png     * Method getMapY

6a9c071a08f1dae2d3e1c512000eef41.png     *        得到地图的高

6a9c071a08f1dae2d3e1c512000eef41.png     *

6a9c071a08f1dae2d3e1c512000eef41.png     *@return6a9c071a08f1dae2d3e1c512000eef41.png     *

ExpandedSubBlockEnd.gif*/ExpandedSubBlockStart.gif

ContractedSubBlock.gifpublicintgetMapY()...{

6a9c071a08f1dae2d3e1c512000eef41.png//TODO: 在这添加你的代码6a9c071a08f1dae2d3e1c512000eef41.pngreturnthis.mapY;

ExpandedSubBlockEnd.gif    }6a9c071a08f1dae2d3e1c512000eef41.png

ExpandedSubBlockStart.gif

ContractedSubBlock.gif/** *//**6a9c071a08f1dae2d3e1c512000eef41.png     * Method getMapFileToString

6a9c071a08f1dae2d3e1c512000eef41.png     *

6a9c071a08f1dae2d3e1c512000eef41.png     *

6a9c071a08f1dae2d3e1c512000eef41.png     *@return6a9c071a08f1dae2d3e1c512000eef41.png     *

ExpandedSubBlockEnd.gif*/ExpandedSubBlockStart.gif

ContractedSubBlock.gifpublicString getMapFileToString()...{

6a9c071a08f1dae2d3e1c512000eef41.png//TODO: 在这添加你的代码6a9c071a08f1dae2d3e1c512000eef41.pngreturnthis.mapStr;

ExpandedSubBlockEnd.gif    }6a9c071a08f1dae2d3e1c512000eef41.png

ExpandedSubBlockStart.gif

ContractedSubBlock.gif/** *//**6a9c071a08f1dae2d3e1c512000eef41.png     * Method main

6a9c071a08f1dae2d3e1c512000eef41.png     *

6a9c071a08f1dae2d3e1c512000eef41.png     *

6a9c071a08f1dae2d3e1c512000eef41.png     *@paramargs

6a9c071a08f1dae2d3e1c512000eef41.png     *

ExpandedSubBlockEnd.gif*/ExpandedSubBlockStart.gif

ContractedSubBlock.gifpublicstaticvoidmain(String[] args)...{

6a9c071a08f1dae2d3e1c512000eef41.png//TODO: 在这添加你的代码ExpandedSubBlockStart.gif

ContractedSubBlock.giftry...{

6a9c071a08f1dae2d3e1c512000eef41.pngchar[] buf;

6a9c071a08f1dae2d3e1c512000eef41.png            MapFileReader mfr=newMapFileReader("../mapFile/map1.txt");

6a9c071a08f1dae2d3e1c512000eef41.png            buf=mfr.getMapFileToString().toCharArray();

6a9c071a08f1dae2d3e1c512000eef41.png            System.out.println (mfr.getMapFileToString());

6a9c071a08f1dae2d3e1c512000eef41.png            System.out.println (buf);

ExpandedSubBlockStart.gif

ContractedSubBlock.giffor(inti=0; i<10*10; i++)...{

6a9c071a08f1dae2d3e1c512000eef41.png                System.out.printf("%d", LandStylePic.getIndex(buf[i]));

ExpandedSubBlockEnd.gif            }6a9c071a08f1dae2d3e1c512000eef41.png            System.out.println();

ExpandedSubBlockEnd.gif        }ExpandedSubBlockStart.gif

ContractedSubBlock.gifcatch(FileNotFoundException e)...{

6a9c071a08f1dae2d3e1c512000eef41.png            e.printStackTrace();

ExpandedSubBlockEnd.gif        }ExpandedSubBlockStart.gif

ContractedSubBlock.gifcatch(IOException e)...{

6a9c071a08f1dae2d3e1c512000eef41.png            e.printStackTrace();

ExpandedSubBlockEnd.gif        }ExpandedSubBlockEnd.gif    }6a9c071a08f1dae2d3e1c512000eef41.png

ExpandedBlockEnd.gif}

因为每个类我都单独做了测试,所以都有一个main(String[] args)主函数。这个不会变成方法带入类中,所以其他类中用到此类也不会有其中的代码执行。注意:FileReader是现在用到的读入字符的类(具体自己看JDK,里面有详细说明),而它是没有readLine方法的,而BufferedReader是带缓冲的Reader,可以把FileReader作为BufferedReder构造的参数,就像套水管一样,套上BufferedReader出来的就能用BufferedReader的方法了。至于String和StringBuffer的区别不在本文讨论范围内,记住String是不能改变而StringBuffer是可变的就行了,为了不频繁申请内存资源,所以用StringBuffer比较好。readLine读进来的是字符串(String),开始的两行是要存到Int变量中去的,所以要调用Inetger.parseInt(String)方法把String转化为Int,而Integer是Int的类表示方法(理解性说法),它有很多方法,而Int之类的类型如Float、Double、char不是类,所以他们声明的变量从严格的意义上来说不是对象,所以有了对应的wrap类来提供对应的方法。

这个文件要加到前面的工程中去,或者放到前面那些程序的同文件夹下,然后再编译执行这个文件你就会看到一个比较有趣的画面,它对地图文件做了内部翻译,结果是内部索引号,说白了就是变成数字序列了。这是给“翻译图片”程序用的,呵呵。给个执行结果:

o_%e7%bf%bb%e8%af%91%e7%bb%93%e6%9e%9c.JPG

那么前面的MapsEditorFrame.java也要做相应的改动了。其实也就是把maps数组不手写,而直接把MapFileReader的对象的getMapFileToSting()的地图内容字符串给它就行了,由于返回的是个String类型,而String本身就带一个toCharArray()的方法,所以两次调用就直接把地图内容给放到maps的char类型数组里面了,而其他程序几乎不需要大的改动。(改动还是有的,比如添加了个设置地图大小的方法setMapSize(int x, int y))

None.gifimportjava.awt.*;

None.gifimportjava.awt.event.*;

None.gifimportjavax.swing.*;

None.gifimportjava.util.*;

None.gifimportjava.io.*;

None.gif

ExpandedBlockStart.gif

ContractedBlock.gif/** *//**6a9c071a08f1dae2d3e1c512000eef41.png * Sample application using JFrame.

6a9c071a08f1dae2d3e1c512000eef41.png *

6a9c071a08f1dae2d3e1c512000eef41.png *@authorSosoAyaen

6a9c071a08f1dae2d3e1c512000eef41.png *@version1.00 06/08/10

ExpandedBlockEnd.gif*/ExpandedBlockStart.gif

ContractedBlock.gifpublicclassMapsEditorFrameextendsJFrameimplementsActionListener...{

6a9c071a08f1dae2d3e1c512000eef41.png   

6a9c071a08f1dae2d3e1c512000eef41.png//容器组件等6a9c071a08f1dae2d3e1c512000eef41.pngprivateJButton jbOK=null, jbCancel=null, jbIcon=null,

6a9c071a08f1dae2d3e1c512000eef41.png                    jbSave=null, jbLoadMapFromFile=null;

6a9c071a08f1dae2d3e1c512000eef41.pngprivateJPanel jpCenter=null, jpIcon=null, jpSouth=null,

6a9c071a08f1dae2d3e1c512000eef41.png                    jpNorth=null, jpEast=null, jpEastCo=null;

6a9c071a08f1dae2d3e1c512000eef41.pngprivateJLabel jlMap=null, jlTitle=null, jlTips=null;

6a9c071a08f1dae2d3e1c512000eef41.pngprivateJMenu jmenu=null;

6a9c071a08f1dae2d3e1c512000eef41.pngprivateJMenuItem jmiExit=null;

6a9c071a08f1dae2d3e1c512000eef41.pngprivateJMenuBar jmb=null;

6a9c071a08f1dae2d3e1c512000eef41.png    

6a9c071a08f1dae2d3e1c512000eef41.pngprivateJTextField jtf=null;

6a9c071a08f1dae2d3e1c512000eef41.pngprivateintchange=1;//控制点击同一按钮后控制不同控件

6a9c071a08f1dae2d3e1c512000eef41.png    

6a9c071a08f1dae2d3e1c512000eef41.png//临时测试用的地图文件数组6a9c071a08f1dae2d3e1c512000eef41.pngprivatechar[] maps;

6a9c071a08f1dae2d3e1c512000eef41.png    

6a9c071a08f1dae2d3e1c512000eef41.png//随机数6a9c071a08f1dae2d3e1c512000eef41.pngprivateRandom rand=newRandom();

6a9c071a08f1dae2d3e1c512000eef41.pngprivateintWIDTH=5, HEIGHT=5;

6a9c071a08f1dae2d3e1c512000eef41.pngprivatefinalString mapFilePath="../mapFile/";

6a9c071a08f1dae2d3e1c512000eef41.png//private Dimension d = null;6a9c071a08f1dae2d3e1c512000eef41.pngExpandedSubBlockStart.gif

ContractedSubBlock.gif/** *//**6a9c071a08f1dae2d3e1c512000eef41.png     * The constructor.

ExpandedSubBlockEnd.gif*/ExpandedSubBlockStart.gif

ContractedSubBlock.gifpublicMapsEditorFrame (String title)...{

6a9c071a08f1dae2d3e1c512000eef41.pngsuper(title);

6a9c071a08f1dae2d3e1c512000eef41.pngthis.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

6a9c071a08f1dae2d3e1c512000eef41.png        

6a9c071a08f1dae2d3e1c512000eef41.png//载入地图文件ExpandedSubBlockStart.gif

ContractedSubBlock.giftry...{

6a9c071a08f1dae2d3e1c512000eef41.png            MapFileReader mfr=newMapFileReader (mapFilePath+"map1.txt");

6a9c071a08f1dae2d3e1c512000eef41.png            maps=mfr.getMapFileToString().toCharArray();

p>%0A%0A<div%20style=

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值