Write a map editor in Java

Source: Internet
Author: User
Tags array interface return
Write a map editor in Java

Remember when the media interviewed the father of C + +, he said that as a programmer, you have to believe that you can solve everything you already understand.
In other words: You can solve any problem if you want to understand

Real problem: Develop a two-dimensional game based on the map editor, the request to generate two binary files, each contains a two-dimensional array, *.map storage floor tiles, flowers and so on. *.item props, such as a point may trigger an event. It's very simple, Write casually. See here you've got a general idea of the overall structure of the program.
Computer language: java.

To understand that events must be analyzed

Initially, Map Editor: Generate some form of array, regardless of which form of array, your purpose:
Generates an array. The map is actually a two-dimensional coordinate system (X,Y), which is very easy to connect to: no matter

I am going to set two program interface (main interface/map interface), the Java Layout manager is not good fiddling, as a separate two class, the main interface with JBuilder automatically created application module (with menu). The map interface writes itself, is also jframe, the class passes messages to each other, the map interface is initialized at the beginning of the program, or it can be initialized in the program from the main interface (problematic)

Build Programs
The following are program code:

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);




There are two places to explain:
Mapbutton: A class written by myself

The following are program code:

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: Write your own mappic class of intance:

The following are program code:

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");
BASEIMAGES[1] = new ImageIcon (MapPic.class.getResource ("m2.png");
BASEIMAGES[2] = new ImageIcon (MapPic.class.getResource ("m3.png");
BASEIMAGES[3] = new ImageIcon (MapPic.class.getResource ("m4.png");
BASEIMAGES[4] = new ImageIcon (MapPic.class.getResource ("m5.png");
BASEIMAGES[5] = new ImageIcon (MapPic.class.getResource ("m6.png");
BASEIMAGES[6] = new ImageIcon (MapPic.class.getResource ("m7.png");
BASEIMAGES[7] = new ImageIcon (MapPic.class.getResource ("m8.png");
BASEIMAGES[8] = new ImageIcon (MapPic.class.getResource ("m9.png");

Itemimages = new Imageicon[3];
Itemimages[0] = new ImageIcon (MapPic.class.getResource ("error.png");
ITEMIMAGES[1] = new ImageIcon (MapPic.class.getResource ("i1.png");
ITEMIMAGES[2] = new ImageIcon (MapPic.class.getResource ("i2.png");
}

Public ImageIcon Getimageicon (int x, int flags) {
if (flags = = 0) {
return baseimages[x];
else if (flags = = 1) {
return itemimages[x];
}
return null;
}
}




Write Mapbutton is to deal with the event can be accurate to get the coordinates of the button, forgot to say, the map interface I was using a button instead of the grid. It's easy to think, the stupidest and the most labor-saving way.

Pic Alone to write a good change, when the content changes, it is easy to change, hard to write without also casually.

Here's the thing.
There are two events to handle, the first is the button event, the second menu event
button Event I apply such a structure

The following are program code:

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 the button event event in a separate class, I do not understand, can not understand. It's very advanced.
The menu event model JBuilder itself. Overwrite

The following are program code:

public void *_actionperformed (ActionEvent e) {...}




What is passed from the main interface to the map interface with two intermediate values:

Here is the event handler for the button in the map interface

The following are program code:

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 ("Here props have been placed empty!\nthe item has been null!";
}
Map[pressedwidth][pressedheight] = temp_x;
Pressedbutton.seticon (Icon) Pic.getimageicon (temp_x,
temp_y));
} else {
if (map[pressedwidth][pressedheight] = = 0) {
Jfm.showmessage ("Props cannot be placed on this!\nnot putting item at this point!";
} 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;
}
}
}
}
};



What are the two median values? At a glance OH

Finally, generate the map

The following are program code:

public void Createmap () throws IOException {
try {
DataOutputStream mapbinaryfile = new DataOutputStream (
New FileOutputStream (Mapeditor.filename + "map");

DataOutputStream itembinaryfile = new DataOutputStream (
New FileOutputStream (Mapeditor.filename + "item");

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




End!
It's as simple as that! But there are a lot of problems. You can't call it a program, just write it, play it.
Need source code friend please leave an email, to point out the bug OH
or MSN Exchange: chinuxman@hotmail.com



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.