JAVA:将值从一个 class 传递到另一个
JAVA: Passing values from one class to another
我是 Java 的新手,几天以来我一直在努力解决一个可能非常简单但我真的无法解决的问题。
我有两个 public classes,都是外部 classes 的内部。在第一个中,我获得了一些数据(来自眼动仪设备)。在第二个中,我想把它们画在图像上。我正确地获取了数据并将它们存储在一个数组列表中,但是当我在第二个方法中使用它们时,我得到一个空的数组列表。
这里的代码:
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));
}
}
}
换句话说,我想做的是用 class GazeListener 的 onGazeUpdate() 方法检测数据 x1 和 x2,然后能够在该方法中使用这些数据class LoadImageApp.
的 paint(Graphics g)
我也为此设置了 set/get 方法,但我还是搞错了。
我哪里错了?
非常感谢您!
在您的 LoadImageApp.paint
方法中,您创建了一个新的 GazeListener
实例。您的新实例对您在 main
.
中创建的实例一无所知
解决此问题的方法是在 main
中创建 final GazeListener gazeListener = new GazeListener();
,然后将其作为构造函数参数传递给 app
:LoadImageApp a= new LoadImageApp(gazeListener);
。然后,您将其存储为一个字段并从 paint 中引用该字段,而不是在 paint
中创建一个新实例
作为附加说明,您将 GazeListener 和 LoadImageApp 称为“inner 类”。他们不是内在的类,但static member classes. If you were hoping for them to behave as if they were inner classes他们不会。内部 类 使您可以访问父实例的字段。您的 类 没有获得该访问权限,因为他们具有 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());
...
}
}
在您的 paint 方法中,您创建了一个新的 GazeListener 实例并将其命名为 "a"。这个 "a" 是空的,因为它是一个新实例,您没有向它添加任何数据。
我是 Java 的新手,几天以来我一直在努力解决一个可能非常简单但我真的无法解决的问题。 我有两个 public classes,都是外部 classes 的内部。在第一个中,我获得了一些数据(来自眼动仪设备)。在第二个中,我想把它们画在图像上。我正确地获取了数据并将它们存储在一个数组列表中,但是当我在第二个方法中使用它们时,我得到一个空的数组列表。 这里的代码:
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));
}
}
}
换句话说,我想做的是用 class GazeListener 的 onGazeUpdate() 方法检测数据 x1 和 x2,然后能够在该方法中使用这些数据class LoadImageApp.
的 paint(Graphics g)我也为此设置了 set/get 方法,但我还是搞错了。
我哪里错了?
非常感谢您!
在您的 LoadImageApp.paint
方法中,您创建了一个新的 GazeListener
实例。您的新实例对您在 main
.
解决此问题的方法是在 main
中创建 final GazeListener gazeListener = new GazeListener();
,然后将其作为构造函数参数传递给 app
:LoadImageApp a= new LoadImageApp(gazeListener);
。然后,您将其存储为一个字段并从 paint 中引用该字段,而不是在 paint
作为附加说明,您将 GazeListener 和 LoadImageApp 称为“inner 类”。他们不是内在的类,但static member classes. If you were hoping for them to behave as if they were inner classes他们不会。内部 类 使您可以访问父实例的字段。您的 类 没有获得该访问权限,因为他们具有 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());
...
}
}
在您的 paint 方法中,您创建了一个新的 GazeListener 实例并将其命名为 "a"。这个 "a" 是空的,因为它是一个新实例,您没有向它添加任何数据。