Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
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 3637 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
« 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

Myomyomyo.
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.

"It's after the end of the world! Don't you know that yet?"
Offline appel

JGO Wizard


Medals: 51
Projects: 4


I always win!


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

Why not use Tiled?
http://www.mapeditor.org/

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

Check out the 4K competition @ www.java4k.com
Check out GAMADU (my own site) @ http://gamadu.com/
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?
http://www.mapeditor.org/

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?
http://www.mapeditor.org/

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.

 

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

The first screenshot will be displayed as a thumbnail.

Longarmx (38 views)
2014-10-17 03:59:02

Norakomi (29 views)
2014-10-16 15:22:06

Norakomi (24 views)
2014-10-16 15:20:20

lcass (28 views)
2014-10-15 16:18:58

TehJavaDev (56 views)
2014-10-14 00:39:48

TehJavaDev (55 views)
2014-10-14 00:35:47

TehJavaDev (46 views)
2014-10-14 00:32:37

BurntPizza (64 views)
2014-10-11 23:24:42

BurntPizza (36 views)
2014-10-11 23:10:45

BurntPizza (78 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!