Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #1675 > unrolled thread

Swing Battleship Game

Started by"KDawg44" <kdawg44@THRWHITE.remove-dii-this>
First post2011-04-27 15:34 +0000
Last post2011-04-27 15:34 +0000
Articles 9 — 4 participants

Back to article view | Back to comp.lang.java.gui


Contents

  Swing Battleship Game "KDawg44" <kdawg44@THRWHITE.remove-dii-this> - 2011-04-27 15:34 +0000
    Re: Swing Battleship Game "Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this> - 2011-04-27 15:34 +0000
      Re: Swing Battleship Game "visionset" <visionset@THRWHITE.remove-dii-this> - 2011-04-27 15:34 +0000
        Re: Swing Battleship Game "Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this> - 2011-04-27 15:34 +0000
          Re: Swing Battleship Game "visionset" <visionset@THRWHITE.remove-dii-this> - 2011-04-27 15:34 +0000
            Re: Swing Battleship Game "Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this> - 2011-04-27 15:34 +0000
              Re: Swing Battleship Game "Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this> - 2011-04-27 15:34 +0000
              Re: Swing Battleship Game "visionset" <visionset@THRWHITE.remove-dii-this> - 2011-04-27 15:34 +0000
              Re: Swing Battleship Game "Brandon McCombs" <brandon.mccombs@THRWHITE.remove-dii-this> - 2011-04-27 15:34 +0000

#1675 — Swing Battleship Game

From"KDawg44" <kdawg44@THRWHITE.remove-dii-this>
Date2011-04-27 15:34 +0000
SubjectSwing Battleship Game
Message-ID<1179962208.896054.252700@w5g2000hsg.googlegroups.com>
  To: comp.lang.java.gui
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.

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [next] | [standalone]


#1676

From"Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this>
Date2011-04-27 15:34 +0000
Message-ID<72a0cfdde75b8@uwe>
In reply to#1675
  To: comp.lang.java.gui
KDawg44 wrote:
..
>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?  

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>

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

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-gui/200705/1

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1698

From"visionset" <visionset@THRWHITE.remove-dii-this>
Date2011-04-27 15:34 +0000
Message-ID<sB36i.5281$I55.2591@newsfe2-gui.ntli.net>
In reply to#1676
  To: comp.lang.java.gui

"Andrew Thompson" <u32984@uwe> wrote in message news:72a0cfdde75b8@uwe...
> KDawg44 wrote:
> ..
>>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?
>
> 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.

-- 
Mike W

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1699

From"Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this>
Date2011-04-27 15:34 +0000
Message-ID<72ca3e01634d2@uwe>
In reply to#1698
  To: comp.lang.java.gui
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).

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

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-gui/200705/1

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1700

From"visionset" <visionset@THRWHITE.remove-dii-this>
Date2011-04-27 15:34 +0000
Message-ID<RUf6i.780$E9.693@newsfe6-gui.ntli.net>
In reply to#1699
  To: comp.lang.java.gui

"Andrew Thompson" <u32984@uwe> wrote in message news:72ca3e01634d2@uwe...
> 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,

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.

-- 
Mike W

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1701

From"Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this>
Date2011-04-27 15:34 +0000
Message-ID<72ce937eef69f@uwe>
In reply to#1700
  To: comp.lang.java.gui
visionset wrote:
>> visionset wrote:
>> ..
..
>> 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.

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

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-gui/200705/1

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1702

From"Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this>
Date2011-04-27 15:34 +0000
Message-ID<72cea86004149@uwe>
In reply to#1701
  To: comp.lang.java.gui
Andrew Thompson wrote:
>>> ..
>...
>>> 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 

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

>...use textual representations?
..

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

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-gui/200705/1

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1703

From"visionset" <visionset@THRWHITE.remove-dii-this>
Date2011-04-27 15:34 +0000
Message-ID<0Th6i.821$E9.405@newsfe6-gui.ntli.net>
In reply to#1701
  To: comp.lang.java.gui

"Andrew Thompson" <u32984@uwe> wrote in message news:72ce937eef69f@uwe...

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

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.

-- 
Mike W

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1707

From"Brandon McCombs" <brandon.mccombs@THRWHITE.remove-dii-this>
Date2011-04-27 15:34 +0000
Message-ID<4659b82a$0$2806$4c368faf@roadrunner.com>
In reply to#1701
  To: comp.lang.java.gui
Andrew Thompson wrote:
> visionset wrote:
>>> visionset wrote:
>>> ..
> ..
>>> 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.
> 

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?

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.gui


csiph-web