仅使用Java中的JPanel创建条形图

时间:2014-04-22 11:38:34

标签: java swing charts panel

所以我有一个带有2x6文本字段的界面来输入数据,我想在JPanel上将它绘制为6组2列。

按下" Draw"按钮,2x6文本字段被放入2个6项的数组。

我不确定如何创建这些酒吧。

我在考虑宽度的常数,取其高度值(0-100)。

至于位置,他们的x应该是我认为的JPanel高度的负数。 他们的y会增加吗?因此,绘制的每个条形图将y增加40左右?

编辑:基本上我想把我的两个数组的值表示为矩形。

仅供参考,这里是源代码:

//Arrays to store the data in to make working much easier
int[] iaRun1 = new int[6];
int[] iaRun2 = new int[6];

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Snowboarding window = new Snowboarding_PartA();
                window.frmUSnowboarding.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public Snowboarding() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {     
    final JPanel myJPanel = new JPanel();
    myJPanel.setBounds(248, 110, 604, 318);
    myJPanel.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
    myJPanel.setBackground(Color.WHITE);
    frmUSnowboarding.getContentPane().add(myJPanel);

    JButton jBtnDraw = new JButton("Draw");
    jBtnDraw.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            /*
             * The following code is a bit icky, but for the size of the
             * data we are working with, it works just fine.
             * For some reason I just couldn't get a loop
             * to work with storing the data into my array
             */

            //Store my run 1 values into my array
            iaRun1[0] = Integer.parseInt(jTextFieldRun1Judge1.getText());
            iaRun1[1] = Integer.parseInt(jTextFieldRun1Judge2.getText());
            iaRun1[2] = Integer.parseInt(jTextFieldRun1Judge3.getText());
            iaRun1[3] = Integer.parseInt(jTextFieldRun1Judge4.getText());
            iaRun1[4] = Integer.parseInt(jTextFieldRun1Judge5.getText());
            iaRun1[5] = Integer.parseInt(jTextFieldRun1Judge6.getText());

            //Store my run 2 values into my array
            iaRun2[0] = Integer.parseInt(jTextFieldRun2Judge1.getText());
            iaRun2[1] = Integer.parseInt(jTextFieldRun2Judge2.getText());
            iaRun2[2] = Integer.parseInt(jTextFieldRun2Judge3.getText());
            iaRun2[3] = Integer.parseInt(jTextFieldRun2Judge4.getText());
            iaRun2[4] = Integer.parseInt(jTextFieldRun2Judge5.getText());
            iaRun2[5] = Integer.parseInt(jTextFieldRun2Judge6.getText());

            //Initialise for following loops
            int iMin1 = Integer.MAX_VALUE;
            int iMax1 = Integer.MIN_VALUE;
            int iMin2 = Integer.MAX_VALUE;
            int iMax2 = Integer.MIN_VALUE;

            //Find the run 1 max and min
            for (int i = 0; i <iaRun1.length; i++) {
                iMin1 = Math.min(iMin1, iaRun1[i]);
                iMax1 = Math.max(iMax1, iaRun1[i]);
            }

            //Find the run 2 max and min
            for (int i = 0; i < iaRun2.length; i++) {
                iMin2 = Math.min(iMin2, iaRun2[i]);
                iMax2 = Math.max(iMax2, iaRun2[i]);
            }               

            //Initialise some values for totals
            int iRun1Total = 0;
            int iRun2Total = 0;

            //Find totals with a simple loop (both arrays in one loop yeah!)
            for (int i = 0; i < iaRun1.length; i++) {
                iRun1Total += iaRun1[i];
                iRun2Total += iaRun2[i];
            }

            //Set the totals to their corresponding text fields.
            jTextFieldRun1Total.setText(Integer.toString(iRun1Total));
            jTextFieldRun2Total.setText(Integer.toString(iRun2Total));

            //Now I have the totals, the averages should be relatively simple?
            //Initialise some values for averages (less outlying scores)
            int iRun1Avg = 0;
            int iRun2Avg = 0;

            //Simple arithmatic for the average
            iRun1Avg = ((iRun1Total - iMin1 - iMax1)/4);
            iRun2Avg = ((iRun2Total - iMin2 - iMax2)/4);

            //Set averages to corresponding fields
            jTextFieldRun1Avg.setText(Integer.toString(iRun1Avg));
            jTextFieldRun2Avg.setText(Integer.toString(iRun2Avg));  

            //Finally set athletes final score (whichever average is highest)
            if (iRun1Avg > iRun2Avg) {
                jTextFieldFinalScore.setText(Integer.toString(iRun1Avg));
            }
            else {
                jTextFieldFinalScore.setText(Integer.toString(iRun2Avg));
            }

        }
    });
    jBtnDraw.setBounds(282, 509, 160, 64);
    frmUSnowboarding.getContentPane().add(jBtnDraw);

0 个答案:

没有答案