ok here is my code... it's for a battle ship game...(just the GUI though).
_________________________________ Here is the JFrame... _________________________________
ok now in the Driver class i wrote a coulpe of for loops to draw each grid board...
the only problem I can see, that will be a pain in the future, is making it to where the mouse interacts with each individual square on the grids...
here is the for loop code separate..
im just not sure, do I need to make a "Grid Board" class? because the only way to get the squares to do anything right now, I would have to know every givin point in each square..
that's a good total of about.. oh... 40,000 points... for each grid table.
This would perhaps make more sense in the Swing forum; but I'll attempt to answer it here.
I only briefly read through your code, concentrating mainly on your question. Instead of creating your own rectangles, why not use JPanels (or a custom subclass thereof)? You can set the background color of each JPanel, register a mouse listener, and then based on which JPanel the mouse was in you could do your logic. I believe that there's a method "getComponentAt(int x, int y)" in Component that you can use to determine which JPanel the mouse is in.
Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Justin Fox
Ranch Hand
Joined: Jan 24, 2006
Posts: 802
posted
0
awesome dude!!
I appreciate it a lot, I was trying to make a Square class and pass its
x,y,w,l everytime i made one...
but your idea seems easier, and prolly a lot less code...
thanks,
Justin
Justin Fox
Ranch Hand
Joined: Jan 24, 2006
Posts: 802
posted
0
well, i need to make lil rectangles (ships) and place them on the grid...
Can you not create a JPanel, fit it to any grid size and place whatever elements on the JPanel, including a section of a ship?
Or how about dynamically creating the JPanel with the whole ship; marking off n equal and distinct area of the JPanel, where n is the number of hits the ship can take; then placing then horizontally or vertically, and then use smaller JPanels to mark unoccupied areas?