我想在重绘时使用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方法

最佳答案

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

class MyJPanel extends JPanel {
    SnowBoarding snowBoarding;

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


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

关于java - 从类到类B调用数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23418252/

10-10 02:30