Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: 2D JButton Array

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Location
    Australia
    Posts
    13
    My Mood
    Relaxed
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 2D JButton Array

    Okay, so basically i am creating a Level Editor and i have two seperate JPanels on the JFrame. I want to be able to select a tile which is also a button from one JPanel which will have its own ID and then click on a space/button on the other JPanel which will then set that place in the array to the ID of the tile from the other JPanel and also set the image to the one attached to the ID (eg. grass - ID 0). After i've created the level it will be printed to a text file which will then be read by the engine.

    Am i doing this the right way or is there a better way of doing this?
    Thank you!!

    Level Text File / Array:
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
    0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
    0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0
    0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0
    0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0
    0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0
    0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0
    0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0
    0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 634 Times in 540 Posts

    Default Re: 2D JButton Array

    I'm not really sure what your question is...? Are you asking us if this approach works? The only way to answer that is to write the code and see what happens.

    I personally probably wouldn't use JButtons. Why can't the user just click directly in the JPanel that's showing the tiles?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Dec 2013
    Location
    Australia
    Posts
    13
    My Mood
    Relaxed
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: 2D JButton Array

    I'm asking whether the button approach would work, and if not, what is a better way exactly?
    With your question, well one panel pretty much handles what tile will be placed and the other panel is the position of the tile.

    I just have no idea what the best way to display the tiles on both panels is!
    Do you get what i mean?

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 634 Times in 540 Posts

    Default Re: 2D JButton Array

    The button approach might work. The only way to answer it is by coding it up.

    I'm not sure why you're displaying the tiles on both panels, but only letting the user click one of the panels. Why not let the user click the panel directly?

    Recommended reading: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    And: How to Write a Mouse Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Dec 2013
    Location
    Australia
    Posts
    13
    My Mood
    Relaxed
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: 2D JButton Array

    One "Tilesheet" panel shows the available tiles to place on the other "Map" panel. The "Map" panel shows the images in a grid, say 48x48. When a tile is placed on a position, that position in the array will change to the selected tile's ID.
    Sorry if i am repeating myself.

  6. #6
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: 2D JButton Array

    I would advice you to create your own custom component that will paint these things by itself.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,795
    Thanks
    52
    Thanked 2,329 Times in 2,285 Posts

    Default Re: 2D JButton Array

    I'd use buttons initially because they have so many features that would be useful. Later if the response time or the spacing gets to be a problem, create your own custom objects.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: March 10th, 2014, 08:26 PM
  2. array of type JButton
    By newbie23 in forum Member Introductions
    Replies: 1
    Last Post: June 16th, 2013, 05:43 AM
  3. Replies: 2
    Last Post: January 14th, 2013, 03:22 PM
  4. Persistence causing problems with JButton 2D Array
    By easyp in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 21st, 2010, 01:21 PM
  5. JButton...
    By chronoz13 in forum AWT / Java Swing
    Replies: 1
    Last Post: November 27th, 2009, 11:39 AM