0

I've tried every solution I could find here and on forums but I cannot close the wind JFrame no matter what I do and I have no idea why. I need to dispose of the JFrame and launch a different one by creating a WarioWareGUI object.

Right now, the best I seem to be able to do is stop the ball from moving and just leave the old JFrame in the background, but that's not quite good enough. I need the wind JFrame to close

I've tried infinitely disposing, I've tried setting the JFrame as public static and initializing it as an object of BallGame so that I can dispose from anywhere. I've tried simply disposing it immediately after creating it. Nothing seems to get rid of it.

I even tried just setting the ball speed to 0 and making the frame invisible but wind.setVisible(false); doesn't do anything either.

package GameProject;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
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.MouseMotionListener;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.Timer;
import javax.swing.WindowConstants;

public final class BallGame extends JComponent implements ActionListener, MouseMotionListener, KeyListener {

    private int ballx = 150;
    private int bally = 30;
    private int paddlex = 0;
    private static int ballySpeed = 7;
    private static int ballxSpeed = 5;
    private int score = 0;
    private int round = 1;
    private int bounces = 0;
    private static int delay = 20;
    public static boolean gameOver = false;
    public static boolean gameOverFlag = false;
    public boolean started;

    public static void main(int round) {

        //only true the first time the game is run
        if ((gameOver == false) && (gameOverFlag == false)) {
            gameMake(round);
        }

        //only true after the game has resulted in a loss
        if ((gameOver == true) && (gameOverFlag==true)) {
            gameOver = false;
            //gameMake(round);
            WarioWareGUI gui = new WarioWareGUI();
        }

        //only true after the game has resulted in a loss and
        //returned to main menu
        if ((gameOver == false) && (gameOverFlag==true)) {

        }

    }

    public static void gameMake(int round) {

        JFrame wind = new JFrame("RedBall/GamePinfo");
        BallGame g = new BallGame();
        wind.add(g);
        wind.pack();
        wind.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        wind.setLocationRelativeTo(null);
        wind.setVisible(true);
        wind.addMouseMotionListener(g);

        wind.dispose();

        //speed up the timer each round
        Timer tt = new Timer(delay, g);
        tt.start();

    }

    public void newball(int ballx, int bally, int ballxspeed, int ballyspeed) {

        ballx = 150;
        bally = 30;
        ballxspeed = 5;
        ballyspeed = 7;

        JOptionPane.showMessageDialog(null, "new ball !");

        return;
    }

    @Override
    public Dimension getPreferredSize() {

        return new Dimension(800, 600);
    }

    @Override
    protected void paintComponent(Graphics g) {

        //draw the sky
        g.setColor(Color.cyan);
        g.fillRect(0, 0, 800, 600);

        g.setColor(Color.GREEN);
        g.fillRect(0, 550, 800, 100);

        //draw the paddel
        g.setColor(Color.black);
        g.fillRect(paddlex, 500, 100, 20);

        //draw the ball
        g.setColor(Color.RED);
        g.fillOval(ballx, bally, 30, 30);

        g.setColor(Color.white);
        g.setFont(new Font("Arial", 8, 50));
        g.drawString(String.valueOf(score), 30, 80);

    }

    @Override
    public void actionPerformed(ActionEvent e) {

        //Manages ball movement
        ballx = ballx + ballxSpeed;
        bally = bally + ballySpeed;

        if (gameOverFlag) {
            round = 1;
            BallGame.main(round);
        }

        else if (gameOver) {

            gameOverFlag = true;

            //displays the dialog box indicating victory
            JOptionPane.showMessageDialog(this, "You lost on round"
                    + round + "!");

            ballx = 150;
            bally = 30;


        }

        else if (bounces==10) {

            //reset score
            bounces = 0;

            round++;

            JOptionPane.showMessageDialog(this,"You Win! You move on to"
                    + " round " + round + "!");

        }


        // Sets ball speed upward if it hits the paddle
        else if (ballx >= paddlex && ballx <= paddlex + 100 && bally >= 475) {

            ballySpeed = -7 - (round*2);

            //monitors the number of times the ball is successfully hit
            score++;
            bounces++;

        }

        // Handles loss condition
        else if (bally >= 500 ) {

            gameOver = true;

        }

        // Sets ball movement down if it hits the ceiling
        else if (bally <= 0) {

            ballySpeed = 7 + (round*2);

        }

        // Sets ball
        else if (ballx >= 775) {

            ballxSpeed = -5 - (round*2);

        }

        // Window left
        else if (ballx <= 0) {

            ballxSpeed = 5 + (round*2);

        }

        //**********************************************************************

        repaint();
    }

    @Override
    public void mouseMoved(MouseEvent e) {

        //places paddle in middle of mouse
        paddlex = e.getX() - 50;
        repaint();
    }

    @Override
    public void mouseDragged(MouseEvent e) {
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {

    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

}

I expect calling wind.dispose to shut down the GUI but it won't, no matter where I put it or how I declare the JFrame. I can't just make it invisible either.

The code right now is just where it was when I gave up, I'm not trying to just dispose of it right away of course, but once I can get it to just dispose once successfully, hopefully I can get it to do that at the right time.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 1
    @HovercraftFullOfEels they explain in the question: "I'm not trying to just dispose of it right away of course, but once I can get it to just dispose once successfully, hopefully I can get it to do that at the right time." – TheOnlyMrCat Aug 04 '19 at 20:09
  • @TheOnlyMrCat: thank you -- I skipped over that and dove into the code itself. – Hovercraft Full Of Eels Aug 04 '19 at 20:10
  • The JFrame closes for me, but the JVM does not exit. Side issue: you should make a call to the sniper's method within the paintComponent override – Hovercraft Full Of Eels Aug 04 '19 at 20:18
  • 1
    Oops that should be the "super's" method -- dang auto-correct on my phone -- specifically `super.paintComponent(g);` on the first line of your override, to remove dirty pixels. Another thing -- you really don't want to throw JFrame's at the user. Better to swap JPanel views with a CardLayout. – Hovercraft Full Of Eels Aug 04 '19 at 20:41

0 Answers0