Sign in to follow this  
Chryzmo

[Java] MouseEnter and MousePressed Together

Recommended Posts

Hey all, I'm working on a map editor for a 2D game I want to write. I am writing this editor in Java Swing and I've come across something I cannot find a solution for. I want to be able to select the tile I am drawing with, then click, hold, and drag the mouse to be able to continuously draw over the map. In order to do this I need to be able to tell if the mouse button is being pressed when the mouse enters a tile. I understand the basic use of MouseListeners and MouseEvents. Is there a way that I can tell if the left mouse button is being held down at the time that the cursor enters a tile, using mouseEntered()? Thanks, Chris Moore

Share this post


Link to post
Share on other sites
Quote:
Original post by Chryzmo
I understand the basic use of MouseListeners and MouseEvents. Is there a way that I can tell if the left mouse button is being held down at the time that the cursor enters a tile, using mouseEntered()?

Uh, how are you doing your tile map? It sounds like you've got a component per-tile, which is a crazy way of doing things. Instead create a Canvas and draw all your tiles into it when needed. Then you can use MouseMotionListener to handle drag events on the whole canvas.

Share this post


Link to post
Share on other sites
Yes, I have a series of MapTiles (custom class) that are stored in a MapPanel(extended JPanel) and then displayed via my GUI.

Each tile has more properties than just the graphic it shows, such as Passable, Obstacle, Teleport, Door, Item, Monster, etc. Each of these items is able to be accessed and modified via another JPanel in the editor. This way the user just clicks on the tile and the side JPanel updates, showing the current tile's information.

Would I be able to do this with a Canvas? Why would the Canvas approach be better than the one I'm currently taking?

Thanks,
Chris

Share this post


Link to post
Share on other sites
Trying to do a tilemap with lots of individual Swing components is a common mistake by people who have been coding GUIs too long. ;)

Other than the obvious mouse event issues that you're having, this approach is heavy on memory and overheads and doesn't really gain you anything. It'll also make scrolling a real pain too (and don't even think of using a scrollpane for that, thats even more silly).

Use a single canvas, draw the tiles yourself and manually resolve mouse clicks onto the specific tile with some simple maths. Then you can open your custom attributes panel for that particular tile.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this