外类无法访问方法的局部变量

时间:2018-05-05 00:21:47

标签: java

我使用paintComponent方法在JPanel中绘制不同的形状,并计算每种形状(即矩形和椭圆形)的迭代次数。我希望我的实例变量能够访问在paintComponent方法中递增的形状计数变量,但无法弄清楚为什么实例变量“countRectangle1”没有复制方法的局部变量“countRectangle” -

package ch8ex8p1;

import java.security.SecureRandom;
import java.awt.Graphics;
import java.awt.Color;
import javax.swing.JPanel;

public class DrawShape extends JPanel
{
private SecureRandom randomNumbers = new SecureRandom();
private int x;
private int y;
private int width;
private int height;
private Color color;
private int countRectangle1; //this will copy countRectangle after iteration 
private int countOval1;


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

    int width = getWidth(); //total width
    int height = getHeight(); //total height
    int coordinate1;
    int coordinate2;
    int countRectangle = 0; //counts number of times a Rectangle is drawn


    for (int counter = 0; counter <=10; counter++ )
    {
        //generate numbers to use for x and width
        coordinate1 = randomNumbers.nextInt(width);
        coordinate2 = randomNumbers.nextInt(width);

        while (coordinate1 == coordinate2)
            coordinate2 = randomNumbers.nextInt(width);

        //identify x and width
        if (coordinate1 > coordinate2)
        {
            this.width = coordinate1;
            this.x = coordinate2;
        }
        else
        {
            this.width = coordinate2;
            this.x = coordinate1;
        }

        //generate numbers to use for y and height
        coordinate1 = randomNumbers.nextInt(height);
        coordinate2 = randomNumbers.nextInt(height);
        while (coordinate1 == coordinate2)
            coordinate2 = randomNumbers.nextInt(height);

        //identify y and height
        if (coordinate1 > coordinate2)
        {
            this.height = coordinate1;
            this.y = coordinate2;
        }
        else
        {
            this.height = coordinate2;
            this.y = coordinate1;
        }

        //generate random color for the shapes
        Color color = new Color(randomNumbers.nextInt(256), randomNumbers.nextInt(256),
                randomNumbers.nextInt(256));
        this.color = color;

        setBackground(Color.BLACK);

        //draw random shape
        int drawFlag = randomNumbers.nextInt(2);
        if (drawFlag==0)
        {
            MyRect myRectangle = new MyRect(this.x, this.y, this.width, this.height
                    , this.color);
            myRectangle.draw(g);
            setCount(1);
            countRectangle++;
        }
        else
        {
            MyOval myOval = new MyOval(this.x, this.y, this.width, this.height
                    , this.color);
            myOval.draw(g);

        }

    }//end for loop


    //assign count to outer variable
    this.countRectangle1 = countRectangle;
    //setCount(countRectangle);
    System.out.print("\ncount Rectangle: " + countRectangle1);

}//end painComponent

public void setCount(int count)
{
    countRectangle1 = countRectangle1 + count;
}

public int getCount()
{
    return countRectangle1;
}
}//end class

我尝试过使用setter和getter但它不起作用。我也尝试将类的变量更改为“protected”,但它仍然不起作用。似乎当paintComponent完成时,计数重置为0.我在分配值时向类变量添加了“this”,但它仍然没有得到正确的输出。在paintComponent中,它似乎得到了计数但是当它在paintComponent之外时,它变为0。

这是调用count的类,但是得到0:

import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.*;

public class DrawShapeTest extends JFrame 
{
public static void main(String[] args)
{
    DrawShape shape = new DrawShape();
    JFrame app = new JFrame();
    app.setSize(400, 400);

    JLabel rectangleLabel = new JLabel();
    //rectangleLabel.setText("aaaarrrrrgghhhh!!!!!" + shape.getCount());
    app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    shape.setSize(300, 300);
    System.out.println("\nsecond print Rectangle: " + shape.getCount());
    app.setLayout(new BorderLayout());
    app.add(shape);
    app.add(rectangleLabel, BorderLayout.SOUTH);
    app.setVisible(true);
}
}

感谢任何帮助。矩形计数的假定第二个打印行显示0,实际上是在Draw Shape Class(生成形状)下的for循环之前打印所以看起来我的代码不按顺序排列,我现在还在尝试搞清楚。

1 个答案:

答案 0 :(得分:0)

System.out.println("\nsecond print Rectangle: " + shape.getCount());

之后致电app.setVisible(true);

问题是您在分配值之前访问变量,因为函数paintComponent()在开始绘制之前不会运行。 (可见)

修改 要启动JFrame应用,请将其放入主方法

java.awt.EventQueue.invokeLater(new Runnable() {
      public void run() {
           JFrame frame = new JFrame();
             //bla bla....
           frame.setVisible(true);
      }
});