title: Having problems using GridLayout to create a battleship program
body: <p>OK, so I have an assignment where we have to create a low end battleship program in Java using the GUI. We have to create an array of <code>JLabel</code> objects, with an image attached, (for the water). I have done this using <code>GridLayout</code>. </p>
  
  <p>The board comes up fine, but if I try and add a ship, (I am at this point just trying to see how to add a ship onto the board), I end up either not having anything show up, or, as in the case of the code I have at the moment, I can get ships on the board to show up, but they tend to overlap each other. (there was a problem with them going over the edge, but I sorted that part out.</p>
  
  <p>So I was wondering if someone wouldn't mind having a look at my code and tell me if I am going about this all wrong. Is <code>GridLayout</code> the way to go, or should I be utilizing some other method? I'd also like to know if there is a more efficent way of going about manipulating the ships, especially so they don't overlap, beacuse I can't seem to find any real effective way. Is this just not possible with <code>GridLayout</code>? Here's my code:</p>
  
  <pre><code>package battleship;
  
  import java.awt.Container;
  import java.awt.GridLayout;
  import java.awt.image.BufferedImage;
  import java.io.IOException;
  import java.net.MalformedURLException;
  import java.net.URL;
  import java.util.ArrayList;
  import java.util.Random;
  import javax.imageio.ImageIO;
  import javax.swing.Icon;
  import javax.swing.ImageIcon;
  import javax.swing.JFrame;
  import javax.swing.JLabel;
  import javax.swing.JPanel;
  import javax.swing.SpringLayout;
  
  public class Battleship extends JFrame {
  
  private ImageIcon waterImg, shipCovered, boatImgEndH1, boatImgEndH2, boatImgMidH, boatImgFrntH2, boatImgFrntH1,
          boatImgEndV1, boatImgEndV2, boatImgMidV, boatImgFrntV2, boatImgFrntV1;
  private Container content;
  private JPanel boardFrame;
  private JLabel[][] boardGrid;
  public JLabel label, label2;
  private int width, length;
  
  public Battleship() {
  }
  
  public Battleship(int width, int length) {
      SpringLayout layout = new SpringLayout();
      this.width = width;
      this.length = length;
      this.setVisible(true);
      this.setSize(640, 540);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      content = this.getContentPane();
      content.setLayout(layout);
  
      try {
                   BufferedImage water = ImageIO.read(new URL("http://i.stack.imgur.com/gJmeJ.png"));
                   BufferedImage ship = ImageIO.read(new URL("http://i.stack.imgur.com/T5uTa.png"));
  
                   waterImg = new ImageIcon(new ImageIcon(water).getImage());
                   boatImgEndH1 = new ImageIcon(new ImageIcon(ship).getImage());
                   boatImgEndH2 = new ImageIcon(new ImageIcon(ship).getImage());
                   boatImgMidH = new ImageIcon(new ImageIcon(ship).getImage());
                   boatImgFrntH2 = new ImageIcon(new ImageIcon(ship).getImage());
                   boatImgFrntH1 = new ImageIcon(new ImageIcon(ship).getImage());
                   boatImgEndV2 = new ImageIcon(new ImageIcon(ship).getImage());
                   boatImgEndV1 = new ImageIcon(new ImageIcon(ship).getImage());
                   boatImgMidV = new ImageIcon(new ImageIcon(ship).getImage());
                   boatImgFrntV2 = new ImageIcon(new ImageIcon(ship).getImage());
                   boatImgFrntV1 = new ImageIcon(new ImageIcon(ship).getImage());
                  } catch (MalformedURLException ex) {
                      throw new Error("One of the cameras you are trying to connect to doesn't exist!");
                  } catch (IOException ex) {
                      throw new Error("CCTV couldn't connect to the required camera feeds!");
                  }
  
      boardFrame = new JPanel(new GridLayout(width, length, 1, 1));
  
      content.add(boardFrame);
      layout.putConstraint(SpringLayout.WEST, boardFrame, 5, SpringLayout.WEST, content);
      layout.putConstraint(SpringLayout.NORTH, boardFrame, 5, SpringLayout.NORTH, content);
  
      boardGrid = new JLabel[width][length];
  
      for (int x = 0; x &lt; width; x++) {
          for (int y = 0; y &lt; length; y++) {
              boardFrame.add(boardGrid[x][y] = new JLabel(waterImg, JLabel.CENTER));
          }
      }
      twoShip();
      threeShip();
  
      this.setVisible(true);
      boardFrame.setVisible(true);
  
  }
  
  public int random(int n) {
  
  Random randNumGenerator = new Random();
      int random= randNumGenerator.nextInt(n) + 1;
  return random;
  }
  
  private void twoShip() {
      int ran5 = random(width - 1);
      int ran4 = random(width - 2);
      int ran2 = random(2);
  
      int max = (ran5 - 1);
      while (boardGrid[ran5][ran5].getIcon().equals(waterImg)) {
              if (ran2 == 2) {
                  if (ran5 &gt;= (width - 1)) {
                      while (!boardGrid[max][ran4].getIcon().equals(waterImg)) {
                          return;
                      } while (boardGrid[max][ran4].getIcon().equals(waterImg)) {
                          boardGrid[max][ran4].setIcon(boatImgEndV1);
                      }
                  } else {
                      while(!boardGrid[ran5][ran4].getIcon().equals(waterImg)) {
                          return;
                      } while (boardGrid[ran5][ran4].getIcon().equals(waterImg)) {
                          boardGrid[ran5][ran4].setIcon(boatImgEndV1);
                      }
                  }
                  if (ran5 &gt;= (width - 1)) {
                      while (!boardGrid[max + 1][ran4].getIcon().equals(waterImg)) {
                          return;
                      } while (boardGrid[max + 1][ran4].getIcon().equals(waterImg)) {
                          boardGrid[max + 1][ran4].setIcon(boatImgFrntV1);
                      }
                  } else {
                      while(!boardGrid[ran5 + 1][ran4].getIcon().equals(waterImg)) {
                          return;
                      } while (boardGrid[ran5 + 1][ran4].getIcon().equals(waterImg)) {
                          boardGrid[ran5 + 1][ran4].setIcon(boatImgFrntV1);
                      }
                  }
              } else {
                  if (ran5 &gt;= (width - 1)) {
                      while(!boardGrid[ran4][max].getIcon().equals(waterImg)) {
                          return;
                      } while (boardGrid[ran4][max].getIcon().equals(waterImg)) {
                          boardGrid[ran4][max].setIcon(boatImgEndH1);
                      }
                  } else if (ran5 &lt;= (width - 1)) {
                      while(!boardGrid[ran4][ran5].getIcon().equals(waterImg)) {
                          return;
                      } while (boardGrid[ran4][ran5].getIcon().equals(waterImg)) {
                          boardGrid[ran4][ran5].setIcon(boatImgEndH1);
                      }
                  }
                  if (ran5 &gt;= (width - 1)) {
                      while (!boardGrid[ran4][max + 1].getIcon().equals(waterImg)) {
                          return;
                      } while (boardGrid[ran4][max + 1].getIcon().equals(waterImg)) {
                          boardGrid[ran4][max + 1].setIcon(boatImgFrntH1);
                      }
                  } else if (ran5 &lt;= (width - 1)) {
                      while (!boardGrid[ran4][ran5 + 1].getIcon().equals(waterImg)) {
                          return;
                      } while (boardGrid[ran4][ran5 + 1].getIcon().equals(waterImg)) {
                          boardGrid[ran4][ran5 + 1].setIcon(boatImgFrntH1);
                      }
                  }
              }
      }
  }
  
  private void threeShip() {
      int ran3 = random(width - 1);
      int ran2 = random(2);
  
      int max = (ran3 - 2);
      for (int x = 0; x &lt; width; x++) {
          for (int y = 0; y &lt; length; y++) {
              if (ran2 == 2) {
                  if (boardGrid[x][y].getIcon().equals(waterImg)) {
                      if (ran3 &gt;= (width - 3)) {
                          boardGrid[max][ran3].setIcon(boatImgEndV1);
                          boardGrid[max][ran3].validate();
                      } else {
                          boardGrid[ran3][ran3].setIcon(boatImgEndV1);
                      }
                  }
                  if (boardGrid[x][y].getIcon().equals(waterImg)) {
                      if (ran3 &gt;= (width - 3)) {
                          boardGrid[max + 1][ran3].setIcon(boatImgMidV);
                      } else {
                          boardGrid[ran3 + 1][ran3].setIcon(boatImgMidV);
                      }
                  }
                  if (boardGrid[x][y].getIcon().equals(waterImg)) {
                      if (ran3 &gt;= (width - 3)) {
                          boardGrid[max + 2][ran3].setIcon(boatImgFrntV1);
                      } else {
                          boardGrid[ran3 + 2][ran3].setIcon(boatImgFrntV1);
                      }
                  }
              } else {
                  if (boardGrid[x][y].getIcon().equals(waterImg)) {
                      if (ran3 &gt;= (width - 3)) {
                          boardGrid[ran3][max].setIcon(boatImgEndH1);
                      } else {
                          boardGrid[ran3][ran3].setIcon(boatImgEndH1);
                      }
                  }
                  if (boardGrid[x][y].getIcon().equals(waterImg)) {
                      if (ran3 &gt;= (width - 3)) {
                          boardGrid[ran3][max + 1].setIcon(boatImgMidH);
                      } else {
                          boardGrid[ran3][ran3 + 1].setIcon(boatImgMidH);
                      }
                  }
                  if (boardGrid[x][y].getIcon().equals(waterImg)) {
                      if (ran3 &gt;= (width - 3)) {
                          boardGrid[ran3][max + 2].setIcon(boatImgFrntH1);
                      } else {
                          boardGrid[ran3][ran3 + 2].setIcon(boatImgFrntH1);
                      }
                  }
              }
          }
      }
  }
  
  public static void main(String[] args) {
      new Battleship(17, 17);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Any help or insights would be greatly appreciated. <em>EDIT</em> I have updated to code I have so far, which is allowing me to insert ships, but they either overlap or just don't show up, and I have setup the images to run via html, so you can run it and see.</p>