Im trying to "Restart" my little snake game by pressing "Enter". It works but it opens a new JFrame and leaves the old one opened. And if i play the game 4 times i will have 4 opened windows at the end. I know this might sound as a stupid question, but im introducing myself in the gui world and any kind of help would be appreciated. Thank you guys! Here i leave the 3 classes that run my program:
public class SnakeGame { //main class
public static void main(String[] args) {
GameFrame frame = new GameFrame();
}
}
import javax.swing.JFrame;
public class GameFrame extends JFrame{
GamePanel panel;
GameFrame(){
panel = new GamePanel();
this.add(panel);
this.setTitle("Snake");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.pack();
this.setVisible(true);
this.setLocationRelativeTo(null); //centra el JFrame en la pantalla
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;
public class GamePanel extends JPanel implements ActionListener {
static final int SCREEN_ANCHO = 600;
static final int SCREEN_ALTO = 600;
static final int UNIT_SIZE = 25;
static final int GAME_UNITS = ((SCREEN_ANCHO * SCREEN_ALTO) / UNIT_SIZE);
static final int DELAY = 75;
final int[] x = new int[GAME_UNITS];
final int[] y = new int[GAME_UNITS];
int bodyParts = 6;
int applesEaten;
int appleX;
int appleY;
char direction = 'R';
boolean running = false;
Timer timer; // crea un objeto de tipo timer que sirve para iniciar acciones cada x tiempo
Random random; // objeto de tipo random para generar numeros (posiciones) aleatorias
GamePanel() {
random = new Random();
this.setPreferredSize(new Dimension(SCREEN_ANCHO, SCREEN_ALTO));
this.setBackground(Color.BLACK);
this.setFocusable(true); //hace que que los eventos incidan sobre el panel (hace que sea posible ponerle el foco)
this.addKeyListener(new MyKeyAdapter());
startGame();
}
public void startGame() {
newApple();
running = true;
timer = new Timer(DELAY, this); //contador que actua cada x tiempo sobre el objeto indicado (el panel si pones this)
timer.start(); //comienza el contador
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
draw(g);
}
public void draw(Graphics g) {
if(running){
// for (int i = 0; i < SCREEN_ALTO / UNIT_SIZE; i++) {
// g.drawLine(i * UNIT_SIZE, 0, i * UNIT_SIZE, SCREEN_ALTO);
// g.drawLine(0, i * UNIT_SIZE, SCREEN_ANCHO, i * UNIT_SIZE);
// }
g.setColor(Color.RED);
g.fillOval(appleX, appleY, UNIT_SIZE, UNIT_SIZE);
for (int i = 0; i < bodyParts; i++) {
if (i == 0) {
g.setColor(Color.green.darker().darker());
g.fillRect(x[i], y[i], UNIT_SIZE, UNIT_SIZE);
} else {
g.setColor(Color.green.brighter());
g.setColor(new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255)));
g.fillRect(x[i], y[i], UNIT_SIZE, UNIT_SIZE);
}
g.setColor(Color.RED);
g.setFont(new Font("Ink Free", Font.BOLD, 35));
FontMetrics metrics = getFontMetrics(g.getFont());
g.drawString("Score:" + applesEaten, (SCREEN_ANCHO - metrics.stringWidth("Score:" + applesEaten))/2, g.getFont().getSize());
}
}
else{
gameOver(g);
}
}
public void newApple() {
appleX = random.nextInt((int) (SCREEN_ANCHO / UNIT_SIZE)) * UNIT_SIZE;
appleY = random.nextInt((int) (SCREEN_ALTO / UNIT_SIZE)) * UNIT_SIZE;
}
public void move() {
for (int i = bodyParts; i > 0; i--) {
x[i] = x[i - 1];
y[i] = y[i - 1];
}
switch (direction) {
case 'U':
y[0] = y[0] - UNIT_SIZE;
break;
case 'D':
y[0] = y[0] + UNIT_SIZE;
break;
case 'L':
x[0] = x[0] - UNIT_SIZE;
break;
case 'R':
x[0] = x[0] + UNIT_SIZE;
break;
}
}
public void checkApple() {
if ((x[0] == appleX) && (y[0] == appleY)) {
bodyParts++;
applesEaten++;
newApple();
}
}
public void checkCollisions() {
//revisa si la cabeza se choca con el cuerpo
for (int i = bodyParts; i > 0; i--) {
if ((x[0] == x[i]) && (y[0] == y[i])) {
running = false;
}
}
//revisa si la cabeza toca borde izquierdo
if (x[0] < 0) {
x[0] = SCREEN_ANCHO-Math.abs(x[0]);
}
//revisa si la cabeza toca borde derecho
if (x[0] >= SCREEN_ANCHO) {
x[0] = x[0]%SCREEN_ANCHO;
}
//revisa si la cabeza toca borde superior
if (y[0] < 0) {
y[0]=SCREEN_ALTO-Math.abs(y[0]);
}
//revisa si la cabeza toca borde inferior
if (y[0] >= SCREEN_ALTO) {
y[0]=y[0]%SCREEN_ALTO;
}
}
public void gameOver(Graphics g) {
//GameOver text
g.setColor(Color.RED);
g.setFont(new Font("Ink Free", Font.BOLD, 75));
FontMetrics metrics = getFontMetrics(g.getFont());
g.drawString("GAME OVER", (SCREEN_ANCHO - metrics.stringWidth("GAME OVER"))/2, SCREEN_ALTO/2-20);
//score
g.setFont(new Font("Ink Free", Font.BOLD, 55));
metrics = getFontMetrics(g.getFont());
g.drawString("Score:" + applesEaten, (SCREEN_ANCHO - metrics.stringWidth("Score:" + applesEaten))/2, SCREEN_ALTO/2+40);
//PLAY AGAIN
g.setFont(new Font("Ink Free", Font.BOLD, 35));
metrics = getFontMetrics(g.getFont());
g.drawString("Press ENTER to play again", (SCREEN_ANCHO - metrics.stringWidth("Press ENTER to playa again"))/2, SCREEN_ALTO/2+80);
}
@Override
public void actionPerformed(ActionEvent e) {
if (running) {
move();
checkApple();
checkCollisions();
}
repaint();
}
public class MyKeyAdapter extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT:
if (direction != 'R') {
direction = 'L';
}
break;
case KeyEvent.VK_RIGHT:
if (direction != 'L') {
direction = 'R';
}
break;
case KeyEvent.VK_UP:
if (direction != 'D') {
direction = 'U';
}
break;
case KeyEvent.VK_DOWN:
if (direction != 'U') {
direction = 'D';
}
break;
}
if(e.getKeyCode() == KeyEvent.VK_SPACE){ //this is were i tried to close the old window
new GameFrame();
}
}
}
}
question from:
https://stackoverflow.com/questions/65679978/why-i-cant-close-my-jframe-with-the-dispose-method 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…