Hello, I made a timer that shows an image when the time runs out, but I wanna make it work when I close the program and when I open it again, the time still running, for that I save the current miliseconds when the timer was started and the remaining time and if the time is running in a JSON file when I close the window and thus stopping the program, and read that data when I re-run the program
But when I close the window when the timer is running the program is still running and I have to force the stop, the JSON file is still written, but when I re-run the program, the timer appears as finished when it shouldn't, what's wrong?
import org.json.JSONObject; import org.json.JSONTokener; import javax.imageio.ImageIO; import javax.swing.*; import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.image.BufferedImage; import java.io.*; import java.nio.file.Files; import java.nio.file.Paths; public class Temporizador extends JFrame { private static final int DELAY = 10; // Milésimas de segundo private static final long TIEMPO = 86400000/DELAY; // En centésimas de segundo // Edit these if you wanna test it private static final String ROUTE = "C:\\Users\\SEBAS\\Documents\\NetBeansProjects\\OtroTemporizador"; private static final String IMAGEN = ROUTE + "\\Imagenes\\Imagen.jpg"; private static final String DATA = ROUTE + "\\Data\\Data.json"; private final JButton iniciar; private final JLabel imagen; private Timer reloj; private long tiempoRestante; private Data data; public static void main(String[] args) { new Temporizador(); } private Temporizador() { data = readFile(); setTitle("Temporizador"); setSize(800, 600); setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); setResizable(false); setLocationRelativeTo(null); JPanel panel = new JPanel(); panel.setLayout(null); imagen = new JLabel(); imagen.setLocation(0, 90); imagen.setSize(600, 510); imagen.setPreferredSize(new Dimension(600, 510)); imagen.setOpaque(true); imagen.setBackground(Color.WHITE); imagen.setFont(new Font("Courier", Font.BOLD,75)); iniciar = new JButton(); iniciar.setText("Correr"); iniciar.setLocation(0, 0); iniciar.setSize(100, 30); iniciar.setPreferredSize(new Dimension(100, 30)); iniciar.setVisible(true); iniciar.addActionListener((event) -> { startTimer(TIEMPO); }); panel.add(imagen); add(iniciar); add(panel); // Guardar y/o checkear que se halla guardado información if (data.running) { startTimer((data.remaining - (System.currentTimeMillis() - data.start)) / DELAY); } addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { if (data.running) { data.remaining = tiempoRestante * DELAY; writeFile(data); } else { writeFile(new Data()); } e.getWindow().dispose(); } }); setVisible(true); } private void startTimer(long time) { tiempoRestante = time; data.running = true; data.start = System.currentTimeMillis(); reloj = new Timer(DELAY, (e) -> { updateTiempo(); if (tiempoRestante <= 0) { reloj.stop(); data.running = false; showImage(); } }); reloj.start(); } private void showImage() { File file = new File(IMAGEN); try { BufferedImage bufferedImage = ImageIO.read(file); imagen.setIcon(new ImageIcon(bufferedImage)); } catch (IOException ex) { ex.printStackTrace(); } } private static final int secDiv = 1000/DELAY; private static final int minDiv = 60 * secDiv; private static final int hourDiv = 60 * minDiv; private void updateTiempo() { tiempoRestante--; long cent = tiempoRestante % secDiv; long sec = (tiempoRestante / secDiv) % 60; long min = (tiempoRestante / minDiv) % 60; long hours = (tiempoRestante/ hourDiv); imagen.setText((hours < 10 ? "0" : "") + hours + ":" + (min < 10 ? "0" : "") + min + ":" + (sec < 10 ? "0" : "") + sec + ":" + (cent < 10 ? "0" : "") + cent); } private static class Data { long start = 0; // Milisegundos long remaining = 0; // Milisegundos boolean running = false; } private static Data readFile() { File file = new File(DATA); Data data = new Data(); if (!file.exists() || file.length() == 0) return data; try { JSONTokener parser = new JSONTokener(new FileInputStream(file)); JSONObject obj = new JSONObject(parser); data.start = obj.getInt("start"); data.remaining = obj.getInt("remaining"); data.running = obj.getBoolean("running"); } catch (FileNotFoundException e) { e.printStackTrace(); } return data; } private static void writeFile(Data data) { JSONObject obj = new JSONObject(); obj.put("start", data.start); obj.put("remaining", data.remaining); obj.put("running", data.running); try { File file = new File(DATA); BufferedWriter writer = Files.newBufferedWriter(Paths.get(file.toURI())); obj.write(writer); writer.close(); } catch (IOException e) { e.printStackTrace(); } } }