• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

GUI

 
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


Yeah, moving it there.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic