gpt4 book ai didi

java - 使用GridLayout创建战舰程序时遇到问题

转载 作者:行者123 更新时间:2023-12-01 13:43:31 26 4
gpt4 key购买 nike

好的,我有一个任务,我们必须使用 GUI 用 Ja​​va 创建一个低端战舰程序。我们必须创建一个 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("/image/gJmeJ.png"));
BufferedImage ship = ImageIO.read(new URL("/image/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运行,所以你可以运行它并查看。

最佳答案

阅读“model-view-controller”;它使开发变得更加容易。您的绘制棋盘和游戏类(class)应该与您的管理船舶类(class)不同。这允许您单独测试它们。

您将通过生成随机板并通过 System.out.println 打印它们来测试船舶管理(这将进行板初始化、命中管理等) > 看看事情是否有效。

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

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

关于java - 使用GridLayout创建战舰程序时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20512293/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com