⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 paprogresswindow.java

📁 一个用于排队系统仿真的开源软件,有非常形象的图象仿真过程!
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        double currentTime = (((double)System.currentTimeMillis())/1000 -                     //get time elapsed from start in seconds
                                           this.globalStartTime);

        double simulationDuration;
        if (!wasPaused) {
            simulationDuration = currentTime - lastSimulationFinishTime;            // the duration of the last finished simulation
        }
        else {
            simulationDuration = currentTime - lastSimulationFinishTime - (resumeTime - pauseTime);
            wasPaused = false;
        }
        lastSimulationFinishTime = currentTime;                                        //set the new last simulation finishing time
        globalCumulativeTime += simulationDuration;                                    //increase the cumulative time
        globalMeanSimulationTime = globalCumulativeTime/lastSimulationCompletedIndex;  //the mean duration time

        double min = simulationDuration;                                               //initialization useful for the first simulation finished
        double max = simulationDuration;

        // END INITIALIZATIONS//

        simulationTimes[lastSimulationCompletedIndex-1] = simulationDuration;          //insert last simulation duration into the Vector

        //for cycle used to get minimum and maximum simulaion duration
        for (int i=0; i<lastSimulationCompletedIndex; i++) {
            if (simulationTimes[i] < min)  min = simulationTimes[i];
            if (simulationTimes[i] > max)  max = simulationTimes[i];
        }



        int estimatedTime;
        int[] hist;      // it's a vector containing the number of simulation with a duration in a certain range.
                         // It's like a histogram; the algorithm uses the relative frequencies to estimate the most
                         // probable residual time.

        double dispersion = max - min;

        if (dispersion != 0) {    // always true except for the first case, or if all of the simulations have the same duration

            double gap = dispersion / histogramIntervals;    //the width of a single histogram band
            hist = new int[(int)histogramIntervals];

            // for cycle used to populate the histogram
            for (int i=0; i<lastSimulationCompletedIndex; i++) {
                double thisValue = simulationTimes[i];
                for (int j=1; j<=histogramIntervals; j++) {
                    if (thisValue <= (min + j*gap)) {
                        hist[j-1]++;
                        break;
                    }
                }
            }

            double sum = 0;
            for (int i=0; i<histogramIntervals; i++) {
                sum += ((double)(hist[i]))* (min + (i+1)*gap - gap/2);   //weighted sum of all simulation durations
            }
            estimatedTime = (int) (sum/(lastSimulationCompletedIndex)* (numberOfSimulations - lastSimulationCompletedIndex ));
        }
        else {
            estimatedTime = (int)(max*numberOfSimulations - lastSimulationCompletedIndex); //or min since they are the same number
        }
        this.remainingTimeString = "Residual time: " + getTimeString(estimatedTime);
        //this.remainingTimeLabel.setText(remainingTimeString);
    }

    /**
     * Used to format a time in seconds to a hh : mm : ss format
     * @param t time in seconds to be formatted
     * @return the formatted time
     */
    private String getTimeString(int t) {
        String timeString;
        int s = (t%60);
        int m = ((t/60)%60);
        int h = ((t/3600)%3600);
        if (h == 0) {
            timeString = "";
        }
        else {
            if (h < 10) {
                timeString = "0" + Integer.toString(h) + "h : ";
            }
            else {
                timeString = Integer.toString(h) + "h : ";
            }
        }
        if (m < 10) {
            timeString += "0" + Integer.toString(m) + "m : ";
        }
        else {
            timeString += Integer.toString(m) + "m : ";
        }
        if (s < 10) {
            timeString += "0" + Integer.toString(s) + "s";
        }
        else {
            timeString += Integer.toString(s) + "s";
        }
        return timeString;
    }

    /**
     * Sets the window in pause state
     */
    public void pause() {
        wasPaused = true;
        pauseTime = ((double)System.currentTimeMillis())/1000 - globalStartTime;
        mainLabel.setText(CommonConstants.HTML_START + CommonConstants.HTML_FONT_TITLE +
            PAUSE_MESSAGE + CommonConstants.HTML_FONT_TIT_END +
            CommonConstants.HTML_END);
        stopAnimation();
        paused = true;
        stopped = false;
        removeAnimation();
    }

    /**
     * Called to start the progress window
     */
    public void start() {
        globalStartTime = ((double)System.currentTimeMillis())/1000;
        lastSimulationFinishTime = 0;
        elapsedTime = 0;
        paused = false;
        stopped = false;
        timer = new Timer();
        timer.start();
        mainLabel.setText(CommonConstants.HTML_START + CommonConstants.HTML_FONT_TITLE +
            RUNNING_MESSAGE + CommonConstants.HTML_FONT_TIT_END +
            CommonConstants.HTML_END);
        restoreAnimation();
    }

    /**
     * Called to restart the progress window after it was paused
     */
    public void restart() {
        paused = false;
        stopped = false;
        resumeTime = ((double)System.currentTimeMillis())/1000 - globalStartTime;
        timer.setLastTime(resumeTime);
        mainLabel.setText(CommonConstants.HTML_START + CommonConstants.HTML_FONT_TITLE +
            RUNNING_MESSAGE + CommonConstants.HTML_FONT_TIT_END +
            CommonConstants.HTML_END);
        restoreAnimation();
    }

    private void removeAnimation() {
        globalPanel.remove(pivotPanel);
        pivotPanel = new JPanel (new GridBagLayout());
        pivotPanel.add(stoppedAnimation);
        globalPanel.add(pivotPanel,BorderLayout.CENTER);
        globalPanel.doLayout();
        globalPanel.validate();
        doLayout();
        validate();
    }

    private void restoreAnimation() {
        globalPanel.remove(pivotPanel);
        animation = new SampleQNAnimation();
        animation.start();
        pivotPanel = new JPanel (new GridBagLayout());
        animation.setPreferredSize(new Dimension(200,120));
        animation.setBackground(new Color(151,151,151));
        pivotPanel.add(animation);
        globalPanel.add(pivotPanel,BorderLayout.CENTER);
        globalPanel.doLayout();
        globalPanel.validate();
        doLayout();
        validate();
    }

    /**
     * Called when simulation is stopped.
     */
    public void stop() {
        paused = false;
        stopped = true;
        mainLabel.setText(CommonConstants.HTML_START + CommonConstants.HTML_FONT_TITLE +
            STOP_MESSAGE + CommonConstants.HTML_FONT_TIT_END +
            CommonConstants.HTML_END);
        if (stepNumber > 1) stepLabel.setText(CommonConstants.HTML_START + CommonConstants.HTML_FONT_NORM +
                                         "Only " + (stepNumber-1) + " simulations of " + numberOfSimulations + " were completed " + CommonConstants.HTML_FONT_NOR_END + CommonConstants.HTML_END);
        else
            stepLabel.setText(CommonConstants.HTML_START + CommonConstants.HTML_FONT_NORM +
                                                     "No simulation was completed " + CommonConstants.HTML_FONT_NOR_END + CommonConstants.HTML_END);

        stopAnimation();
        removeAnimation();
        progressBar.setValue(0);
        remainingTimeString = "Residual time: -- : --";
        //remainingTimeLabel.setText(remainingTimeString);
    }

    /**
     * Stops only the animation thread
     */
    public void stopAnimation() {
        animation.stop();
    }

    /**
     * Called when the parametric analyis is finished, not when it is stopped
     */
    public void finished() {
        stopped = true;
        paused = false;
        mainLabel.setText(CommonConstants.HTML_START + CommonConstants.HTML_FONT_TITLE +
            "Simulation complete" + CommonConstants.HTML_FONT_TIT_END +
            CommonConstants.HTML_END);
        stepLabel.setText(CommonConstants.HTML_START + CommonConstants.HTML_FONT_NORM +
                                         "All of the " + this.numberOfSimulations + " simulations were completed" +  CommonConstants.HTML_FONT_NOR_END + CommonConstants.HTML_END);

        progressBar.setValue(numberOfSimulations);
        remainingTimeString = "Residual time: -- : --";
        //remainingTimeLabel.setText(remainingTimeString);
        stopAnimation();
        removeAnimation();
    }


    protected class Timer extends Thread {
        double lastTime;
        public Timer() {
            super("Progress timer");
            this.setPriority(Thread.MAX_PRIORITY);
        }

        public void setLastTime(double timeInSec) {
            lastTime = timeInSec;
        }

        public void run() {
            lastTime = ((double)System.currentTimeMillis())/1000 - globalStartTime;
            while (!stopped) {
                try {
                    sleep(1000);
                }
                catch(InterruptedException ie){
                   JOptionPane.showMessageDialog(null,"Unexpected progress timer termination");
                }
                if  (!paused) {
                    double time = ((double)System.currentTimeMillis())/1000 - globalStartTime;
                    elapsedTime += (time - lastTime);
                    lastTime = time;
                    updateTime();
                }
            }
        }
    }
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -