Simple Game Programming Advice


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Simple Game Programming Advice

  1. #1
    Join Date
    Aug 2004
    Posts
    4

    Question Simple Game Programming Advice

    Hey all... first time poster here...

    I am in the initial stages of creating an networked client/server version of Battleship (the classic grid based strategy(guessing?) game.

    In terms of creating the board itself and being able to drag and drop images of battleships on to the grid (& and have them "snap to the grid")... Does anyone have any advice?

    I was thinking that my first approach would be to create multiple layers bottom layer being an image of the sea or water, the next layer being a grid of drawlines() or drawRects. all of that being done in Graphics2D. though im not sure about layering things in Graphic2D.

    then I wasnt sure how the Drag & Drop works with Images over a non-Swing Object (in this case two Graphics2D components which have images draw on them)?

    Any help would be appreciated!
    "Best case scenario, We'll start a day late..."

  2. #2
    Join Date
    Feb 2004
    Posts
    808
    i'd be tempted to use a 2D array of jpanels; they can emit events when the mouse is over them, and you can use this to detect if a drag is taking place (maybe paint a red border on them?).. painting the shadow of the drag is a separate issue. upon drag release, the snapping would be achieved by assigning a panel a content (perhaps using a separate array of ints). the panel would display its relevant content (the same image that was dragged) and "snapping " occurrs
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  3. #3
    Join Date
    Aug 2004
    Posts
    4
    by a 2dimensional array of Jpanels... do you mean have a single image of water and slicing into (e.g)81 squares each with its own image?

    if so... 2 questions about that approach..
    - am i able to refer to each JPanel like follows:
    (JPanel)panels[i][j].setSize(20,20)
    ?
    - and do you mean take the image of the battleship (likely separated into 3,4 or 5 20px by 20px squares) and set the content for the appropriate JPanel to the square battleship section?

    Thanks in advance!
    "Best case scenario, We'll start a day late..."

  4. #4
    Join Date
    Feb 2004
    Posts
    808
    that's what i was thinking.. if you want water, then make a repeating pattern so you only have one small tile of water. your jpanels would need to be custom anyway, to paint hits, misses, ships and water..

    the size? well, its down to the layout manager. use a flexible manager like gridbag, set all panels to have min, pref and max sizes of 20x20, then arrange it so that the frame cannot distort them when it resizes..
    (put them in their own panel that is fixed in size, then add the fixed panel to the frame. game area grid ..
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  5. #5
    Join Date
    Aug 2004
    Posts
    4
    Can i just add an ImageIcon to each Jpanel without another component to add the image to?

    or were you suggesting using another kind of image object...? im only familiar with ImageIcon... willing to learn more tho

    right now what i have is a JPanel (mainPanel = new JPanel()) with GridLayout (new GridLayout(9,9) with 81 JPanels (e.g. panels = new JPanel[9][9]) that have extra space around the image... no matter the size set by mainPanel or each individual JPanel ...

    Also, CAN I draw images and lines on top of this 2d array of JPanels? i would like to draw Gridlines and of course the ships and hits and misses..

    thanks
    "Best case scenario, We'll start a day late..."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center