从类到B类调用数组

时间:2014-05-01 23:13:12

标签: java arrays class user-interface

我想在重绘时使用paintComponent()中snowBoarding类的run_1和run_2数组。我想使用这些值从矩形中制作条形图, 但我无法弄清楚如何将snowBoarding的值带入myJPanel。

public class snowBoarding extends JFrame {
    public int[] run_1 = new int[6];
    public int[] run_2 = new int[6];
    private myJPanel DrawPanel = null;


    private myJPanel getDrawPanel() {
        if (DrawPanel == null) {
            DrawPanel = new myJPanel();
            DrawPanel.setLayout(new GridBagLayout());
            DrawPanel.setBounds(new Rectangle(258, 39, 326, 361));
            DrawPanel.setBackground(Color.white);
            DrawPanel.setEnabled(true);

            DrawPanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));

            //Instantiate the BufferedImage object and give it the same width 
            // and height as that of the drawing area JPanel
            img = new BufferedImage(DrawPanel.getWidth(), 
                                    DrawPanel.getHeight(), 
                                    BufferedImage.TYPE_INT_RGB);

            //Get its graphics context. A graphics context of a particular object allows us to draw on it.
            g2dImg = (Graphics2D)img.getGraphics();

            //Draw a filled white coloured rectangle on the entire area to clear it.
            g2dImg.setPaint(Color.WHITE);
            g2dImg.fill(new Rectangle2D.Double(0, 0, img.getWidth(), img.getHeight()));
        }
        return DrawPanel;
    }

    public JButton getButton_calc_draw() {
        if (Button_calc_draw == null) {
            Button_calc_draw = new JButton();
            Button_calc_draw.setBounds(303, 411, 131, 39);
            Button_calc_draw.setFont(new Font ("Dialog", Font.BOLD, 18));
            Button_calc_draw.setText("Draw");
            Button_calc_draw.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent e) {
                    // Get values from the text fields
                                            //I want these values from the array//
                    run_1[0] = Integer.parseInt(textField.getText());
                    run_1[1] = Integer.parseInt(textField_1.getText());
                    run_1[2] = Integer.parseInt(textField_2.getText());
                    run_1[3] = Integer.parseInt(textField_3.getText());
                    run_1[4] = Integer.parseInt(textField_4.getText());
                    run_1[5] = Integer.parseInt(textField_5.getText());

                    for (int i = 0; i < run_1.length; i++) {
                        temp[i] = run_1[i];
                    }
                    Arrays.sort(temp);

                    for (int i = 1; i < (temp.length -1) ; i++){
                        avg1+=temp[i];
                    }

                    avg1 = avg1/4;

                    //and these as well
                    run_2[0] = Integer.parseInt(textField_6.getText());
                    run_2[1] = Integer.parseInt(textField_7.getText());
                    run_2[2] = Integer.parseInt(textField_8.getText());
                    run_2[3] = Integer.parseInt(textField_9.getText());
                    run_2[4] = Integer.parseInt(textField_10.getText());
                    run_2[5] = Integer.parseInt(textField_11.getText());

                    for (int i = 0; i < run_2.length; i++) {
                        temp[i] = run_2[i];
                    }
                    Arrays.sort(temp);

                    for (int i = 1; i < (temp.length -1) ; i++){
                        avg2+=temp[i];
                    }

                    avg2 = avg2/4;


                    if (avg1 > avg2){
                        OverallScore = avg1;
                    }
                    else {
                        OverallScore = avg2;
                    }


                    total_1.setText(Integer.toString(avg1));
                    total_2.setText(Integer.toString(avg2));
                    Overall.setText(Integer.toString(OverallScore));
                    DrawPanel.setShallPaint(true);
                    DrawPanel.repaint();
                    }

                 ;
            });


class myJPanel extends JPanel {
    SnowBoarding snowBoarding;

    public void MyJPanel(SnowBoarding snowBoarding) {
    this.snowBoarding = snowBoarding;
}


private static final long serialVersionUID = 1L;
private Rectangle2D.Double rectangle;
int[] score = new int[12];
// placed into this array
/* score[0] = run_1[0];
score[1] = run_1[1];
score[2] = run_1[2];
score[3] = run_1[3];
score[4] = run_1[4];
score[5] = run_1[5];
score[6] = run_2[0];
score[7] = run_2[1];
score[8] = run_2[2];
score[9] = run_2[3];
score[10] = run_2[4];
score[11] = run_2[5];
*/



/*
for (i = 0; i < run_1.length, i++){
    score[i] = run_1[i];
}
for (i = 0; i < run_2.length, i++){
    score[i+6] = run_2[i];
}
*/


private boolean shallPaint = false;

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    if (shallPaint) {

        Graphics2D g2D = (Graphics2D) g;
        rectangle = new Rectangle2D.Double(0, 350-score[1] * 2, 25, score[1] * 2);
        g2D.setPaint(Color.blue); 
        g2D.fill(rectangle);
        g2D.draw(rectangle);              
    }
}

public void setShallPaint(boolean pShallPaint) {
    shallPaint  = pShallPaint;
}
}

如果您能够回答清楚的代码示例,并提供非常感谢的说明。

编辑将myJpanel DrawPanel = null行放入。 和DrawPanel方法

2 个答案:

答案 0 :(得分:1)

将引用传递给snowBoarding类对象(请写上以大写字母开头的类名)

class MyJPanel extends JPanel {
    SnowBoarding snowBoarding;

    public MyJPanel(SnowBoarding snowBoarding) {
        this.snowBoarding = snowBoarding;
    }

然后在paintComponent方法中你可以简单地使用这些数组     snowBoarding.run_1

答案 1 :(得分:0)

在创建MyJpanel期间将SnowBoarding类的对象引用传递给MyJpanel constrcutor

在SnowBoarding课程中。

所以

MyJpanel should look like below

class MyJPanel extends JPanel {
    SnowBoarding snowBoarding;

    public MyJPanel(SnowBoarding snowBoarding) {
        this.snowBoarding = snowBoarding;
    }
//other code goes here
}

并在SnowBoarding课程中,

MyJpanel panel = new MyJpanel(snowBoarding);

其中snowBoarding是保存这两个数组的对象。

现在访问

中的run1和run2
public void paintComponent(Graphics g) {
snowBoarding.run1 // use it
snowBoarding.run2 //use it
rest of the code
}