Nick Alexander
Nick Alexander

Reputation: 1653

Array of JLabel vs. Graphics2D Painting

I have to create a connect 5 game with a GUI for my CS course final project. I worked with Graphics2D for the last project - a Maze - and working with Graphics2D was a nightmare. At most, the connect5 board will be 20x20, which would be an Area of 400. I was wondering what the performance implications of creating an Array of 400 JLabels to handle the GUI, since it will be easy to determine a mouse click within boundaries, get the array induces, and changing the color of space along with other similar operations whereas the same operations would be much more difficult with Graphics2D. So, my overall question is: would creating an array of that many JLabels be undesirable? If so, what other alternatives might I have? Thanks everyone!

Upvotes: 2

Views: 140

Answers (1)

NESPowerGlove
NESPowerGlove

Reputation: 5496

I think the JLabel approach would be undesirable, I think one component with custom painting would be more desirable, because it seems a bit easier.

If you have your classes in that abstract format not tied to the UI or anything (so playable even on the command line where you could output in a text a representation of the board), then it should be simple to loop through the spaces representing the board and make the small set of drawing calls to draw each slot (empty or not).

Seems better to if you wanted to introduce animations like pieces falling down and then bumping up for a while when they hit the piece below it.

Upvotes: 1

Related Questions