gpt4 book ai didi

java 盒子面具不工作

转载 作者:行者123 更新时间:2023-12-01 06:48:08 25 4
gpt4 key购买 nike

这里的想法是创建一个盒子网格。黑色网格下面是另一个由多种颜色的盒子组成的网格。当您单击一个框时,它的 mask 会消失,显示下面的彩色框。然后,如果颜色与万岁相匹配,则单击第二个框,如果不匹配,则游戏继续。这是代码。

import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class GuessingGame extends Applet{
/**
*
*/
private static final long serialVersionUID = 1L;
private final int START_X = 20;
private final int START_Y = 40;
private final int ROWS = 4;
private final int COLS = 4;
private final int BOX_WIDTH = 20;
private final int BOX_HEIGHT = 20;
//this is used to keep track of boxes that have been matched.
private boolean matchedBoxes[][];
//this is used to keep track of two boxes that have been clicked.
private MaskableBox chosenBoxes[];
private MaskableBox boxes[][];
private Color boxColors[][];
private Button resetButton;


public void init() {
boxes = new MaskableBox[ROWS][COLS];
boxColors = new Color[ROWS][COLS];
resetButton = new Button("Reset Colors");
resetButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
randomizeColors();
buildBoxes();
repaint();
}
});
add(resetButton);
//separate building colors so we can add a button later
//to re-randomize them.
randomizeColors();
buildBoxes();
}

public void paint(Graphics g) {
for (int row =0; row < boxes.length; row ++) {
for (int col = 0; col < boxes[row].length; col++) {
if(boxes[row][col].isClicked()) {
//boxes[row][col].setMaskColor(Color.black);
//boxes[row][col].setMask(!boxes[row][col].isMask());
//boxes[row][col].setClicked(false);
//}
if (!matchedBoxes[row][col]) {
gameLogic(boxes[row][col]);
//boxes[row][col].draw(g);
}
}
}
}
//loop through the boxes and draw them.
for (int row = 0; row < boxes.length; row++) {
for (int col = 0; col < boxes[row].length; col++) {
boxes[row][col].draw(g);

}
}
}


public void gameLogic(MaskableBox box) {
if ((chosenBoxes[0] != null)&&(chosenBoxes[1] != null)) {
if(chosenBoxes[0].getBackColor() == chosenBoxes[1].getBackColor()) {
for (int i=0; 0 < chosenBoxes.length; i++ ) {
for(int row = 0; row < boxes.length; row++) {
for(int col = 0; col < boxes[row].length; col++) {
if( boxes[row][col] == chosenBoxes[i] ) {
System.out.println("boxes [row][col] == chosenBoxes[] at index: " + i );
matchedBoxes[row][col] = true;
break;
}
}
}
}
}else {
chosenBoxes[0].setMask(true);
chosenBoxes[1].setMask(true);
}
chosenBoxes = new MaskableBox[2];

}else {
if (chosenBoxes[0] == null) {
chosenBoxes[0] = box;
chosenBoxes[0].setMask(false);
return;
}else{
if (chosenBoxes[1] == null) {
chosenBoxes[1] = box;
chosenBoxes[1].setMask(false);
}
}
}
}


private void removeMouseListeners() {
for(int row = 0; row < boxes.length; row ++) {
for(int col = 0; col < boxes[row].length; col++) {
removeMouseListener(boxes[row][col]);
}
}
}

private void buildBoxes() {
// need to clear any chosen boxes when building new array.
chosenBoxes = new MaskableBox[2];
// create a new matchedBoxes array
matchedBoxes = new boolean [ROWS][COLS];
removeMouseListeners();
for(int row = 0; row < boxes.length; row++) {
for(int col = 0; col < boxes[row].length; col++) {
boxes[row][col] =
new MaskableBox(START_X + col * BOX_WIDTH,
START_Y + row * BOX_HEIGHT,
BOX_WIDTH,
BOX_HEIGHT,
Color.gray,
boxColors[row][col],
true,
true,
this);
addMouseListener(boxes[row][col]);
}
}
}





private void randomizeColors() {
int[] chosenColors = {0,0,0,0,0,0,0,0};
Color[] availableColors = {Color.red, Color.blue, Color.green,
Color.yellow, Color.cyan, Color.magenta, Color.pink, Color.orange };
for(int row = 0; row < boxes.length; row++) {
for (int col = 0; col < boxes[row].length; col++) {
for (;;) {
int rnd = (int) (Math.random() * 8);
if (chosenColors[rnd]< 2) {
chosenColors[rnd]++;
boxColors[row][col] = availableColors[rnd];
break;
}
}
}
}
}
}

这是包含 maskablebox 的第二批代码

import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;

public class MaskableBox extends ClickableBox {
private boolean mask;
private Color maskColor;
Container parent;

public MaskableBox(int x, int y, int width, int height, Color borderColor,
Color backColor, boolean drawBorder, boolean mask, Container parent ) {
super(x, y, width, height, borderColor, backColor, drawBorder, parent);
this.parent = parent;
this.mask = mask;
}

public void draw(Graphics g) {
if(mask=false) {
super.draw(g);
// setOldColor(g.getColor());
// g.setColor(maskColor);
// g.fillRect(getX(),getY(),getWidth(), getHeight());
// if(isDrawBorder()) {
// g.setColor(getBorderColor());
// g.drawRect(getX(),getY(),getWidth(),getHeight());
// }
// g.setColor(getOldColor());
}else {
if(mask=true) {
//super.draw(g);
setOldColor(g.getColor());
g.setColor(maskColor);
g.fillRect(getX(),getY(),getWidth(), getHeight());
if(isDrawBorder()) {
g.setColor(getBorderColor());
g.drawRect(getX(),getY(),getWidth(),getHeight());
}
g.setColor(getOldColor());
}
}
}


public boolean isMask() {
return mask;
}

public void setMask(boolean mask) {
this.mask = mask;
}

public Color getMaskColor() {
return maskColor;
}

public void setMaskColor(Color maskColor) {
this.maskColor = maskColor;
}
}

我现在收到这些错误消息。

Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
at GuessingGame.gameLogic(GuessingGame.java:74)
at GuessingGame.paint(GuessingGame.java:55)
at java.awt.Container.update(Container.java:1801)
at sun.awt.RepaintArea.updateComponent(RepaintArea.java:239)
at sun.awt.RepaintArea.paint(RepaintArea.java:216)
at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:306)
at java.awt.Component.dispatchEventImpl(Component.java:4706)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

最佳答案

看,您在那里看到的错误消息被称为:“堆栈跟踪”

它包含有关错误原因的非常有用的信息:

Exception in thread "AWT-EventQueue-1" java.lang.ArrayIndexOutOfBoundsException: 2
at GuessingGame.gameLogic(GuessingGame.java:77) // This is where the error happened
at GuessingGame.paint(GuessingGame.java:55) // This is your code
at java.awt.Container.update(Container.java:1801) // not your code
at sun.awt.RepaintArea.updateComponent(RepaintArea.java:239) // not your code

如果你看到它说:GuessingGame.java:77,这意味着错误发生在文件GuessingGame.java的第77行。其他文件不在您的源代码( Container.java 和 Repaint.java )因此,它们不是问题发生的地方。

在 GuessingGame.java 的第 77 行,您尝试访问超出数组边界索引(因此 ArrayIndexOutOfBoundException )此外,数字2 它是您尝试使用的索引。

有了这些信息,您可以查看源代码并看到第 77 行(至少从发布的代码来看)是:

 if( boxes[row][col] == chosenBoxes[i] ) {

因此,boxes 不是问题(它的大小为 4,它被声明为 boxes = new MaskableBox[ROWS][COLS]; 并且 ROWS 和 COLS 都是声明为 4 )。所以负责人必须是:chosenBoxes

它被初始化为:

chosenBoxes = new MaskableBox[2];

大小2,这意味着唯一有效的索引是01。当您尝试使用 index=2 时,会引发异常。

现在记住这些信息,您可以搜索可能导致它的代码。

正如其他答案指出的那样,原因是

for (int i=0; 0 <= chosenBoxes.length; ++i ) {

上面只有 3 行!它显示,“当零小于或等于 2...” 它将始终返回 true,因为 0 将始终 < 小于 2。

因此,您只需修复该部分并重新测试即可。

希望这个解释能够帮助您以后解决此类问题。学会阅读堆栈跟踪非常重要。

我什至可以想象 future 的缩写:RTFST ;)

关于java 盒子面具不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3066253/

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