How to reset the game ?

I’m using Graphics2D with java and I want to reset the game if the alien ship hit my ship.
I tried some different things but didn’t worked.

Here’s the JFrame class:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Window extends JFrame {

    public Window() {
        add(new Screen());
        setTitle("Square Invaders!");
        setSize(900, 560);

    public static void main(String[] args) {
        new Window();

Here’s the part of the code where I check if the game ended which resides in the class (extends JPanel):

public void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        if (ingame) {
            if (spaceShip.isVisible()) {

            g2d.drawString("Squares left: " + aliens.size(), 5, 15);
        } else {
            String msg = "Game Over";
            Font small = new Font("Viner Hand ITC", Font.BOLD, 36);
            FontMetrics metr = this.getFontMetrics(small);

            g.drawString(msg, (S_WIDTH - metr.stringWidth(msg)) / 2,
                    S_HEIGHT / 2 - metr.getHeight());

I tried to do this(by adding the : new Window()):

            g.drawString(msg, (S_WIDTH - metr.stringWidth(msg)) / 2,
                    S_HEIGHT / 2 - metr.getHeight());
            new Window();

the result is a window after window is being created !

well you need to reset everything score player …
just when you do like

player = new Player(....);

just repeat that! And reset score,lives … You don’t need to create a new window!
I hope i helped!

So do I have to reset the panel ? which is inside the Frame.
or just rest all the other entities that reside inside the panel ? is that what you mean ?

and is it possible to just call the constructor of the Panel which will reset everything by writing : new Panel() ?

You just need to reset all game objects (instantiate them again), as GNecro said. You could make an initialization method (which sets up the game objects) and run that method every time you need to reset the game.

Done! issue Solved!
thanks GNecro1

If you want to reset a game, you don’t want to “close the game” and restart it within the application.

To achieve the reset, just reset any variables back to the default.

I am going to assume you have an array of aliens, where you first put the aliens in the array, you could put it into a new method

	public void createLevel(){
		aliens = new Alien[levelSize];
		for(int i = 0; i < levelSize; i++){
			alien[i] = new Alien();
		//recreate or have a method inside player to change it back to the default values
		//and anything else you need to create for your game level

something like that, and you can create new levels of any size pretty fast.

Edit: Ninjad, I hate formatting inside the post, so I always open up eclipse to format :frowning: just as i finished the code you posted it, but it was to much effort to delete!

Awesome Phased ! now I also learned how to make levels ! I had no idea I could do it in that simple way

Edit: LOL, no please don’t delete it, I learned something new from that code you posted Phased.