When I run my code, and advance from level to level it works correctly, but when I would like to restart my program I cannot get the frame to reset. Here is my code
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Frame implements KeyListener{
GraphicsDevice vc;
Player p = new Player (this);
Game1 g1 = new Game1(this,p);
Game2 g2 = new Game2(this,p);
Game3 g3 = new Game3(this,p);
Game4 g4 = new Game4(this,p);
Caves caves = new Caves(this);
int gameFrame = 1;
JFrame f;
public Frame(){
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
vc = env.getDefaultScreenDevice();
Robot r;
try {
r = new Robot();
r.mouseMove(1000, 1000);
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
f = new JFrame ();
f.add(g1);
f.addKeyListener(this);
f.setResizable(false);
f.setUndecorated(true);
vc.setFullScreenWindow(f);
g1.repaintMethod();
}
public static void main(String[] args) {
Frame f = new Frame();
}
boolean pB, l;
@Override
public void keyPressed(KeyEvent arg0) {
// TODO Auto-generated method stub
int key = arg0.getKeyCode();
if (key == KeyEvent.VK_ESCAPE){
f.dispose();
}
if (key == KeyEvent.VK_LEFT){
System.out.println("Left");
switch (gameFrame){
case 1:
g1.moveLeft();
break;
case 2:
g2.moveLeft();
break;
case 3:
g3.moveLeft();
break;
case 4:
g4.moveLeft();
break;
case 5:
caves.moveLeft();
break;
}
}
if (key == KeyEvent.VK_RIGHT){
switch (gameFrame){
case 1:
g1.moveRight();
break;
case 2:
g2.moveRight();
break;
case 3:
g3.moveRight();
break;
case 4:
g4.moveRight();
break;
case 5:
caves.moveRight();
break;
}
}
if (key == KeyEvent.VK_UP){
}
if (key == KeyEvent.VK_DOWN){
}
if (key == KeyEvent.VK_SPACE){
switch (gameFrame){
case 1:
g1.select();
break;
case 2:
g2.select();
break;
case 3:
g3.select();
break;
case 4:
g4.select();
break;
case 5:
caves.select();
break;
}
}
if (key == KeyEvent.VK_F1){
l = true;
}
if (key == KeyEvent.VK_F12){
pB = true;
}
checkRestart();
System.out.println("L: " + l + "\t p: " + pB);
}
private void checkRestart() {
// TODO Auto-generated method stub
if (pB && l){
restart();
}
}
private void restart() {
// TODO Auto-generated method stub
System.out.println ("Restarting");
p.idol1 = false;
p.idol2 = false;
p.idol3 = false;
p.idol4 = false;
gameFrame = 0;
nextArea();
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
int key = arg0.getKeyCode();
if (key == KeyEvent.VK_L){
l = false;
}
if (key == KeyEvent.VK_P){
pB = false;
}
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
public void nextArea (){
System.out.println ("NextArea");
if (gameFrame < 5){
gameFrame ++;
}
if (gameFrame == 1){
f.removeAll();
f.add(g1);
g1.repaintMethod();
}
else if (gameFrame == 2){
f.remove(g1);
f.add(g2);
g2.repaintMethod();
}
else if (gameFrame == 3){
f.remove(g2);
f.add(g3);
g3.repaintMethod();
}
else if (gameFrame == 4){
f.remove(g3);
f.add(g4);
g4.repaintMethod();
}
else if (gameFrame == 5){
f.remove(g4);
f.add(caves);
caves.repaintMethod();
}
f.requestFocus();
f.repaint();
f.validate();
f.revalidate();
}
}
I reset the gameFrame to 0, and then call nextArea which increments it by 1, and then it should remove all the frames (whichever one I was on when I restarted), and put in frame 1, so the game starts over. Why isn't it doing this?
Thanks for your help.