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]);
}
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
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
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
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.