【发布时间】:2020-06-20 15:27:34
【问题描述】:
当我开始康威生命游戏时,我正在尝试绘制单个单元格 (20x20px)。当我单击屏幕时,我绘制一个单元格,或者根据数组中单元格的状态将其删除。这是可行的,但是,在任何给定时刻,屏幕上只能出现一个单元格,我不确定为什么如果单击屏幕的不同部分,单元格数组中的位置会发生变化。
主代码
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferStrategy;
import java.util.ArrayList;
import java.util.Iterator;
public class mainApplication extends JFrame implements Runnable, MouseListener {
private static final Dimension windowsize = new Dimension(80, 600);
private BufferStrategy strategy;
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 ArrayList<Cell> cellsList = new ArrayList<>();
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 - width / 2;
int y = screensize.height / 2 - height / 2;
setBounds(x, y, screensize.width, screensize.height);
setVisible(true);
createBufferStrategy(2);
strategy = getBufferStrategy();
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) {
int x = e.getX();
int y = e.getY();
xArrayElement = (x/20);
yArrayElement = (y/20);
xPosition = x - (x % 20);
yPosition = y - (y % 20);
// cellList.removeIf(cell -> cell.contains(xPosition, yPosition));
Iterator<Cell> iterator = cellsList.iterator();
if (e.getButton() == MouseEvent.BUTTON3) {
while (iterator.hasNext()) {
if (iterator.next().contains(xPosition, yPosition)) {
iterator.remove();
}
}
}
else{
cellsList.add(new Cell(xPosition, yPosition));
}
}
@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(cellsList != null) {
for (Cell cell : cellsList) {
cell.paint(g);
System.out.println("test");
}
}
this.repaint();
strategy.show();
}
}
public static void main(String[]args){
mainApplication test = new mainApplication();
}
}
细胞类
import java.awt.*;
public class Cell {
int x;
int y;
public Cell(int x, int y){
this.x = x;
this.y = y;
}
public boolean contains(int xx, int yy) {
return xx >= x && yy >= y && xx <= x + 20 && yy <= y + 20;
}
public void paint(Graphics g){
g.setColor(Color.white);
g.fillRect(x, y, 20,20);
}
}
【问题讨论】:
-
记住我的另一个答案。当您单击一个单元格时,将该单元格添加到某种列表中。重新绘制时,请遍历列表并重新绘制所在位置的所有单元格。这是因为每次重绘时都会清除背景(这是您应该做的)。所以每次都需要刷新屏幕。我将编写一个快速演示,以便您查看。
-
嘿,抱歉,在看到您的其他答案之前,我实际上已经上传了这个,但我一直在努力解决这个问题。我做了一个列表,添加了单元格。在我遍历列表的绘制方法中,如果列表中有一个元素,我会绘制单元格,如果没有,那么我会创建一个 if 语句来绘制任何被删除的单元格。然而,从来没有画过任何东西。我会为你更新我的代码
标签: java arrays swing awt paint