简体   繁体   中英

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. 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. 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.

I also set the set/get methods for this purpose but I still mistaking something.

What am I getting wrong?

Thank you very much in advance!

Inside your LoadImageApp.paint method you create a new GazeListener instance. Your new instance knows nothing about the one you created in main .

The way to solve this is to create your final GazeListener gazeListener = new GazeListener(); in main and then pass it to the app as a constructor argument: LoadImageApp 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

As an additional note you called your GazeListener and LoadImageApp " inner classes ". 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.

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". This "a" is empty because it is a new instance and you don't add any data to it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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