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

📄 distributionseditor.java

📁 一个用于排队系统仿真的开源软件,有非常形象的图象仿真过程!
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     * current distribution type is changed.
     */
    protected ItemListener change_listener = new ItemListener() {
        public void itemStateChanged(ItemEvent e) {
            try {
                current = (Distribution)((Class) distributions.get(e.getItem())).newInstance();
                refreshView();
            } catch (InstantiationException ex) {
                System.out.println("Error: Error instantiating selected Distribution");
                ex.printStackTrace();
            } catch (IllegalAccessException ex) {
                System.out.println("Error: Error accessing to selected Distribution");
                ex.printStackTrace();
            }
        }
    };
// -------------------------------------------------------------------------------------------------

// --- Initialize data structure and layout --------------------------------------------------------
    /**
     * Initialize this dialog data structures
     * @param initial Reference to initial distribution to be shown
     */
    protected void initData(Distribution initial) {
        this.initial = initial;
        this.target = initial;
        if (initial != null)
            this.current = (Distribution)initial.clone();
        else
            this.current = new Exponential(); // Default distribution if nothing is selected

        // If distributions is not already set, sets it!
        if (distributions == null)
            distributions = findDistributions();
    }

    /**
     * Initialize this dialod's components and default dialog property
     */
    protected void initComponents() {
        // Sets default title, close operation and dimensions
        this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        this.setTitle("Editing Distribution...");
        int width = 320, height=400;

        // Centers this dialog on the screen
        Dimension scrDim = Toolkit.getDefaultToolkit().getScreenSize();
        this.setBounds((scrDim.width-width)/2,(scrDim.height-height)/2,width,height);


        // Creates a main panel and adds margins to it
        JPanel mainpanel = new JPanel(new BorderLayout());
        mainpanel.setLayout(new BorderLayout());
        mainpanel.add(Box.createVerticalStrut(BORDERSIZE), BorderLayout.NORTH);
        mainpanel.add(Box.createVerticalStrut(BORDERSIZE), BorderLayout.SOUTH);
        mainpanel.add(Box.createHorizontalStrut(BORDERSIZE), BorderLayout.WEST);
        mainpanel.add(Box.createHorizontalStrut(BORDERSIZE), BorderLayout.EAST);
        this.getContentPane().add(mainpanel, BorderLayout.CENTER);

        // Creates a subpanel that holds scrolledpanel and distr_panel and adds it to mainpanel
        JPanel subpanel = new JPanel(new BorderLayout());
        mainpanel.add(subpanel, BorderLayout.CENTER);
        JPanel distr_panel = new JPanel(new BorderLayout());
        subpanel.add(distr_panel, BorderLayout.NORTH);

        // Creates scrolledpanel that holds param_panel and mean_c_panel
        JPanel scrolledpanel = new JPanel(new GridLayout(2,1));
        subpanel.add(new JScrollPane(scrolledpanel), BorderLayout.CENTER);
        scrolledpanel.add(param_panel);
        scrolledpanel.add(mean_c_panel);
        mean_c_panel.setBorder(BorderFactory.createMatteBorder(1,0,0,0,Color.gray));

        // Adds bottom_panel to contentpane
        JPanel bottom_panel = new JPanel(new FlowLayout());
        this.getContentPane().add((bottom_panel), BorderLayout.SOUTH);

        // Adds Okay button to bottom_panel
        JButton okaybutton = new JButton(okayAction);
        bottom_panel.add(okaybutton);

        // Adds Cancel button to bottom_panel
        JButton cancelbutton = new JButton(cancelAction);
        bottom_panel.add(cancelbutton);

        // Adds distribution chooser
        distr_panel.add(new JLabel("Selected Distribution: "), BorderLayout.WEST);
        Object[] distributionNames = distributions.keySet().toArray();
        Arrays.sort(distributionNames); // Sorts alphabetically distribution names
        choser = new JComboBox(distributionNames);
        choser.setToolTipText("Choose distribution type");
        // Select correct distribution
        if (current != null) {
            choser.setSelectedItem(current.getName());
            refreshView();
        }
        choser.addItemListener(change_listener);
        distr_panel.add(choser, BorderLayout.CENTER);

        // Adds image viewer with a couple of borders
        JPanel image_panel = new JPanel (new BorderLayout());
        distr_panel.add(image_panel, BorderLayout.SOUTH);
        image_panel.add(Box.createVerticalStrut(BORDERSIZE / 2), BorderLayout.NORTH);
        image_panel.add(Box.createVerticalStrut(BORDERSIZE / 2), BorderLayout.SOUTH);
        image_panel.add(iconpanel, BorderLayout.CENTER);
    }
// -------------------------------------------------------------------------------------------------

// --- Shows current distribution ------------------------------------------------------------------
    protected void refreshView() {
        if (current != null) {
            // Flushes param_panel
            param_panel.removeAll();
            mean_c_panel.removeAll();
            // Shows image
            iconpanel.setImage(current.getImage());
            // Maximum width (used to line up elements of both panels)
            int maxwidth = new JLabel("mean:", JLabel.TRAILING).getMinimumSize().width;

            // Shows this distribution's parameters on param_panel
            JLabel label;
            JTextField textfield;
            for (int i=0; i<current.getNumberOfParameters(); i++) {
                // Creates the label
                label = new JLabel(current.getParameter(i).getDescription() + ":", JLabel.TRAILING);
                // Corrects maxwidth if needed
                if (maxwidth < label.getMinimumSize().width)
                    maxwidth = label.getMinimumSize().width;
                param_panel.add(label, new SpringLayout.Constraints(Spring.constant(0), Spring.constant(0),
                        Spring.constant(maxwidth), Spring.constant(label.getMinimumSize().height)));
                // Creates the fextfield used to input values
                textfield = new JTextField(5);
                textfield.setMaximumSize(new Dimension(textfield.getMaximumSize().width,
                                                    textfield.getMinimumSize().height));
                label.setLabelFor(textfield);
                textfield.setName(Integer.toString(i));
                textfield.addFocusListener(parameterListener);
                textfield.addKeyListener(parameterListener);
                param_panel.add(textfield);
            }
            SpringUtilities.makeCompactGrid(param_panel,
                                            current.getNumberOfParameters(), 2, //rows, cols
                                            6, 6,                               //initX, initY
                                            6, 6);                              //xPad, yPad

            // Now shows mean and c (if applicable) on mean_c_panel
            if (current.hasC() || current.hasMean()) {
                int rows = 0;
                mean_c_panel.setVisible(true);
                // Builds mean section
                if (current.hasMean()) {
                    rows++;
                    // Creates the label
                    label = new JLabel("mean:", JLabel.TRAILING);
                    mean_c_panel.add(label, new SpringLayout.Constraints(Spring.constant(0), Spring.constant(0),
                        Spring.constant(maxwidth), Spring.constant(label.getMinimumSize().height)));
                    // Creates the fextfield used to input mean values
                    textfield = new JTextField(5);
                    textfield.setMaximumSize(new Dimension(textfield.getMaximumSize().width,
                                                        textfield.getMinimumSize().height));
                    label.setLabelFor(textfield);
                    textfield.setName("mean");
                    textfield.addFocusListener(cmListener);
                    textfield.addKeyListener(cmListener);
                    mean_c_panel.add(textfield);
                }

                // Builds c section
                if (current.hasC()) {
                    rows++;
                    // Creates the label
                    label = new JLabel("c:", JLabel.TRAILING);
                    mean_c_panel.add(label, new SpringLayout.Constraints(Spring.constant(0), Spring.constant(0),
                        Spring.constant(maxwidth), Spring.constant(label.getMinimumSize().height)));
                    // Creates the fextfield used to input mean values
                    textfield = new JTextField(5);
                    textfield.setMaximumSize(new Dimension(textfield.getMaximumSize().width,
                                                        textfield.getMinimumSize().height));
                    label.setLabelFor(textfield);
                    textfield.setName("c");
                    textfield.addFocusListener(cmListener);
                    textfield.addKeyListener(cmListener);
                    mean_c_panel.add(textfield);
                }
                SpringUtilities.makeCompactGrid(mean_c_panel,
                                                rows, 2,                            //rows, cols
                                                6, 6,                               //initX, initY
                                                6, 6);                              //xPad, yPad
            }
            else
                mean_c_panel.setVisible(false);
            // Sets text for values
            refreshValues();
            param_panel.getParent().repaint();
        }
    }

    protected void refreshValues() {
        // refresh all values into param_panel
        Component[] components = param_panel.getComponents();
        // Formatter to show only 12 decimal digits to avoid machine-epsilon problems
        DecimalFormat df = new DecimalFormat("#.############");
        df.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.ENGLISH));
        int num;
        for (int i=0; i<components.length; i++) {
            if (components[i] instanceof JTextField) {
                num = Integer.parseInt(components[i].getName());
                Object value = current.getParameter(num).getValue();
                if (value instanceof Double) {
                    double val = ((Double) value).doubleValue();
                    ((JTextField)components[i]).setText(df.format(val));
                }
                else
                    ((JTextField)components[i]).setText(value.toString());
            }
        }
        // refresh all values into mean_c_panel
        components = mean_c_panel.getComponents();
        for (int i=0; i<components.length; i++) {
            // Shows only first 10 decimal digits
            if (components[i] instanceof JTextField && components[i].getName().equals("mean")) {
                ((JTextField)components[i]).setText(df.format(current.getMean()));
            } else if (components[i] instanceof JTextField && components[i].getName().equals("c")) {
                ((JTextField)components[i]).setText(df.format(current.getC()));
            }
        }
        param_panel.getParent().repaint();
    }
// -------------------------------------------------------------------------------------------------
}

⌨️ 快捷键说明

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