How to make Timer countdown along with progress bar?

前端 未结 3 1591
迷失自我
迷失自我 2020-12-11 22:14

How can I make it so that the progress bar slowly goes down with the time limit?

class GamePanel extends JPanel implements MouseListener, ActionListener
{
           


        
相关标签:
3条回答
  • 2020-12-11 22:54

    From the looks of it, all of this code is within a big Java file? That is a bad idea.

    You should have a good reason to define a class as an inner class, and from the looks of it, you do not have one for QuestionPanel and others.

    As for the problem, your paintComponent method is called every time your counter is updated, which is right now roughly once every 0.1 seconds, yet you only tick by 1 pixel on each update, so by the end of 5 seconds, you've cut off 10*5 pixels (50). What you should do is update the progress bar by a different mechanism, such as a calculating the current time processed:

    long processed = System.currentTimeMillis() - startTime;
    double percent = Math.max(0, 1 - processed / 5000.0);
    int width = (int)(590 * percent);
    
    0 讨论(0)
  • 2020-12-11 22:56

    That is definitely too much information, and a very broad question. I'd say at most you only need to include the code for the class where the timer is, and the class where the progress bar gets drawn.

    From skimming the code, I'm guessing you're using a rectangle to draw the progress bar. Based on that, one way you could go about it would be using a variable to store the width of the bar, and every time the timer ticks, decrease the width of the bar by a set amount. Then just set the width of the rectangle drawn to the value stored in the variable.

    0 讨论(0)
  • 2020-12-11 23:05

    Sorry, I still could not find the motivation to actually read your code, but just threw together this example based on the question. See if it gives you some ideas.

    Note that it is an SSCCE and uses just 40 lines of code in all.

    import java.awt.event.*;
    import javax.swing.*;
    
    class CountDownProgressBar {
    
        Timer timer;
        JProgressBar progressBar;
    
        CountDownProgressBar() {
            progressBar = new JProgressBar(JProgressBar.VERTICAL, 0, 10);
            progressBar.setValue(10);
            ActionListener listener = new ActionListener() {
                int counter = 10;
                public void actionPerformed(ActionEvent ae) {
                    counter--;
                    progressBar.setValue(counter);
                    if (counter<1) {
                        JOptionPane.showMessageDialog(null, "Kaboom!");
                        timer.stop();
                    } 
                }
            };
            timer = new Timer(1000, listener);
            timer.start();
            JOptionPane.showMessageDialog(null, progressBar);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater( new Runnable() {
                public void run() {
                    CountDownProgressBar cdpb = new CountDownProgressBar();
                }
            });
        }
    }
    
    0 讨论(0)
提交回复
热议问题