Results 1 to 5 of 5
  1. #1
    javaman1 is offline Member
    Join Date
    Sep 2010
    Posts
    56
    Rep Power
    0

    Default Mutliple JPanel's with an enhanced "for" loop

    I am trying to understand how to make a chessboard with the Java GUI. I know how to use an enhanced for loop, however, I do not know how to declare 64 JPanel objects, each with different names, without brute forcing the instantiation of each. How can i create 64 JPanel objects without brute forcing it?

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Constructors are very important - they make sure that the state of an instance of a class is consistent. (this includes all the state associated with all the super classes). So if you want 64 instances of some class, there will be 64 calls to some constructor or other.

    I'm not sure what "brute forcing" is. Perhaps if it's precisely defined it will turn out to be less of a menace/burden/threat than its characterisation would suggest.

    Likewise talking about the "names" of objects. Do you mean variables? You have whatever variables you need to talk about the objects. One way of referring to the objects that represent the squares of a chessboard would be to have them in some sort of collection like a 2D array. In that case as few as one variable would do for manipulating all 64 objects.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I might add that this is a classic case of a problem where you would work on the GUI after you have the behaviour of the chessboard figured out.

    A chessboard has little - in fact nothing - to do with a planar tessellation of differently coloured squares. First and foremost it's a collection of locations which are occupied (or not) at any given moment by a piece. And the pieces are able to move (change the location they occupy) based on the current state of occupation of other locations.

    Once these rules have been correctly coded it makes sense to consider a gui where the locations are represented by a visual element (possibly a JPanel, possibly not) that can give a visual representation of which piece occupies a location, and which can detect the user's intention for a piece to change its location to another valid one.

  4. #4
    javaman1 is offline Member
    Join Date
    Sep 2010
    Posts
    56
    Rep Power
    0

    Default

    What I want to do is make a 8x8 JFrame on the screen that has black and white JPanels in it. I do not want to use the code "JPanel panex = new JPanel();" 64 times, which what I mean by brute forcing. I want to be able to declare them a simpler way, then edit each one using an enhanced for loop. How would I do this?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    It's a 2 dimensional grid which lends itself very well to a 2 dimensional array, right? You could do all your initializing inside of a pair of nested for loops.

    But having said that, I have to second pbrockway's recommendations as they're all right on the money.
    Last edited by Fubarable; 10-23-2010 at 12:22 AM.

Similar Threads

  1. An "if" statement inside a "for" loop?
    By soccermiles in forum New To Java
    Replies: 18
    Last Post: 04-20-2010, 03:44 AM
  2. Enhanced for loop, the other "element"
    By Java Riser in forum New To Java
    Replies: 5
    Last Post: 02-23-2010, 11:47 PM
  3. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  4. Replies: 1
    Last Post: 10-20-2008, 07:35 AM
  5. Replies: 3
    Last Post: 09-26-2008, 06:05 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •