Hi everyone,

For a group assignment we have to develop a property trading game similar to monopoly. At the moment we have the GUI developed as well as a basic idea of how to go about doing it but was wondering if anyone could help a bit!:S

Is it possible to get an index for a JPanel object? What we want is for all 40 squares to be stored in order in an array list, then when the game starts it should read the values and assign them to the JPanels on the board e.g. JPanel 0 gets property 0 details (Pass Go), JPanel 1 gets property 1 details and so on... is this even possible or are we going the wrong way about it?

The only other way we can think of is hard coding the values into each square's labels and so on :( any help is much appreciated - not looking for code or anything, just a few pointers thats all!

Recommended Answers

All 11 Replies

You can certainly store a JPanel in an ArrayList - any object reference can be stored in an ArrayList. I don't know what you mean by "getting an index for a JPanel" though.
If you have 40 JPanels and you need to retain references to them, some sort of collection like a List or a Map is a good way to manage them. If it's a static collection, and you want to get at them by index number, you could consider using an array. ArrayLists have lots of neat features, but if all you want is to store 40 objects and get at them when you need them, maybe an array is all you need.

Yeah I had been trying to do that, I'm using Netbeans 6.9 at the minute and the GUI editor - all the panels were added through drag and drop so its pretty hard now to try and add them into an arraylist, just keeps coming up with exceptions :S

What I meant by get the index was is there any way of getting a reference to a JPanel similar to an array, like if you added 10 JPanels to a JFrame could you call a JPanel like JPanel[0] for the first one and so on

You mean something like

JPanel[] panels = new JPanel[10];

If so, try that. :)

You can also ask a Container about its components - take a look at the Swing API, starting with JFrame. There's going to be a method, I think maybe "getChildren()" somewhere in there that'll do what you want.
Oh, hell, (rummage rummage) here, I think this is maybe what you're looking for. I'd prefer to maintain my own array, so I know what's what and what's where, because I put it there, but you can try this as well.
I don't know anything about NetBeans, can't help you there, but I generally sub out that sort of work to a method. Depends on how your code is designed - if your squares are objects (they might be) then each Square should know how to generate itself. If not, you'll have a makeSquare method that takes the parameters and returns a JPanel(), which you can put into your array or arraylist and add to your frame. There's any number of ways you might construct this - I could see you doing this by loading game board data from a properties file or creating an enumeration with all the information, for example - but any sensible way will be compatible with something like

for each square (generateAJPanelAndAddItToTheFrameAndArray())

so you can keep your top-level code looking clean. The details, of course, will vary with your design.

Yeah I've been trying it but still getting no where :S

I have the JPanels as the squares on my board, but I also have a class called GUIPropertySquare that contains the code for shading in the square, setting the houses etc. so the code fir the squares is really

private ArrayList<JPanel> squareList   = new ArrayList<JPanel>();

sqSquare03 = new GUIPropertySquare(SquarePosition.TOP);
// Add square to the Array List
squareList.add(sqSquare03);

but now I can't access the GUIPropertySquare methods to set property colour/no of houses - if I change the ArrayList to

ArrayList<GUIPropertySquare> squareList   = new ArrayList<GUIPropertySquare>

it won't compile without errors :(

If anyone is willing to help (in other words anyone who can understand what I'm rambling on about!) I can supply the source code if needed!

Okay, I see a few things here that maybe I can help with.

i. If your GUIPropertySquare extends JPanel, which it must be if your code segment there runs, then you can cast it back to GUIPropertySquare when you access it. Like so:

GUIPropertySquare gps = (GUIPropertySquare)squareList.get(1);

ii. But why doesn't your code compile when you try to make and ArrayList of your GUIPropertySquare class? The line that you cite has one trivial error, lacking the () at the end - if that's the problem, fix it and try again. If that's not the problem, what are the compiler error messages? It would be much better to have your ArrayList composed of the right type - "avoid casting" is a good design heuristic - but I can't immediately imagine why this wouldn't work.

iii. Making an object to extend JPanel and add the functionality you need is definitely one of the good ways of mechanising construction of these JPanels that I spoke of before.


Let's hold off on the source code for the time being - it sounds like there's a lot of it, and I don't really want to dive into it unless and until it's needed. If you can summarize your design, though, that might help both of us. What objects do you have, and what do they represent, and how are you putting them together?

that was actually a great help there, starting to get somewhere now thanks!!!

Unfortunately as this is a group project, everyone is doing different things - I'm mostly GUI and stuff so I really need the person whos doing the property classes and whatnot to finish their work for me to progress, so for the time being I'm making my own temp classes to test mine out... the joys of group work :(

While you're hung up, you might want to read some stuff on development methodology.
Fred Brooks is great - "The Mythical Man Month" is a classic. Yourdon's "Death March" is a pretty good cautionary tale. Eric Raymond's stuff is also well worth reading. "The Art of Unix Programming" is essential reading, in my book, even if you're not working on a Unix or Gnu/Linux platform, and "The Cathedral and the Bazaar" is an excellent reconsideration of the issues Brooks addresses in Mythical Man Month, and what open source has to say about it.

Or, possibly more productive, you might want to develop interfaces for the other classes, so you can get some work done while they're diddling around.

If you know that a property class interfaces a Property interface, and that interface has certain methods that you need, you can program to it without the classes actually existing. In fact, you can make dummy classes that implement the interface and return garbage values, which would allow you to get your code working. This requires that you communicate with the other developers, of course, but it limits your communication to something you can handle: you tell them what you need, and you agree on how you'll get that from them.

(later on, on solo projects, you can use this same technique to make it easier to develop one piece of a project at a time, which is useful.)

you can use the Arraylist in the util class or you can create an array of Jpanel

e.g JPanel [] panel = new JPanel[x];
for(int i =0 ; i < x;i++){
panel = new JPanel();
}
.
.
.
e.t.c

ii. But why doesn't your code compile when you try to make and ArrayList of your GUIPropertySquare class?

Indeed. What exact errors are you getting?

Hi again,

Can't even remember the exact errors anymore (mostly about the wrong types and so on) though I seem to have gotten it working now thanks!

One more question, just wondering if you can do this in Java (I done it before in C#)

But we have a class Square to represent the squares on the board. From the Square class there are the following classes: propertySquare, railroadSquare, cardSquare, specialSquare, all inherited (or extended) from the Square class.
So in theory, if I create an array of type Square, I should be able to add the propertySquare, railroadSquare etc. to the array since they are all inherited from the Square class?

Not just in theory. That's inheritance. If you make sure that any interesting methods are required in Square and overridden in each child class, then you can do something like

Square[] squares = getArrayOfSquaresFromSomewhere();
for each (Square s:squares)
{
s.doSomething();
}

where doSomething() is some method specified in Square and overridden in Square's subclasses. This would tell each of your Squares to execute some method, which can be different for each type of Square.
This allows you to avoid type checking and csating when retrieving Squares from such an array.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.