Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (115)
games submitted by our members
Games in WIP (562)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Making a map editor  (Read 1165 times)
0 Members and 1 Guest are viewing this topic.
Offline jparril1

Junior Member





« Posted 2011-04-06 13:51:23 »

Hey Guys,

     So I thought It would be useful to make myself a simple tile map editor. My plan is to have a window with 2 sections, a tile library and a grid canvas. The user can select a tile type from the library and use it to paint onto the grid. The plan is to store this as a simple 2d array, then when saved, write it to a file using numbers (1 is grass, 2 is water, 3 is dirt, etc) The problem is I am having trouble getting the design down. I cant figure out how to translate what they place in the grid into an array, and then translate that to a file. I guess Im having a disconnection between the GUI and the back end and I cant figure out how to have them communicate
Offline steveyO
« Reply #1 - Posted 2011-04-06 14:55:19 »

Am not sure am following.  When they place a tile in the grid can you simply not take the X and Y mouse coordinates and divide by the tile width/height.  This would give you your array position x and y position (and set this value to the currently selected tile) with your Bounds checking of course.  (I assume you are using a 2x2 array).   Alternatively, have you checked out http://www.mapeditor.org/ and http://tilestudio.sourceforge.net/.  You may be able to adapt these to suit your needs.

https://play.google.com/store/apps/details?id=com.bullsquared.alggame Annoying Little Gits (Android)
www.bullsquared.com   Play java (applet) games! www.chessclockpro.com Free Online Chess Clock
Offline jparril1

Junior Member





« Reply #2 - Posted 2011-04-06 15:10:26 »

Well yes those example programs are pretty much what I want to make. I do want to make my own though, I think it will be a good learning experience. I think getting the mouse coordinates should work. What I have set up now is one main JFrame, and 2 JPanels (one for the actual map and one for the library of tiles). My plan was to put a separate canvas on each of the panels so that I can draw on each section separately. Ive been having some problems getting both canvases working. I never experience an issue in my games drawing on one canvas, but adding two different canvases to two different panels is creating an issue. I think I might just be messing up the packing with swing. Ill post code later to show you guys.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline steveyO
« Reply #3 - Posted 2011-04-06 15:34:27 »

Ok see what you mean now :-) Swing issues huh.. Can't really help you with this (hate Swing).  In the past I created a couple of Map Editors using plain Java2D, and using the techniques I described earlier to place the tiles/save the array.  It worked very well and was very easy to code, however not really suitable if you want other users to use your tool, in which case better stick with your approach... Good luck!

https://play.google.com/store/apps/details?id=com.bullsquared.alggame Annoying Little Gits (Android)
www.bullsquared.com   Play java (applet) games! www.chessclockpro.com Free Online Chess Clock
Offline jparril1

Junior Member





« Reply #4 - Posted 2011-04-06 16:39:21 »

Ok see what you mean now :-) Swing issues huh.. Can't really help you with this (hate Swing).  In the past I created a couple of Map Editors using plain Java2D, and using the techniques I described earlier to place the tiles/save the array.  It worked very well and was very easy to code, however not really suitable if you want other users to use your tool, in which case better stick with your approach... Good luck!

Yes I hate swing dearly. My plan was to mainly use Java2d. The only place I am touching swing is my main Frame and Panels. Once I added my 2 canvases to their respective panels I was planning on doing all of the scrollbars, buttons, graphics, etc with custom JAva2d shapes and images. The only reason I was using Jpanels is because I really dont know any other way of supporting a canvas. The only way Ive ever known to do this (I use this in games as well) Is to make a JFrame, pack a Jpanel in it, then add a canvas to that Jpanel. Then I create a buffer strategy on the canvas and get the graphics context from that buffer strategy and use that to draw. IS there a way to avoid Jpanels and all that?

Also, I like to write my own render methods so I dont use paint() and repaint() at all. Thats why I use the buffer strategy to get the Graphics object.
Offline jparril1

Junior Member





« Reply #5 - Posted 2011-04-06 20:41:24 »

Ok maybe you guys can help, heres my code. Apparently something is wrong with my buffer strategys when I create the buffer. Any ideas?? This code has worked fine in my games...!

Heres the main Window class

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
package userInterface;

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.MenuBar;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.border.Border;

public class Window {

   //window areas
  private JFrame mainFrame;

   private JPanel canvasPanel;
   private JPanel tileLibraryPanel;

   //drawing canvases
  private TileLibraryCanvas tileLibraryCanvas;
   private MapCanvas mapCanvas;

   //menubar
  private JMenuBar menuBar;
   private JMenu fileButton;

   //borders
  Border tileLibraryBorder;
   Border canvasAreaBorder;



   public Window() {

      //initializng the frame and inner panels
     mainFrame = new JFrame("Map Editor");
      canvasPanel = new JPanel();
      tileLibraryPanel = new JPanel();

      //initializing the canvases
     tileLibraryCanvas = new TileLibraryCanvas();
      mapCanvas = new MapCanvas();

      //initializing menu bar and items
     menuBar = new JMenuBar();
      fileButton = new JMenu("File");

      //adding the menubar to the frame
     mainFrame.setJMenuBar(menuBar);

      //adding the filebutton to the menu bar
     menuBar.add(fileButton);

      //creating borders
     Border tileLibraryBorder = BorderFactory.createTitledBorder("Tile Library");
      Border canvasAreaBorder = BorderFactory.createTitledBorder("Map");

      //setting panel sizes
     canvasPanel.setPreferredSize(new Dimension(600, 600));
      tileLibraryPanel.setPreferredSize(new Dimension(200, 600));

      //adding things to panels
     canvasPanel.setBorder(canvasAreaBorder);
      tileLibraryPanel.setBorder(tileLibraryBorder);

      canvasPanel.add(mapCanvas);
      tileLibraryPanel.add(tileLibraryCanvas);

      //setting layout and adding panels to frame
     mainFrame.getContentPane().setLayout(new BorderLayout());
      mainFrame.getContentPane().add(canvasPanel, BorderLayout.CENTER);
      mainFrame.getContentPane().add(tileLibraryPanel, BorderLayout.WEST);


      //setting various attributes
     mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      mainFrame.setPreferredSize(new Dimension(800, 600));
      mainFrame.setResizable(false);
      mainFrame.pack();
      mainFrame.setVisible(true);

   }

}



And here are the two canvases, they are identical basically
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
package userInterface;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferStrategy;

public class MapCanvas extends Canvas {

   private static final long serialVersionUID = 5075462132756421808L;

   BufferStrategy bufferStrategy;

   public MapCanvas() {

      setVisible(true);
      setIgnoreRepaint(true);

      createBufferStrategy(2);
      bufferStrategy = getBufferStrategy();

   }


   public void render() {

      Graphics2D g = (Graphics2D) bufferStrategy.getDrawGraphics();

      g.setColor(Color.GREEN);
      g.fillRect(0, 0, 90, 50);  
   }
}


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
package userInterface;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferStrategy;

public class TileLibraryCanvas extends Canvas {


   BufferStrategy bufferStrategy;
   
   public TileLibraryCanvas() {

      setVisible(true);
      setIgnoreRepaint(true);
     
      createBufferStrategy(2);
      bufferStrategy = getBufferStrategy();

   }

   public void render() {

      Graphics2D g = (Graphics2D) bufferStrategy.getDrawGraphics();
     
      g.setColor(Color.GREEN);
      g.fillRect(0, 0, 90, 50);  
   }
}


Heres the error:

Exception in thread "main" java.lang.IllegalStateException: Component must have a valid peer
   at java.awt.Component$FlipBufferStrategy.createBuffers(Unknown Source)
   at java.awt.Component$FlipBufferStrategy.<init>(Unknown Source)
   at java.awt.Component$FlipSubRegionBufferStrategy.<init>(Unknown Source)
   at java.awt.Component.createBufferStrategy(Unknown Source)
   at java.awt.Canvas.createBufferStrategy(Unknown Source)
   at java.awt.Component.createBufferStrategy(Unknown Source)
   at java.awt.Canvas.createBufferStrategy(Unknown Source)
   at userInterface.TileLibraryCanvas.<init>(TileLibraryCanvas.java:19)
   at userInterface.Window.<init>(Window.java:48)
   at userInterface.Driver.main(Driver.java:7)


I want to note that if I pass 1 to createBufferStrategy() it compiles and runs, but the canvases are not painted so Im not even sure if theyre there.
Offline jparril1

Junior Member





« Reply #6 - Posted 2011-04-07 01:29:11 »

Ok so I found out that I was trying to get the buffer before the canvas was added to the JPanel. I read that If I add this to addNotify(), it will only happen when its added to the panel. The error is gone but now I cant even see my panels at all. Whats going on? Building the Frame and Panel and Canvas layer is confusing me and I think I must be missing something.

Heres the new code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
package userInterface;

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.MenuBar;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.border.Border;

public class Window {

   //window areas
  private JFrame mainFrame;

   private JPanel canvasPanel;
   private JPanel tileLibraryPanel;

   //drawing canvases
  private TileLibraryCanvas tileLibraryCanvas;
   private MapCanvas mapCanvas;

   //menubar
  private JMenuBar menuBar;
   private JMenu fileButton;

   //borders
  Border tileLibraryBorder;
   Border canvasAreaBorder;



   public Window() {

      //initializng the frame and inner panels
     mainFrame = new JFrame("Map Editor");
      canvasPanel = new JPanel();
      tileLibraryPanel = new JPanel();

      //initializing the canvases
     tileLibraryCanvas = new TileLibraryCanvas();
      mapCanvas = new MapCanvas();

      //initializing menu bar and items
     menuBar = new JMenuBar();
      fileButton = new JMenu("File");

      //adding the menubar to the frame
     mainFrame.setJMenuBar(menuBar);

      //adding the filebutton to the menu bar
     menuBar.add(fileButton);

      //creating borders
     Border tileLibraryBorder = BorderFactory.createTitledBorder("Tile Library");
      Border canvasAreaBorder = BorderFactory.createTitledBorder("Map");

      //setting panel sizes
     canvasPanel.setPreferredSize(new Dimension(600, 600));
      tileLibraryPanel.setPreferredSize(new Dimension(200, 600));

      //adding things to panels
     canvasPanel.setBorder(canvasAreaBorder);
      tileLibraryPanel.setBorder(tileLibraryBorder);

      canvasPanel.add(mapCanvas);
      tileLibraryPanel.add(tileLibraryCanvas);

      //setting layout and adding panels to frame
     mainFrame.getContentPane().setLayout(new BorderLayout());
      mainFrame.getContentPane().add(canvasPanel, BorderLayout.CENTER);
      mainFrame.getContentPane().add(tileLibraryPanel, BorderLayout.WEST);


      //setting various attributes
     mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      mainFrame.setPreferredSize(new Dimension(800, 600));
      mainFrame.setResizable(false);
      mainFrame.setIgnoreRepaint(true);
      mainFrame.pack();
      mainFrame.setVisible(true);

   }

}


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
package userInterface;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferStrategy;

public class TileLibraryCanvas extends Canvas {


   BufferStrategy bufferStrategy;

   public TileLibraryCanvas() {

      setVisible(true);
      setIgnoreRepaint(true);


   }

   public void render() {

      Graphics2D g = (Graphics2D) bufferStrategy.getDrawGraphics();

      g.setColor(Color.GREEN);
      g.fillRect(0, 0, 90, 50);

      g.dispose();
      bufferStrategy.show();
   }

   public void addNotify() {

      super.addNotify();

      createBufferStrategy(2);
      bufferStrategy = getBufferStrategy();
   }
}


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
package userInterface;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferStrategy;

public class MapCanvas extends Canvas {

   private static final long serialVersionUID = 5075462132756421808L;

   BufferStrategy bufferStrategy;

   public MapCanvas() {

      setVisible(true);
      setIgnoreRepaint(true);


   }


   public void render() {

      Graphics2D g = (Graphics2D) bufferStrategy.getDrawGraphics();

      g.setColor(Color.GREEN);
      g.fillRect(0, 0, 90, 50);  
     
      g.dispose();
      bufferStrategy.show();
   }
   
   public void addNotify() {

      super.addNotify();

      createBufferStrategy(2);
      bufferStrategy = getBufferStrategy();
   }
}
Offline ra4king

JGO Kernel


Medals: 346
Projects: 3
Exp: 5 years


I'm the King!


« Reply #7 - Posted 2011-04-07 01:39:06 »

You were getting that IllegalStateException because you need to call mainFrame.setVisible(true) before creating the BufferStrategy.

EDIT: Putting "createBufferStrategy(2)" in addNotify() works too Cheesy

Also, you can completely avoid using JPanel here at all. I don't understand why you need to add each Canvas to a JPanel when adding them directly to the JFrame works too. Also, you don't need to call "getContentPane()" because that is done automatically for you for the "add()" and "setLayout()" methods.

1  
2  
3  
4  
...
mainFrame.add(mapCanvas, BorderLayout.CENTER);
mainFrame.add(tileLibraryCanvas, BorderLayout.WEST);
...

Offline jparril1

Junior Member





« Reply #8 - Posted 2011-04-07 01:54:49 »

You were getting that IllegalStateException because you need to call mainFrame.setVisible(true) before creating the BufferStrategy.

EDIT: Putting "createBufferStrategy(2)" in addNotify() works too Cheesy

Also, you can completely avoid using JPanel here at all. I don't understand why you need to add each Canvas to a JPanel when adding them directly to the JFrame works too. Also, you don't need to call "getContentPane()" because that is done automatically for you for the "add()" and "setLayout()" methods.

1  
2  
3  
4  
...
mainFrame.add(mapCanvas, BorderLayout.CENTER);
mainFrame.add(tileLibraryCanvas, BorderLayout.WEST);
...


Thanks! Well I was using JPanels as a way to separate the frame into 2 sections, one for the map and one for the tile selection. If I just added the canvases directly to the JFrame, how would I go about keeping them on their own side of the frame? The idea was to have about 1/3 of the frame be the tiles library and the rest to be the actual area to drop the tiles.

Also, even when I got rid of the error and stopped getting the contentpane, its still broken. Before I did anything with the canvases I could at least see my panels and borders, now my frame is basically just blank entirely, its actually glitched and showing a view of whats on my  desktop.
Offline ra4king

JGO Kernel


Medals: 346
Projects: 3
Exp: 5 years


I'm the King!


« Reply #9 - Posted 2011-04-07 02:11:41 »

If it's showing an image of your desktop, that means the program hung :/

EDIT: Canvas will be layed out just like JPanel and any other class that extends Component.

Couple things I also noticed looking through the code:
-You don't need to call setVisible(true) and setIgnoreRepaint(true) in your canvases because its already been done to the frame.
-You need to follow this design when using BufferStrategy (Scroll down to the code section).

Also, who calls render() or do you have an external game loop?

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline jparril1

Junior Member





« Reply #10 - Posted 2011-04-07 02:55:48 »

thanks ra4king, Im gonna need to look over this a bit more. It wasnt showing my desktop before, must be something I messed with. I even commented out the canvases to try to get it to work the way it used to, just with the jpanels, but no luck. If you have any ideas let me know. If not, Ill be picking at this for a while until I get it. Thanks again!

I think I might take as much swing out of this project as possible, and do what you said. I guess I can just size the canvases appropriately and just put them in the JFrame, avoiding the panels completely.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

BurntPizza (28 views)
2014-09-21 02:42:18

BurntPizza (18 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (27 views)
2014-09-20 20:14:06

BurntPizza (29 views)
2014-09-19 03:14:18

Dwinin (46 views)
2014-09-12 09:08:26

Norakomi (74 views)
2014-09-10 13:57:51

TehJavaDev (101 views)
2014-09-10 06:39:09

Tekkerue (50 views)
2014-09-09 02:24:56

mitcheeb (71 views)
2014-09-08 06:06:29
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!