...

View Full Version : Displaying an array in a Jpanel



jdruwe
02-29-2012, 10:19 AM
Hello

I am developing a game called "Angry Tank", 2 tanks on a landscape trying to destroy eachother as fast as possible. At the moment its only playable in the console. I am adding a GUI but i have no idea how to display my landscape (stored in an array). A friend of mine advised me to use a Jlabel for each coordinate but that would require 2600 Jlabels (+ adding them on a Jpanel) , I think there more efficient ways. Pls some suggestions.

Thanks in advance!

Fou-Lu
02-29-2012, 02:47 PM
Why not use a JTable instead? It renders like that of a grid which sounds to be what you are looking to do, and will let you modify what you need at any time. Can be listened and reacted too as well.
That assumes a matrix style layout is what you are looking for.

jdruwe
03-01-2012, 10:15 AM
Why not use a JTable instead? It renders like that of a grid which sounds to be what you are looking to do, and will let you modify what you need at any time. Can be listened and reacted too as well.
That assumes a matrix style layout is what you are looking for.

Ok i will try this on another forum they suggested this "Look at using a JPanel as an area to draw on. Override the paintComponent method and draw your shapes there."

Fou-Lu
03-01-2012, 01:41 PM
Ok i will try this on another forum they suggested this "Look at using a JPanel as an area to draw on. Override the paintComponent method and draw your shapes there."

Yes, a draw would also be fine, but it just comes down to what you need.
If the game style is like that of say minesweeper, or tic-tac-toe then a jtable or matrix of buttons, panels, etc, make the most sense. If its moving something from 0,0 to 10,2 then drawing may make more sense.
End result you can do either with both approaches. I assumed that a matrix of components made more sense since I thought that the Array is a requirement for landscape. If its just to track coordinate, then I'd say that drawing will be easier.

jdruwe
03-01-2012, 03:53 PM
Yes, a draw would also be fine, but it just comes down to what you need.
If the game style is like that of say minesweeper, or tic-tac-toe then a jtable or matrix of buttons, panels, etc, make the most sense. If its moving something from 0,0 to 10,2 then drawing may make more sense.
End result you can do either with both approaches. I assumed that a matrix of components made more sense since I thought that the Array is a requirement for landscape. If its just to track coordinate, then I'd say that drawing will be easier.

The array contains elements (landscape element "x", air element " ", tank element "A" or "B" and a shot element "")
I need exact coordinates they will never be decimal ^^ cause i round my shot results, so i guess a jtabel makes more sense. But if i need a jlabel or something else for each coordinate i need 2600 of them xD (landscape: 26*100) and they advised me not to do for a better maintenance



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum