Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / May 2007

Tip: Looking for answers? Try searching our database.

Swing Battleship Game

Thread view: 
KDawg44 - 24 May 2007 00:16 GMT
Hi,

For a class I am taking I have to write a Battleship game.  I am using
Swing to program a GUI and I would like to make it so that there are
two panes holding the 11x11 grid for the spots on the computer's and
the player's boards.  I would like the position of the ships on the
players board to be marked somehow and I would like the user to be
able to click on the a spot on the other board to choose their shot
when it is there turn.  I would like to also prevent them from
choosing the same spot twice.

What should I use to create the boards in Swing?  What would be the
best way to try to accomplish something like this?  I would also like
to use images to show results, red X if its a miss, and an explosion
image if it is on.  And also an image of a ship for the positions on
the player's board.

My project does not require all this GUI work, it is more text based
using Java Sockets but I am interested in the GUI part and Swing.

Thanks very much in advance for any direction you provide and I look
forward to the challenge.  I am an average Java developer but not that
great when it comes to GUIs.

Thanks.
Andrew Thompson - 24 May 2007 00:33 GMT
...
>For a class I am taking I have to write a Battleship game.  I am using
>Swing to program a GUI and I would like to make it so that there are
[quoted text clipped - 6 lines]
>
>What should I use to create the boards in Swing?  

Layouts (same as you would use in AWT).

>..What would be the
>best way to try to accomplish something like this?  

The 11x11 grid can be made this easily..
<sscce>
import java.awt.*;
import javax.swing.*;

class LargeGrid {
  public static void main(String[] args) {
     JPanel p = new JPanel( new GridLayout(11,0) );
     for (int ii=0; ii<(11*11); ii++) {
        p.add( new JButton("" + (ii+1)) );
     }
     JOptionPane.showMessageDialog(null, p);
  }
}
</sscce>

Signature

Andrew Thompson
http://www.athompson.info/andrew/

visionset - 27 May 2007 00:42 GMT
> ..
>>For a class I am taking I have to write a Battleship game.  I am using
[quoted text clipped - 9 lines]
>
> Layouts (same as you would use in AWT).

I would say the opposite.
For a game board that is anything other than very simple and you know you
won't extend it in the future, then components are fine. But anything else
and pure painting on a Graphics object is the way to go.
This ensures that with a good OO design you have an easily extendable
application.
Otherwise you end up doing a lot of fighting against layout managers and
components.  But I guess we all do it once to really appreciate that...
(I've done it several times!)
Choose a null layout for the game board and separate your model and painting
logic. Use a clever set of classes that implement Paintable or something and
collect these together in your main JComponent subclass. In an overidden
paintComponent iterate over your paintables and delegate the painting to
them.  You will need a Z order concept implementing so your Paintables may
want to be Comparables also. Your paintables will probably mirror there
Model class counterpart. The Paintable knows about rendering the Model, and
the Model holds the data in nicely designed OO structure. You will likely
have a Board and Pieces classes in both Model and View tiers.

Signature

Mike W

Andrew Thompson - 27 May 2007 07:38 GMT
isionset wrote:
...
>> Layouts (same as you would use in AWT).
>
>I would say the opposite.
...

I would say..

>Otherwise you end up doing a lot of fighting against layout managers and
>components.  But I guess we all do it once to really appreciate that...
>(I've done it several times!)

..you need to invest more time in understanding layouts,
rather than settle for the far less adequate solution of ...

>Choose a null layout ...

..a null layout.

In this specific instance, a GridLayout is perfect for the job.

Can you show code that can make an 11x11 grid of
components, from a null layout, and be x-plat and robust
through different Java versions, screen sizes and default font
sizes?  More simply than the code I posted?
(And yes, that is more than simply a philosophical
question - if the answer is 'yes' - back it up with code).

Signature

Andrew Thompson
http://www.athompson.info/andrew/

visionset - 27 May 2007 14:42 GMT
> isionset wrote:
> ..
[quoted text clipped - 20 lines]
> Can you show code that can make an 11x11 grid of
> components, from a null layout,

No you use layout managers for components.
I said *not* to use components.
You simply draw on the Graphics object.

> and be x-plat and robust
> through different Java versions, screen sizes and default font
> sizes?  More simply than the code I posted?

It isn't just about simplicity, but it isn't much more complex and certainly
it is a more capable and extendable approach.
It is the way anything more than a trivial game board style app is done.
If you want screen size compatibility all you do is scale the graphics
object, a few lines of code for that, and a few more to scale mouse event
Points.

Your component approach is limited and not as xplatform as you think.  You
will have images on these components, and they are a fixed size, so then
you'll need to scale them anyway, all of a sudden you've got a mish mash of
components and painting.  It really does end up a real mess.
To be honest there are a host of other reasons I've forgotten, it was a
while ago.
Like I said a component approach is fine if it really is very simple and the
OP's intents may well be just that.  But real code develops and then it's a
bad choice.

> (And yes, that is more than simply a philosophical
> question - if the answer is 'yes' - back it up with code).

No.  I explained how it would be done and that is sufficient, if the OP
wants a detail clarifying I'm more than happy to oblige if possible.

Signature

Mike W

Andrew Thompson - 27 May 2007 15:55 GMT
>> ..
...
>> Can you show code that can make an 11x11 grid of
>> components, from a null layout,
>
>No you use layout managers for components.
>I said *not* to use components.
>You simply draw on the Graphics object.

And if the components use textual representations?
Did your null layout account for that?  If so, it is sounding
like it had a complexity justifying being wrapped in a
layout manager.

>> (And yes, that is more than simply a philosophical
>> question - if the answer is 'yes' - back it up with code).
>
>No.  I explained how it would be done and that is sufficient, if the OP
>wants a detail clarifying I'm more than happy to oblige if possible.

Puhh..  much as I thought.  All puff, no guff.
Same as the usual  'use a null layout' advocates.

Signature

Andrew Thompson
http://www.athompson.info/andrew/

Andrew Thompson - 27 May 2007 16:04 GMT
>>> ..
>...
[quoted text clipped - 6 lines]
>
>And if the components

*Or* the non-component 'visual representations'
of things representing the 121 grid ..things.

>...use textual representations?
...

Signature

Andrew Thompson
http://www.athompson.info/andrew/

visionset - 27 May 2007 16:56 GMT
>>> (And yes, that is more than simply a philosophical
>>> question - if the answer is 'yes' - back it up with code).
[quoted text clipped - 4 lines]
> Puhh..  much as I thought.  All puff, no guff.
> Same as the usual  'use a null layout' advocates.

I am *not* a 'use null layout advocate'.
I am totally pro layouts and need a very good reason not to use one.
It is just that this is such a case, if it is to become more elaborate in
future.
I don't think I've ever used a null layout in a container that has contained
components.
And I think that makes me pro layout managers.

Signature

Mike W

Brandon McCombs - 27 May 2007 17:55 GMT
>>> ..
> ..
[quoted text clipped - 16 lines]
> Puhh..  much as I thought.  All puff, no guff.
> Same as the usual  'use a null layout' advocates.

All puff, no guff?  Where is *your* code to backup your claim that
components are the way to go instead of drawing on the Graphics object?
Andrew Thompson - 27 May 2007 18:06 GMT
>...Where is *your* code ....

It was so short, someone of limited attention span might have
missed it.   But please *try* to pay attention, Brandon.  Your
posts are amusing, but they waste bandwidth.

Signature

Andrew Thompson
http://www.athompson.info/andrew/

Brandon McCombs - 30 May 2007 01:40 GMT
>> ...Where is *your* code ....
>
> It was so short, someone of limited attention span might have
> missed it.   But please *try* to pay attention, Brandon.  Your
> posts are amusing, but they waste bandwidth.

I'd prefer amusing posts over annoying. It's a good thing you cover the
annoying posts so I can concentrate on the amusing ones.
Lew - 30 May 2007 14:52 GMT
>>> ...Where is *your* code ....
>>
[quoted text clipped - 4 lines]
> I'd prefer amusing posts over annoying. It's a good thing you cover the
> annoying posts so I can concentrate on the amusing ones.

I think the point was that Andrew did provide an example, and was responding
to the direct challenge of "where is *your* code?"  Furthermore, he was making
the point that the answer is simple and brief, a relevant point to the
discussion.  I notice that Andrew was not the first to go /ad hominem/ in the
exchange.

Isn't there a name for the rhetorical device where you challenge someone, then
make them wrong for responding in kind?  Oh, yeah - flamebaiting, but I was
sure there was a more formal name for it.

Signature

Lew



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.