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.

Which part doesnt work, the frame removing or the game re-starting? If the frame does not close, then you could try the below methods, have a look at the JFrame class, (below).

f.dispose();
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);

Or the update() method to call paint instead.

http://docs.oracle.com/javase/1.4.2/docs/api/javax/swing/JFrame.html

Then once that works, you can focus on the game restarting. I'm not sure but you need to reload the game class or reconstruct it??

Can you make a SSCCE and post it that shows your problem?

The game does not restart. I click F1 and F12 at the same time, and both of them are true, and it should remove the frame that the game is currently on, and then set the gameFrame to 0, and then increment it. Then it should go into the if statement on line 176, and add in the first frame, but it doesn't.

but it doesn't.

Try debugging the code by adding printlns to show where the execution flow goes.

most of yours KeyEvent are implemented as internal commands for various JComponents, you can check that in BasicXxxUI, how many times I suggest you implements another API

it goes to the correct if statement, but it doesn't display the first stage again. I see the stage that I was on last. If I remove all the possible areas right after each other

f.remove(g1);
f.remove(g2);
f.remove(g3);
f.remove(g4);
f.remove(caves);

then it displays the first stage, but it doesn't do anything (my arrow is frozen, and I can't do anything, but exit my game).

I checked, and the signal doesn't get to the Game1 class from the frame when I reset the game.

Thanks for the help.

but it doesn't do anything

Where is the code blocking/stopping execution when that happens?

my arrow is frozen,

What is the "arrow"?

the arrow is an image that I display, and you can move it around with the arrow keys. I have it so that if the gameFrame = 1, then it moves the arrow in the g1 panel, you can see it in the switch statement. I printed it out, and its value is 1.

Did you add printlns to see where the execution flow goes?

How does the gameFrame value change? What values of it are printed out by the println statements that you have added?

I have it so that when I press my reset buttons it prints out reseting, and then when it goes to the nextArea() method it prints out Next Area, and when it enters the if statement I print out in gameFrame == 1 if statement.

I set the value to 0 when I reset, and call the nextArea() method. Then it increments by one, and goes into the first if statement.

What different values of gameFrame are printed out by the println statements?

Here is my current 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 ++;
			System.out.println ("Game Frame: " + gameFrame);
		}
		if (gameFrame == 1){
			System.out.println ("Entered gameFrame == 1 if statement");
			f.remove(g1);
			f.remove(g2);
			f.remove(g3);
			f.remove(g4);
			f.remove(caves);
			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();
		
	}
	
	
	
}

I just printed it out to see how far the code goes in the frame, and then I added a println to g1 in the moveLeft() method, and when I clicked left, it did not work.

What different values of gameFrame are printed out by the println statements?

You need to add LOTS more printlns!!!

well if it entered the first one(gameFrame == 1), then it is one. I just want to make sure the first one works before I go work on the second one. The game can run though once. It only glitches when I reset it using F1 and F12

It only glitches when I reset it using F1 and F12

What problems are you having now?

@NormR1 wrote ---> What problems are you having now? ---> BasicXxxUI

my problem is that when I reset the game, I cannot move my arrow around and select what I would like. The moveLeft() and moveRight() methods are not called for some reason.

not called for some reason.

You need to debug your program to see what is happening.
Add some more printlns to show what is happening when you execute your code.

Thanks for the help. After I added the printlns I noticed that my reset values were always true after I reset it. Then I noticed that in my keyReleased() method I was still using my original buttons ('L' and 'O'). after I reset those it all works correctly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.