gpt4 book ai didi

java - 全局修改 Java 方法内的全局变量

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

我正在制作《康维斯生命游戏》。在鼠标监听器中,我希望单击一次时单元格出现/消失在屏幕上。我使用 20x20 像素单元的 40x40 boolean 数组 (gameState)。我想使用我在单击方法中获得的鼠标坐标在绘制方法中绘制正方形。但是,我在第 71 行遇到空指针异常,并且不知道如何解决它。

主要


import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferStrategy;

public class mainApplication extends JFrame implements Runnable, MouseListener {


private static final Dimension windowsize = new Dimension(80, 600);
private BufferStrategy strategy;
private Graphics offscreenGraphics;
private static boolean isGraphicsInitialised = false;
private static int rows = 40;
private static int columns = 40;
private static int height = windowsize.height;
private static int width = windowsize.width;
private static Cells cells;
private int xArrayElement,yArrayElement, xPosition, yPosition;
private static boolean gameState[][] = new boolean[rows][columns];

public mainApplication() {
System.out.println(System.getProperty("user.dir"));

setDefaultCloseOperation(EXIT_ON_CLOSE);
Dimension screensize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();

int x = screensize.width / 2 - windowsize.width / 2;
int y = screensize.height / 2 - windowsize.height / 2;

setBounds(x, y, screensize.width, screensize.height);

setVisible(true);

createBufferStrategy(2);

strategy = getBufferStrategy();

offscreenGraphics = strategy.getDrawGraphics();

isGraphicsInitialised = true;

// MouseEvent mouseEvent = new MouseEvent();
addMouseListener(this);
// addMouseMotionListener(MouseEvent);



Thread t = new Thread(this);

t.start();

}


public void mousePressed(MouseEvent e) { }

public void mouseReleased(MouseEvent e) { }

public void mouseEntered(MouseEvent e) { }

public void mouseExited(MouseEvent e) { }

public void mouseClicked(MouseEvent e) {

if(e.getClickCount() == 1){

xPosition = e.getX();
yPosition = e.getY();

cells.setPosition(xPosition,yPosition);

xArrayElement = (xPosition/20);
yArrayElement = (yPosition/20);

if(gameState[xArrayElement][yArrayElement]){
gameState[xArrayElement][yArrayElement] = false;
}
else if (!gameState[xArrayElement][yArrayElement]) {
gameState[xArrayElement][yArrayElement] = true;
}
}
}

@Override
public void run() {
while (true) {

try { //threads entry point
Thread.sleep(20); //forces us to catch exception
}

catch (InterruptedException e) {
}
}
}


public void paint(Graphics g) {

if (isGraphicsInitialised) {
g = strategy.getDrawGraphics();

g.setColor(Color.BLACK);

g.fillRect(0, 0, 800, 800);


if (gameState[xArrayElement][yArrayElement]) {
g.setColor(Color.WHITE);
cells.paint(g);
}

else if (!gameState[xArrayElement][yArrayElement]) {

g.setColor(Color.BLACK);
g.fillRect(xPosition, yPosition, 20, 20);
}

strategy.show();
}
}

public static void main(String[]args){

mainApplication test = new mainApplication();

}
}

细胞类别


import java.awt.*;

public class Cells {

int x;
int y;


public Cells(){

}

public void setPosition(int xi, int xj){

x = xi;
y = xi;
System.out.println(xi);
System.out.println("sjdkgffdjv" + y);
}

public boolean cellState(boolean visible){

return visible;
}

public void paint(Graphics g){

g.drawRect(x, y, 20,20);
}
}

最佳答案

这是因为您尚未在 Main 类中初始化 cells 变量。所以试试这个

private static Cells cells = new Cells();

关于java - 全局修改 Java 方法内的全局变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60565624/

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