• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

GUI

 
Justin Fox
Ranch Hand
Posts: 802
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,

Justin
 
Joel McNary
Bartender
Posts: 1824
Eclipse IDE Java Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Justin Fox
Ranch Hand
Posts: 802
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 802
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well, i need to make lil rectangles (ships) and place them on the grid...

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


Justin
 
Robert Hill
Ranch Hand
Posts: 94
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joel McNary:
This would perhaps make more sense in the Swing forum...

Yeah, moving it there.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic