나는 약 4 시간 동안이 작업을하고있다. 나는 모든 로직을 가지고 있고, 타이머는 작동하지만 키보드와 마우스 입력은 작동하지만, paintComponent 메소드는 업데이트를 그리기 위해 호출되지 않는다! 나는 실시간 응용 프로그램의 이러한 종류의 경험이 없기 때문에 이것이 작동하지 않는 원인이 될 수 있습니다. 어떤 도움을 주시면 감사하겠습니다!자바 스윙 프로젝트에 머물렀다 : 콘웨이의 삶의 게임
gamemain.java : gamemain
실제로 표시 할 수있는 것도 추가되지 않습니다 때문에
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
public class gamemain extends JPanel {
public static boolean paused = true;
public static JFrame gui;
public static JPanel panel;
static int[][] worldsize = new int[1000][1000];
static world playspace = new world(worldsize);
static Timer timer = new Timer(250, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Debug: timer ticked");
playspace.updateWorldState();
panel.repaint();
}
});
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
System.out.println("Hit painter");
g.drawString("Test", 50, 50);
for(int i = 0; i < 999; i++) {
for(int j = 0; j < 999; i++) {
if(playspace.getWorldStateAt(i, j) == 1) {
g.drawRect(i, j, 1, 1);
}
}
}
}
public static class listener implements KeyListener {
@Override
public void keyTyped(KeyEvent e) {
if(e.getKeyChar() == ' ') {
if(paused) {
timer.start();
paused = false;
System.out.println("Unpaused");
}
else {
timer.stop();
paused = true;
System.out.println("Paused");
}
}
}
@Override
public void keyPressed(KeyEvent e) {
// unused
}
@Override
public void keyReleased(KeyEvent e) {
// unused
}
}
public static class mouselistener implements MouseListener {
@Override
public void mouseClicked(MouseEvent e) {
int x, y;
x = e.getX();
y = e.getY();
playspace.placeCell(x, y);
}
@Override
public void mousePressed(MouseEvent e) {
// unused
}
@Override
public void mouseReleased(MouseEvent e) {
// unused
}
@Override
public void mouseEntered(MouseEvent e) {
// unused
}
@Override
public void mouseExited(MouseEvent e) {
// unused
}
}
public static void main(String[] args) throws InterruptedException {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
gui = new JFrame();
gui.setTitle("Game of Life");
gui.setSize(1000, 1000);
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
panel.addKeyListener(new listener());
panel.addMouseListener(new mouselistener());
panel.setBackground(Color.WHITE);
panel.setOpaque(true);
Container pane = gui.getContentPane();
pane.add(panel);
gui.setVisible(true);
panel.requestFocus();
}
});
}
}
world.java
public class world {
int[][] worldstate = new int[1000][1000];
public world(int[][] world) {
for(int i = 0; i < 1000; i++) { // make all cells dead
for(int j = 0; j < 1000; j++) {
world[i][j] = 0;
}
}
this.worldstate = world;
}
public int getWorldStateAt(int i, int j) {
return this.worldstate[i][j];
}
public void placeCell(int x, int y) {
this.worldstate[x][y] = 1;
}
public void updateWorldState() {
int n1,n2,n3,n4,n5,n6,n7,n8,total; // all possible buddies of a cell
for(int i = 1; i < 999; i++) { // go to each cell
for(int j = 1; j < 999; j++) {
n1 = this.worldstate[i-1][j];
n2 = this.worldstate[i][j-1];
n3 = this.worldstate[i-1][j-1];
n4 = this.worldstate[i+1][j];
n5 = this.worldstate[i][j+1];
n6 = this.worldstate[i+1][j+1];
n7 = this.worldstate[i+1][j-1];
n8 = this.worldstate[i-1][j+1];
total = n1 + n2 + n3 + n4 + n5 + n6 + n7 + n8; // add them up
if(total < 2 && (this.worldstate[i][j] == 1)) { // rule 1
this.worldstate[i][j] = 0;
}
if(total > 3 && (this.worldstate[i][j] == 1)) { //rule 3
this.worldstate[i][j] = 0;
}
if((total == 3) && (this.worldstate[i][j] == 0)) { //rule 4
this.worldstate[i][j] = 1;
}
}
}
}
}
감사합니다. gamemain을 추가해야하는 이유를 설명해 주시겠습니까? 이미 JFrame과 JPanel을 그립니다. –
@MichaelSimanski 주 창과 같이 표시 할 수있는 항목에 추가해야합니다. 업데이트 된 예제 – MadProgrammer
을 확인하십시오. 이제 "gamemain panel = new JPanel();"에서 유형 불일치 오류가 발생합니다. –