Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Swing / AWT / SWT and the fly likes GUI Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "GUI" Watch "GUI" New topic


Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
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.

If you can help please do,


You down with OOP? Yeah you know me!
Joel McNary

Joined: Aug 20, 2001
Posts: 1824

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
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...


Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
well, i need to make lil rectangles (ships) and place them on the grid...

I wouldn't think you could do that with jpanels.

Robert Hill
Ranch Hand

Joined: Feb 24, 2006
Posts: 94
Why do you think that?

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?
marc weber

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Joel McNary:
This would perhaps make more sense in the Swing forum...

Yeah, moving it there.

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
I agree. Here's the link:
subject: GUI
It's not a secret anymore!