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
/*
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