使用GridLayout创建战舰计划时遇到问题

时间:2013-12-11 06:43:14

标签: java swing layout-manager grid-layout

好的,所以我有一个任务,我们必须使用GUI在Java中创建一个低端战舰程序。我们必须创建一个JLabel个对象的数组,附加一个图像(对于水)。我使用GridLayout完成了此操作。

董事会很好,但是如果我尝试添加一艘船,(我现在只是想看看如何在船上添加一艘船),我最终要么没有出现任何东西,要么就像我现在的代码一样,我可以让船上的船出现,但它们往往相互重叠。 (他们越过边缘有问题,但我把那部分分类了。

所以我想知道是否有人不介意看看我的代码并告诉我,如果我这样做是错的。 GridLayout是可行的,还是应该使用其他方法?我还想知道是否有一种更有效的方式来操纵船只,尤其是它们不会重叠,因为我似乎无法找到任何真正有效的方法。 GridLayout这是不可能的吗?这是我的代码:

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 < width; x++) {
        for (int y = 0; y < 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 >= (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 >= (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 >= (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 <= (width - 1)) {
                    while(!boardGrid[ran4][ran5].getIcon().equals(waterImg)) {
                        return;
                    } while (boardGrid[ran4][ran5].getIcon().equals(waterImg)) {
                        boardGrid[ran4][ran5].setIcon(boatImgEndH1);
                    }
                }
                if (ran5 >= (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 <= (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 < width; x++) {
        for (int y = 0; y < length; y++) {
            if (ran2 == 2) {
                if (boardGrid[x][y].getIcon().equals(waterImg)) {
                    if (ran3 >= (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 >= (width - 3)) {
                        boardGrid[max + 1][ran3].setIcon(boatImgMidV);
                    } else {
                        boardGrid[ran3 + 1][ran3].setIcon(boatImgMidV);
                    }
                }
                if (boardGrid[x][y].getIcon().equals(waterImg)) {
                    if (ran3 >= (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 >= (width - 3)) {
                        boardGrid[ran3][max].setIcon(boatImgEndH1);
                    } else {
                        boardGrid[ran3][ran3].setIcon(boatImgEndH1);
                    }
                }
                if (boardGrid[x][y].getIcon().equals(waterImg)) {
                    if (ran3 >= (width - 3)) {
                        boardGrid[ran3][max + 1].setIcon(boatImgMidH);
                    } else {
                        boardGrid[ran3][ran3 + 1].setIcon(boatImgMidH);
                    }
                }
                if (boardGrid[x][y].getIcon().equals(waterImg)) {
                    if (ran3 >= (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);
}

}

非常感谢任何帮助或见解。 编辑我已更新到目前为止的代码,这允许我插入船只,但它们重叠或只是不显示,我已设置图像通过html运行,所以你可以运行它看看。

1 个答案:

答案 0 :(得分:1)

了解“model-view-controller”;它使开发变得更容易。您的 draw-the-board-and-play 类应与 manage-the-ships 类不同。这允许您单独测试它们。

您可以通过生成随机电路板并通过System.out.println打印来测试船舶管理(可以进行电路板初始化,命中管理等)以查看是否有效。

您将通过手工制作一些有趣的电路板来测试绘图,并测试它们是否按预期显示,并在用户点击时调用正确的船舶管理代码。最后,您可以将两个部分插在一起。

通过这种分离,您可以使用仅使用绘图板代码来提出此问题,这比您当前复制粘贴的两者的组合更具可读性