Write a map editor with Java

xiaoxiao2021-03-06  41

Write a map editor with Java

I remember that when the media interviewed C , he said as a programmer, and believed that he can solve anything you have understood.

In other words: You can solve any problems, just want to understand

Reality Problem: Develop a map editor based on floor-tile-based two-dimensional game, require two binary files, each containing a two-dimensional array, *. Map stores floor tiles, floral grass, such as a propan, such as some Point may trigger an event. It is very simple, write. See here you have led to the overall structure of the program.

Computer language: java.

To understand the event must be analyzed

Preliminary view, map editor: Generate a number of arrays, no matter which form of array, your purpose:

Generate an array. The map is actually a two-dimensional coordinate system of (x, y), which is easy to contact:

I am ready to set the two program interfaces (main interface / map interface), Java's layout manager is not good, not as separate, the main interface uses JBuilder automatically created Application module (with menu). Map interface yourself, Also JFRAME, the class is transmitted between each other, and the MAP interface will be initialized at the beginning of the program, or it can initialize from the main interface (problem)

Build procedure

The following is 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] .SetButtonti (i); basemapButton [i] .addactionListener; BasePanel.Add (BaseMapButton [i]);} itempanel.setlayout; 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 yourself

The following is a 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) {superTe 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: Intance of your own Mappic class:

The following program code is: 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 "[iv] /images/wink.gif [/ img]); baseImages [1] = NEW Imageicon ("m2.png" [IMG] /images/wink.gif [/ img]); baseImages [2] = new imageicon ("mappic.class.getResource (" m3.png "[ivg ] /images/wink.gif [/ img]); baseImages [3] = new imageicon ("m4.png" [gg] /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 "[iv] /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" [iv] /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 (" i1.png "[IMG] /images/wink.gif [/ img]); ItemImages [2] = new imageicon ("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;}}

Write MapButton When processing the event, you can get the click coordinates of the button. I forgot to say, the Map interface I used the button instead of the map checkered. This is easy to think, the most stupid is also the most powerful way PIC separately It is better to change, and when the content changes, it is easy to change, and it is hard to write.

Below is an incident.

There are two events to process, the first is the button event, the second menu event

Button event I set this structure

The following is a 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 generates a category separately, I don't understand, I don't understand. It's really high.

Menu event model jBuilder you add .overwrite

The following is program code: public void * _ActionPerformed (ActionEvent E) {...}

Use two intermediate values ​​from the main interface to the MAP interface to pass what:

Here is the event handler of the button in the MAP interface

The following is a 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]! = 0) {item [PressedWidth] [PRESSEDHEIGHT] = 0; JFM.ShowMessage ("The props here have been blank! / NThe Item Has Been Null! "[img] /images/wink.gif [/ img];} map [PRESSEDWIDTH] [PRESSEDHEIGHT] = TEMP_X; PRESSEDBUTTON.SETICON ((icon) Pic.GetImageicon;} else {ix Map [PressedWidth] == 0) {JFM.ShowMessage ("The props can not be placed in this! / nnot put item at this point!" [img] /images/wink.gif [/ img];} else {ix (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 is the two intermediate values?

Enterprise

Finally, it generates MAP

The following program code is: 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 = 0; i

It's so simple! But there are a lot of problems. You can't call the program, just write it.

Friends who need source code, please leave an email, to point out the bug.

Or MSN communication: chinuxman@hotmail.com

转载请注明原文地址:https://www.9cbs.com/read-78235.html

New Post(0)