標籤

code.google.com

2013年4月6日 星期六

Java亂數抽籤程式


在很多場合都會用到抽籤功能,傳統的有用竹筷子上面寫數字放入籤筒裡來取得,還有用紙張寫數字折起來來取出需求數字,不過這些方法都可能會有人為的干擾,最好的方式還是交由程式來取得亂數,當做抽籤結果,以求公平。
這個範例是利用Java亂數來製作的一個抽籤程式,可以選擇是否取數時可允許重覆的數值取得。畫面如下:
RndApp1.jpg
程式碼如下:



package rndApp;
 
import javax.swing.JPanel;
import javax.swing.JFrame;
 
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Toolkit;
 
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JList;
import javax.swing.JTextField;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JCheckBox;
import java.awt.event.KeyEvent;
import javax.swing.BorderFactory;
import javax.swing.JFormattedTextField;
import javax.swing.SwingConstants;
 
public class RndFrame extends JFrame {
 
    private static final long serialVersionUID = 1L;
 
    private JPanel jContentPane = null;
 
    private JScrollPane jScrollPane = null;
 
    private JTextArea jTextArea = null;
 
    private JList jList = null;
 
    private JTextField rndText = null;
 
    private JButton jButton = null;
 
    private JButton jButton1 = null;
 
    private JLabel jLabel = null;
 
    private JCheckBox jCheckBox = null;
 
    private JFormattedTextField jFormattedTextField = null;
 
    // 最大數
    private int maxNum = 50;
    // 是否重新取數
    private boolean reRnd = false;
    // 目前取到的數count
    private int count = 0;
    // 存放值
    private int[] Num = null;
    // 亂數種子
    private java.util.Random r = new java.util.Random(); // @jve:decl-index=0:
 
    private JLabel jLabel1 = null;
 
    private JLabel jLabel2 = null;
 
    private JLabel label_max = null;
 
    private JLabel jLabel4 = null;
 
    private JLabel label_rernd = null;
 
    private JButton jButton2 = null;
 
    /**
     * This is the default constructor
     */
    public RndFrame() {
        super();
        initialize();
    }
 
    private void setMaxNum(int maxNum) {
        this.maxNum = maxNum;
    }
 
    private void setReRnd(boolean b) {
        this.reRnd = b;
    }
 
    private void initNum() {
        count = 0;
        Num = new int[maxNum];
        for (int i = 0; i < maxNum; i++) {
            Num[i] = i + 1;
        }
    }
 
    public void setRndText() {
        if (maxNum - count > 0) {
            int rnd = r.nextInt(maxNum - count);
            String str = String.valueOf(Num[rnd]);
            this.getRndText().setText(str);
            this.getJTextArea().append(str + "\n");
 
            if (reRnd == false) {
                // 如果是不能重覆的話,那把值跟最後一個最交換,
                // 然後設定count++不再取到它
                int tmp = Num[rnd];
                Num[rnd] = Num[maxNum - 1 - count];
                Num[maxNum - 1 - count] = tmp;
                count++;
            }
 
        }
    }
 
    public void setReset() {
        this.setMaxNum(Integer
                .parseInt(this.getJFormattedTextField().getText()));
 
        initNum();
        this.setReRnd(this.jCheckBox.isSelected());
        // 元件回復狀況
        this.getJTextArea().setText("");
        this.label_max.setText(this.getJFormattedTextField().getText());
        this.label_rernd.setText(this.jCheckBox.isSelected() == true ? "是"
                : "否");
    }
 
    /**
     * This method initializes this
     * 
     * @return void
     */
    private void initialize() {
        initNum();
        this.setSize(400, 300);
        this.setName("jFrame");
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setContentPane(getJContentPane());
        this.setTitle("抽籤程式");
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        this.setLocation(screenSize.width / 2 - this.getWidth() / 2,
                screenSize.height / 2 - this.getHeight() / 2);
        this.setVisible(true);
    }
 
    /**
     * This method initializes jContentPane
     * 
     * @return javax.swing.JPanel
     */
    private JPanel getJContentPane() {
        if (jContentPane == null) {
            label_rernd = new JLabel();
            label_rernd.setBounds(new Rectangle(58, 50, 45, 18));
            label_rernd.setHorizontalTextPosition(SwingConstants.TRAILING);
            label_rernd.setHorizontalAlignment(SwingConstants.CENTER);
            label_rernd.setText("否");
            jLabel4 = new JLabel();
            jLabel4.setBounds(new Rectangle(3, 50, 55, 18));
            jLabel4.setText("重覆取數");
            label_max = new JLabel();
            label_max.setBounds(new Rectangle(58, 30, 44, 16));
            label_max.setHorizontalAlignment(SwingConstants.CENTER);
            label_max.setText("50");
            jLabel2 = new JLabel();
            jLabel2.setBounds(new Rectangle(2, 30, 60, 16));
            jLabel2.setText("最大人數");
            jLabel1 = new JLabel();
            jLabel1.setBounds(new Rectangle(2, 10, 68, 16));
            jLabel1.setText("目前設定值");
            jLabel = new JLabel();
            jLabel.setBounds(new Rectangle(18, 123, 127, 33));
            jLabel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
            jLabel.setFont(new Font("Dialog", Font.BOLD, 24));
            jLabel.setDisplayedMnemonic(KeyEvent.VK_UNDEFINED);
            jLabel.setText("最大人數:");
            jContentPane = new JPanel();
            jContentPane.setLayout(null);
            // jContentPane.setBorder(BorderFactory.createEmptyBorder(0, 0, 0,
            // 0));
            jContentPane.add(getJScrollPane(), null);
            jContentPane.add(getJList(), null);
            jContentPane.add(getRndText(), null);
            jContentPane.add(getJButton(), null);
            jContentPane.add(getJButton1(), null);
            jContentPane.add(jLabel, null);
            jContentPane.add(getJCheckBox(), null);
            jContentPane.add(getJFormattedTextField(), null);
            jContentPane.add(jLabel1, null);
            jContentPane.add(jLabel2, null);
            jContentPane.add(label_max, null);
            jContentPane.add(jLabel4, null);
            jContentPane.add(label_rernd, null);
            jContentPane.add(getJButton2(), null);
        }
        return jContentPane;
    }
 
    /**
     * This method initializes jScrollPane
     * 
     * @return javax.swing.JScrollPane
     */
    private JScrollPane getJScrollPane() {
        if (jScrollPane == null) {
            jScrollPane = new JScrollPane();
            jScrollPane.setBounds(new Rectangle(308, 2, 82, 264));
            jScrollPane.setViewportView(getJTextArea());
        }
        return jScrollPane;
    }
 
    /**
     * This method initializes jTextArea
     * 
     * @return javax.swing.JTextArea
     */
    private JTextArea getJTextArea() {
        if (jTextArea == null) {
            jTextArea = new JTextArea();
        }
        return jTextArea;
    }
 
    /**
     * This method initializes jList
     * 
     * @return javax.swing.JList
     */
    private JList getJList() {
        if (jList == null) {
            jList = new JList();
            jList.setBounds(new Rectangle(5, 101, 302, 8));
        }
        return jList;
    }
 
    /**
     * This method initializes rndText
     * 
     * @return javax.swing.JTextField
     */
    private JTextField getRndText() {
        if (rndText == null) {
            rndText = new JTextField();
            rndText.setBounds(new Rectangle(103, 8, 112, 88));
            rndText.setFont(new Font("Dialog", Font.PLAIN, 66));
            rndText.setHorizontalAlignment(JTextField.CENTER);
            rndText.setText("");
        }
        return rndText;
    }
 
    /**
     * This method initializes jButton
     * 
     * @return javax.swing.JButton
     */
    private JButton getJButton() {
        if (jButton == null) {
            jButton = new JButton();
            jButton.setBounds(new Rectangle(215, 64, 90, 32));
            jButton.setText("取數");
            jButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent e) {
                    RndFrame frame = ((RndFrame) ((JButton) e.getSource())
                            .getTopLevelAncestor());
                    frame.setRndText();
                }
            });
        }
        return jButton;
    }
 
    /**
     * This method initializes jButton1
     * 
     * @return javax.swing.JButton
     */
    private JButton getJButton1() {
        if (jButton1 == null) {
            jButton1 = new JButton();
            jButton1.setBounds(new Rectangle(57, 193, 90, 35));
            jButton1.setText("設定");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent e) {
                    RndFrame frame = ((RndFrame) ((JButton) e.getSource())
                            .getTopLevelAncestor());
                    frame.setReset();
                }
            });
        }
        return jButton1;
    }
 
    /**
     * This method initializes jCheckBox
     * 
     * @return javax.swing.JCheckBox
     */
    private JCheckBox getJCheckBox() {
        if (jCheckBox == null) {
            jCheckBox = new JCheckBox();
            jCheckBox.setBounds(new Rectangle(18, 159, 237, 30));
            jCheckBox.setFont(new Font("Dialog", Font.BOLD, 24));
            jCheckBox.setText("是否可以重覆取數");
        }
        return jCheckBox;
    }
 
    /**
     * This method initializes jFormattedTextField
     * 
     * @return javax.swing.JFormattedTextField
     */
    private JFormattedTextField getJFormattedTextField() {
        if (jFormattedTextField == null) {
            javax.swing.text.NumberFormatter mf2 = null;
            mf2 = new javax.swing.text.NumberFormatter();
            mf2.setMaximum(new Integer(500));
            mf2.setMinimum(new Integer(0));
            jFormattedTextField = new JFormattedTextField(mf2);
            jFormattedTextField.setBounds(new Rectangle(151, 123, 119, 33));
            jFormattedTextField.setHorizontalAlignment(JTextField.CENTER);
            jFormattedTextField.setText("50");
            jFormattedTextField.setFont(new Font("Dialog", Font.PLAIN, 24));
        }
        return jFormattedTextField;
    }
 
    /**
     * This method initializes jButton2
     * 
     * @return javax.swing.JButton
     */
    private JButton getJButton2() {
        if (jButton2 == null) {
            jButton2 = new JButton();
            jButton2.setBounds(new Rectangle(229, 237, 76, 24));
            jButton2.setText("關於");
            jButton2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent e) {
                    RndFrame frame = ((RndFrame) ((JButton) e.getSource())
                            .getTopLevelAncestor());
                    javax.swing.JOptionPane.showMessageDialog(frame,
                            "blog.yslifes.com\r\nPower by yku");
                }
            });
        }
        return jButton2;
    }
 
    public static void main(String[] args) {
        new RndFrame();
    }
} // @jve:decl-index=0:visual-constraint="10,10"
程式碼下載:
Jar Run程式,可直接執行
用法說明:
下方可設定取數的範圍為1到某個數中間(最大人數),取數過程是否可取到重覆的數字,選擇好後按下”設定”按鈕可以看到左上角設定值已被更改,再來按下按鈕”取數”可於中間TextField看到取到的數字為何,而右側會顯示歷史記錄。
RndApp2.jpg
ps.其實這程式的亂數還有很大的改進空間,有興趣的朋友可以自己再做些修改。

沒有留言:

張貼留言