Win a copy of Java Challengers this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

JFrame not disposing

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 508
15
Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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 ).
 
Arjun Inamdar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The main method is in another class
 
Prasad Saya
Rancher
Posts: 508
15
Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, how do I run this program?
 
Arjun Inamdar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Prasad Saya
Rancher
Posts: 508
15
Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Arjun Inamdar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Main Class


MiniTennis Class
 
Arjun Inamdar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry wrong code for MiniTennis
 
Prasad Saya
Rancher
Posts: 508
15
Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)!
 
Saloon Keeper
Posts: 4380
163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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.
 
Arjun Inamdar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I also messed up the main class. Here is the fixed one.
 
Arjun Inamdar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Piet

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


 
Arjun Inamdar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Will that make the JFrame dispose properly
 
Piet Souris
Saloon Keeper
Posts: 4380
163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Piet Souris
Saloon Keeper
Posts: 4380
163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way:

Welcome to the Ranch! I hope you enjoy the stay!
 
The harder I work, the luckier I get. -Sam Goldwyn So tiny. - this ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic