【问题标题】:calling an array from classA to classB从类调用一个数组到类 B
【发布时间】:2014-05-01 23:13:11
【问题描述】:

当我重绘时,我想在paintComponent() 中使用来自snowBoarding 类的run_1 和run_2 数组。我想使用这些值用矩形制作条形图, 但我不知道如何将滑雪板中的值带入 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;
}
}

如果您能提供清晰的代码示例和描述,将不胜感激。

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

【问题讨论】:

    标签: java arrays class user-interface


    【解决方案1】:

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

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

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

    【讨论】:

    • 公共方法是否意味着不公开 void MyJPanel?
    • 不,它是一个构造函数,它默认返回类的类型,所以你不必也不能再次输入它。
    【解决方案2】:

    在创建 MyJpanel 期间将 SnowBoarding 类的对象引用传递给 MyJpanel 构造函数

    在您的单板滑雪课程中。

    所以

    MyJpanel should look like below
    
    class MyJPanel extends JPanel {
        SnowBoarding snowBoarding;
    
        public MyJPanel(SnowBoarding snowBoarding) {
            this.snowBoarding = snowBoarding;
        }
    //other code goes here
    }
    

    在您的单板滑雪课程中,

    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
    }
    

    【讨论】:

    • 所以我把 MyJpanel 面板放在哪里 = new MyJpanel(snowBoarding);修改后
    • 你已经在像这样 DrawPanel = new myJPanel();
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-25
    • 1970-01-01
    • 2020-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多