I am creating a program using NetBeans 11.2 that has an animation in it. While creating it after the animation when the static variable (CurrentFrame is equal to 176) I want the Frame to remove the Jpanel so I can add pictures of cars to my program and move the pictures. so i made an if statement so when Currentframe == 176
it will remove said panel but it wont work no matter what i do. I did System.out.print to see if it even counted up to 176 and it did. So what's the problem any tips?
package Frametest;
import javax.swing.*;
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Main extends JPanel implements ActionListener {
public final Timer animate;
public static Boolean z = false;
public final ImageIcon ImageArray[];
public static int delay = 50, FrameCount = 177, Currentframe = 0;
public Main() {
ImageArray = new ImageIcon[FrameCount];
//Array used to store 200 Frames
for (int i = 1; i < ImageArray.length; i++) {
ImageArray[i] = new ImageIcon(getClass().getResource("Cartest (" + i + ").jpg"));
//i++ causes the Array to constantly pull up an image that i have named
//from numbers 1-200
}
animate = new Timer(delay, this);
animate.start();
//Setting a delay using a timer
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
System.out.println(Currentframe);
Currentframe++;
ImageArray[Currentframe].paintIcon(this, g, 0, 0);
//each time this method is called the image is going to differentiate
}
public void actionPerformed(ActionEvent e) {
do {
repaint();
} while (Currentframe >= ImageArray.length);
//clears the graphics and images and recalls the method
}
static class Action implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
JFrame f = new JFrame("hello");
Main s = new Main();
f.add(s, BorderLayout.CENTER);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(1280, 760);
f.setVisible(true);
if (Currentframe == 176) {
f.getContentPane().removeAll();
f.setVisible(false);
}
}
}
public static void main(String[] args) throws MalformedURLException, IOException, InterruptedException {
//Title Page
JFrame f = new JFrame("Button Example");
JButton b = new JButton("Click Here");
b.setBounds(50, 100, 95, 30);
f.add(b);
b.addActionListener(new Action());
f.setSize(400, 400);
f.setLayout(null);
f.setVisible(true);
}
}