Win a copy of Raising Young Coders: A Parent’s Guide to Teaching Programming at Home this week in the General Computing forum!
Forums Login/signup

JFrame not disposing

+Pie Number of slices to send: Send
In my code, for some reason, my JFrame is not disposing. I want it to dispose if BallY > 720, but it doesn't work. Code below.

/*
MiniTennis by Arjun I.
Code already created
*/

import java.awt.*;
import java.awt.event.*;


import javax.swing.*;

public class MiniTennis extends JComponent implements ActionListener, MouseMotionListener{
//variables for ball and paddle movement
private int BallX = 715;
private int BallY = 200;
private int BallXSpeed = 5;
private int BallYSpeed = 8;
private int PaddleX = 665;
public int score = 0;
public int highscore = 0;
private boolean checkIfOver = false;
private JFrame game = new JFrame("Mini Tennis by Arjun Inamdar(Play)");

//starts the whole game
public void newgame() {
//creates new JFrame and instance of MiniTennis to add to JFrame

MiniTennis minitennis = new MiniTennis();
game.add(minitennis);
game.setSize(1430,860);
game.setResizable(true);
game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game.setVisible(true);

Timer t = new Timer(20, minitennis);
t.start();

game.addMouseMotionListener(minitennis);




}

//draws all the shapes
public void paintComponent(Graphics g) {
//creates background
g.setColor(new Color(0,0,0));
g.fillRect(0,0,2000,2000);

//creates all of the shapes and score text
g.setColor(new Color(255,255,255));
g.fillOval(BallX, BallY, 30,30);
g.fillRect(PaddleX, 700,100,20);
g.setFont(new Font("sans-serif",Font.BOLD,30));
g.drawString("Score: " + score, 1290,40);
}

//moves the ball and sees if game is over
@Override
public void actionPerformed(ActionEvent event) {
BallX = BallX + BallXSpeed;
BallY = BallY + BallYSpeed;
repaint();

if(BallX >= PaddleX && BallX <= PaddleX + 100 && BallY >= 680 && BallY <= 700) {
BallYSpeed = -8;
score++;
}else if(BallX >= 1400) {
BallXSpeed = -5;
}else if(BallY <= 0) {
BallYSpeed = 8;
}else if(BallX <= 0) {
BallXSpeed = 5;
}

if(BallY > 720) {
game.dispose();
}


}

//unused method
@Override
public void mouseDragged(MouseEvent arg0) {


}

//moves paddle
@Override
public void mouseMoved(MouseEvent e) {
PaddleX = e.getX() - 50;
repaint();

}


}

Please help as JFrame will not dispose
+Pie Number of slices to send: Send


Did you try to run the program? Looks like the main method is missing.

Also, one can UseCodeTags to format the code (I noticed this is your first visit here ).
+Pie Number of slices to send: Send
The main method is in another class
+Pie Number of slices to send: Send
So, how do I run this program?
+Pie Number of slices to send: Send
First copy this code into a file named MiniTennis. Then, you create a new file(whatever you want to name it) and create a new class with the main method. Then you create an instance of the MiniTennis class, and you run the newgame method.
+Pie Number of slices to send: Send
 

Arjun Inamdar wrote:First copy this code into a file named MiniTennis. Then, you create a new file(whatever you want to name it) and create a new class with the main method. Then you create an instance of the MiniTennis class, and you run the newgame method.



Please post that code here.
+Pie Number of slices to send: Send
Main Class


MiniTennis Class
+Pie Number of slices to send: Send
Sorry wrong code for MiniTennis
+Pie Number of slices to send: Send
Did you try to compile and run the code? What was the result? I couldn't compile the code (the class with the main method)!
+Pie Number of slices to send: Send
@Prasad
you can easily fake a main method with this:


Problem with the code is these few lines:

It is hard tp predict which MiniTennis and game we are dealing when the frame is showing on the screen. Replace the reference 'miniteniis' by 'this' and you get a sort of working tennis game.

From an OO point of view there are many remarks to be made. If OP is interested, then let us know.
+Pie Number of slices to send: Send
Sorry, I also messed up the main class. Here is the fixed one.
+Pie Number of slices to send: Send
@Piet

So are you saying to do this-
MiniTennis minitennis  = new MiniTennis();
game.add(this.minitennis);


+Pie Number of slices to send: Send
Will that make the JFrame dispose properly
+Pie Number of slices to send: Send
hi Arjun,

no, I don't mean that. The  new Minigame() is already created in the main method, so it already exists when newgame() is invoked, includig the game variable. By doing another 'MiniTennis mini = new MiniTennis()' you are creating another MiniTennis instance, with its own game variable, that itself creates another NewTennis() instance et cetera! That makes it very hard to follow what actually is happening.

So, just leave out the line 'MiniTennis mini = new MiniTennis()' and replace it with 'game.add(this)'. Even then, the code is pretty fragile. For instance, MiniTennis represents your tennisfiled, why must that class take care of a JFrame to which it should belong? As you see, that complicates the code quite a bit! But that for later, first see if what you have now works as intended.

By the way: to see the 'Exit on close' doing its job, you must be very quick to click the close button of the frame, since as I have it now, the ball disappears at the bottom in no time, the frame disappears to, but the game is still running. So, I guess there is some more work to be done!
+Pie Number of slices to send: Send
By the way:

Welcome to the Ranch! I hope you enjoy the stay!

reply
reply
This thread has been viewed 807 times.
Similar Threads
Newbie help
Problems with my run() method.
Class to draw a circle to Jframe
noob needs paint help
keyboard arrow keys not working
More...

All times above are in ranch (not your local) time.
The current ranch time is
Jun 27, 2025 04:28:22.