简体   繁体   English

JAVA:将值从一类传递到另一类

[英]JAVA: Passing values from one class to another

I am pretty new with Java and I am struggling since some days on a problem that maybe is very simple but I am really not able to solve. 我对Java还是很陌生,因为几天来我一直在努力解决一个可能很简单的问题,但是我真的无法解决。 I have two public classes, both inner classes of an external one. 我有两个公共类,两个内部类都是一个外部类。 In the first one I get some data (from an eye tracker device). 在第一个中,我(从眼动仪设备)获得了一些数据。 In the second one, I would like to paint them on an image. 在第二篇中,我想将它们绘制在图像上。 I get correctly the data and store them in an arraylist, but when I go for using them in the method of the second one I get an empty arraylist. 我正确地获取了数据并将其存储在arraylist中,但是当我在第二种方法中使用它们时,我得到了一个空的arraylist。 Here the code: 这里的代码:

  import ...

public class TETSimple {
    //static LoadImageApp image = new LoadImageApp();

    public ArrayList x1;
    public ArrayList y1;

    public static void main(String[] args) {
        final GazeManager gm = GazeManager.getInstance();
        boolean success = gm.activate(ApiVersion.VERSION_1_0, ClientMode.PUSH);

        final GazeListener gazeListener = new GazeListener();
        gm.addGazeListener(gazeListener);

        JFrame f = new JFrame("Immagine");

        LoadImageApp a = new LoadImageApp();
        f.add(a);
        f.pack();
        f.setVisible(true);

        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                gm.removeGazeListener(gazeListener);
                gm.deactivate();
            }
        });
    }


    public static class GazeListener
        implements IGazeListener {

        private ArrayList<Double> x1 = new ArrayList<Double>();
        private ArrayList<Double> y1 = new ArrayList<Double>();

        public ArrayList getX1() {
            return this.x1;

        }

        public ArrayList getY1() {
            return this.y1;
        }

        public void setX1(ArrayList l1) {
            x1 = l1;
        }

        public void setY1(ArrayList l2) {
            x1 = l2;
        }

        @Override
        public void onGazeUpdate(GazeData gazeData) {
            Double xcor = gazeData.smoothedCoordinates.x;
            Double ycor = gazeData.smoothedCoordinates.y;

            x1.add(new Double(xcor));
            y1.add(new Double(ycor));

            //System.out.println(x1.toString()); --> it works and returns all the values detected and added into the array
        }
    }

    public static class LoadImageApp
        extends Component {
        BufferedImage img;

        public Integer x;
        public Integer y;

        public void paint(Graphics g) {
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            Double screenW = screenSize.getWidth();
            Double screenH = screenSize.getHeight() * 0.95;

            int screenWidth = screenW.intValue();
            int screenHeight = screenH.intValue();

            // Dimensioni dell'immagine
            if (img == null) {
                return;
            }
            int imageWidth = img.getWidth(this);
            int imageHeight = img.getHeight(this);
            g.drawImage(img, 0, 0, screenWidth, screenHeight, 0, 0, imageWidth, imageHeight, null);
            GazeListener a = new GazeListener();
            System.out.println(a.getX1().toString()); //--> doesn't work, return an empty array

            if (a.x1 != null && !a.x1.isEmpty() && a.y1 != null && !a.y1.isEmpty()) {
                Double currentx = a.x1.get(a.x1.size() - 1);
                Double currenty = a.y1.get(a.x1.size() - 1);
                System.out.println(currentx);
            } else {
                System.out.println("empty array");
            }
        }

        // costruttore della classe:
        public LoadImageApp() {
            try {
                img = ImageIO.read(new File("C:picture.jpg"));
            } catch (IOException e) {
            }
        }

        public Dimension getPreferredSize() {
            if (img == null) {
                return new Dimension(100, 100);
            } else {
                return new Dimension(img.getWidth(null), img.getHeight(null));
            }
        }
    }

In other words, what i would like to do is to detect the data x1 and x2 with the method onGazeUpdate() of the class GazeListener, and then to be able to use this data in the method paint(Graphics g) of the class LoadImageApp. 换句话说,我想做的是使用GazeListener类的onGazeUpdate()方法检测数据x1和x2,然后能够在LoadImageApp类的paint(Graphics g)方法中使用此数据。 。

I also set the set/get methods for this purpose but I still mistaking something. 我也为此目的设置了set / get方法,但我仍然犯了错误。

What am I getting wrong? 我怎么了?

Thank you very much in advance! 提前非常感谢您!

Inside your LoadImageApp.paint method you create a new GazeListener instance. LoadImageApp.paint方法内部,创建一个新的GazeListener实例。 Your new instance knows nothing about the one you created in main . 您的新实例对您在main创建的实例一无所知。

The way to solve this is to create your final GazeListener gazeListener = new GazeListener(); 解决此问题的方法是创建final GazeListener gazeListener = new GazeListener(); in main and then pass it to the app as a constructor argument: LoadImageApp a= new LoadImageApp(gazeListener); main ,然后将其作为构造函数参数传递给appLoadImageApp a= new LoadImageApp(gazeListener); . You then store that as a field and refer to the field from paint instead of creating a new instance inside paint 然后,将其存储为字段,并从paint引用该字段,而不是在paint内部创建新实例

As an additional note you called your GazeListener and LoadImageApp " inner classes ". 作为附加说明,您将GazeListener和LoadImageApp称为“ 内部类 ”。 They are not inner classes, but static member classes . 它们不是内部类,而是静态成员类 If you were hoping for them to behave as if they were inner classes they will not. 如果您希望它们表现得像内部类 ,则不会。 Inner classes give you access to the parent instance's fields. 内部类使您可以访问父实例的字段。 Your classes don't get that access as they have the static modifier. 您的类没有static访问权限,因为它们具有static修饰符。

public static class LoadImageApp
    extends Component {
    private GazeListener gazeListener;
    public LoadImageApp(GazeListener aGazeListener) {
        this.gazeListener = aGazeListener;
    }
    ...
    public void paint(Graphics g) {
        ...
        // don't do the next line
        //GazeListener a = new GazeListener();
        //System.out.println(a.getX1().toString()); //--> doesn't work, return an empty array
        System.out.println(this.gazeListener.getX1().toString()); 
        ...
    }
}

In your paint method you create a new instance of GazeListener and you call it "a". 在paint方法中,创建一个新的GazeListener实例,并将其称为“ a”。 This "a" is empty because it is a new instance and you don't add any data to it. 此“ a”为空,因为它是一个新实例,并且您没有向其添加任何数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM