Hi !
Featured games (85)
games approved by the League of Dukes
Games in Showcase (636)
Games in Android Showcase (178)
games submitted by our members
Games in WIP (685)
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  
  [Solved] Need help with yet another tile based map editor!  (Read 4224 times)
0 Members and 1 Guest are viewing this topic.
Offline Sane

Junior Newbie

« Posted 2011-07-04 10:26:12 »

I've been implementing a tile based map editor for some days now and I'm having some problems with the painting grid.

How do i do it? I started by creating a JPanel with gridlayout, and fill it with objects, but this doesn't seem like a good way to go. I'm thinking of  implementing one big canvas(?), check for mouseClick and calculate the position to draw the sprite on. Does it sound good?

I'm new to graphical programming in Java and I have no idea of what classes to use, so all help you can provide is appreciated!
Offline theagentd

« JGO Bitwise Duke »

Medals: 575
Projects: 4
Exp: 8 years

« Reply #1 - Posted 2011-07-04 13:14:39 »

Just attach a MouseListener to it. You can easily calculate the tile coordinates of the tile being clicked on by dividing the mouse coordinates with the size of a tile. If you have a moveable "camera" (translation) you'll have to factor that in too. Not really sure what "problem" you actually have here. =S
I started making a map editor for a tile based game with Java2D a few years ago, but I didn't get very far with it. Basically I used 2 different canvases, one for showing the map and the other for the tile chooser, where you choose what tile from a tileset to put on the screen. You may also want to implement some basic "painter tools" to speed up filling large areas with the same tile. Now that I think about it, just try to make a remake of MS Paint with tiles instead of colors. xD

Offline Sane

Junior Newbie

« Reply #2 - Posted 2011-07-04 15:57:22 »

Yeah, it's now possible to paint on my canvas, my only problem is that all that has been drawn resets everytime i either scroll or resize the window.Does it happen becuase of a method I've forgotten to override or something?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline philfrei
« Reply #3 - Posted 2011-07-04 20:50:14 »

I don't entirely understand what you are trying to do (probably do to lack of familiarity with this aspect of Game programming). Is a Map a collection of Tiles and their positions? When editing a tile-based map, is the goal to place already completed Tiles in a grid? Or are you still working on the images for the various tiles?

Using GridBag layout with a JPanel seems to me like a cool idea for an editor. I like it. Was that what you were first attempting? Each Tile is then made part of the display via an .add() method and paintComponent() will automatically include them in any redraw. (Assumes Tile subclasses a component of some sort.)

What sort of problems did you run up against when you tried that, may I ask?

If you are just going to draw directly onto the screen (via things like grid rectangles or images that correspond to the various tiles), maybe use a JComponent instead of a JPanel, as the extra functionality provided by a JPanel (ability to be a container) is wasted.

What is the problem that occurs with the redrawing? It takes too long? The position or size of things is altered? I do know that
when you resize, the program will trigger a repaint and try to apply whatever Layout you are using to the display window. Maybe using a null Layout and drawing all the graphics with absolute x & y values will reduce the redrawing.

music and music apps:
Offline appel

JGO Wizard

Medals: 69
Projects: 4

I always win!

« Reply #4 - Posted 2011-07-05 09:59:49 »

Why not use Tiled?

It's supported by many, like Slick and libgdx, and probably Java2D as well.

Check out the 4K competition @
Check out GAMADU (my own site) @
Offline captainsoft
« Reply #5 - Posted 2011-07-10 18:11:57 »

You shoud first create a good (yet simple) model of your TileMap. I am thinking of a Map class, and it could contains an 2D-Array of Tile objects, which have an Image property.

This is the basic model, you can later add properties for supporting overlays, checking for walkable areas, jagged (no square) maps and so on.

Derive from JPanel or JComponent, and override its paint-Method. Check for the JavaDoc of Graphics class. In the method you go through the tiles of the map and draw its images (using Graphic's methods).

With a MouseListener attached, you can check which Tile you have clicked on, change its Image and draw the component anew.

Try to play around a bit with it, the details depend on the style of you map and game.

Offline Sane

Junior Newbie

« Reply #6 - Posted 2011-07-11 19:55:20 »

I have made a lot of progress with my level editor. I started by extending JPanel and writing my on paintComponent method. When the MouseListener detects a button click, the logic counts where to draw the tile. In the background I have a Tile[][] matrix that contains data about every tile that has been added. This way it was easy for me to save the content (using ObjectOutputStream) and load it again when the user wishes to do so.

When I started this I wasn't familiar with the classes involved and I think that I have figured out most of it. I had a lot of issues with memory management and the application now runs at 100 mb memory. (Got it down to 100 from 200 mb). There is probably some bad code there that causes this, which I will take a look at later, but at least the application doesn't crash because of OutOfMemoryException anymore! I'll probably post some pictures when I have added all the necessary buttons, the model part is done, I just have to implement the view.
Offline Sane

Junior Newbie

« Reply #7 - Posted 2011-08-19 10:02:32 »

It's been a while since I worked with my editor, but I thought that it would be fun to post a picture of it! As a sign of that your help didn't go to waste!

Feel free to ask any questions if you wonder something Smiley I have implemented save and export functionality, so it's now possible to actually use the editor for any 2D-game!
Offline aazimon
« Reply #8 - Posted 2011-08-19 14:45:08 »

Your graphics look pretty good. I look forward to seeing the a game demo. Did you break all your sprite elements down to the size of your grid? Or do you all sprites to be a size factor of the grid size. i.e. sprite can take up 2x2 grids?
Offline SwampChicken
« Reply #9 - Posted 2011-08-22 04:33:26 »

Why not use Tiled?

IIRC, there is even a link to the Java source.
(so you can have a look around at how it handles tile maps)
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Sane

Junior Newbie

« Reply #10 - Posted 2011-08-22 17:10:24 »

Your graphics look pretty good. I look forward to seeing the a game demo. Did you break all your sprite elements down to the size of your grid? Or do you all sprites to be a size factor of the grid size. i.e. sprite can take up 2x2 grids?

I'm not sure if I understand your question correctly, but I'll explain how i have implemented it. When you start a new project you can define the size of each tile in pixels. In the code I have left support for two different parameters, one size for the tile set, and one size for the map. But at the moment you can just choose a common size for both of them. I have tested this by hard coding different sizes and my solution for handling the different sizes is to, say that you have chosen 32x32 pixels for each tile from the tile set and the map has tiles of size 50x50. What happens is that the tiles from the tile set are stretched to match the size of the map. Does it make sense? The reason why I haven't added support for this is in the GUI is because i don't know how to handle this when I export the completed level. I will later on add support for placing characters on the map, and they will be separated from the tile system, making it possible for them to have bigger textures separated from the tile system.

I haven't made the tile set myselft, it's just something I got from the Internet, but we have people drawing tiles, so hopefully our finalized products will look good.

Why not use Tiled?

IIRC, there is even a link to the Java source.
(so you can have a look around at how it handles tile maps)

I'm aware of that. When I started I had no idea of how image manipulation and everything related to level editors works in java. So instead of studying the code and try to understand I thought that in the end I would save time by implementing and learning it all by myself. Now when I have an idea of how this works I'm thinking of starting to contribute to Tiled instead. I've implemented a file-parser for it for one of my projects in C++ and I think that their solution for the exported file looks much better than mine. I did this since they have support for isometric view and it wasn't anything I was planning implement for my own editor.

And it's a bit too late to look at their source of how to handle tiles, since I'm done with my own implementation of it, but I'm sure they're a bit more organized than my negligently implemented MVC-pattern.
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

Dwinin (60 views)
2015-11-07 13:29:08

Rems19 (73 views)
2015-10-31 01:36:56

Rems19 (65 views)
2015-10-31 01:32:37

williamwoles (102 views)
2015-10-23 10:42:59

williamwoles (88 views)
2015-10-23 10:42:45

Jervac_ (102 views)
2015-10-18 23:29:12

DarkCart (129 views)
2015-10-16 00:58:11

KaiHH (112 views)
2015-10-11 14:10:14

KaiHH (150 views)
2015-10-11 13:26:18

BurntPizza (164 views)
2015-10-08 03:11:46
Rendering resources
by Roquen
2015-11-13 14:37:59

Rendering resources
by Roquen
2015-11-13 14:36:58

Math: Resources
by Roquen
2015-10-22 07:46:10

Networking Resources
by Roquen
2015-10-16 07:12:30

Rendering resources
by Roquen
2015-10-15 07:40:48

Math: Inequality properties
by Roquen
2015-10-01 13:30:46

Math: Inequality properties
by Roquen
2015-09-30 16:06:05

HotSpot Options
by Roquen
2015-08-29 11:33:11 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‑
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!