我对Java还是很陌生,因为几天来我一直在努力解决这个问题,这个问题可能很简单,但我确实无法解决。我有两个公共类,两个内部类都是一个外部类。在第一个中,我(从眼动仪设备)获得了一些数据。在第二篇中,我想将它们绘制在一张图像上。我正确地获取了数据并将其存储在arraylist中,但是当我在第二种方法中使用它们时,我得到了一个空的arraylist。这里的代码:
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));
}
}
}
换句话说,我要做的是使用GazeListener类的onGazeUpdate()方法检测数据x1和x2,然后能够在LoadImageApp类的paint(Graphics g)方法中使用此数据。 。
我也为此目的设置了set / get方法,但我仍然误会了一些东西。
我怎么了?
提前非常感谢您!
在LoadImageApp.paint
方法内部,您将创建一个新GazeListener
实例。您的新实例对您在中创建的实例一无所知main
。
解决此问题的方法是创建final GazeListener gazeListener = new GazeListener();
in main
,然后将其app
作为构造函数参数传递给LoadImageApp a= new LoadImageApp(gazeListener);
。然后,将其存储为字段,并从paint引用该字段,而不是在其中创建新实例paint
另外要注意的是,您将GazeListener和LoadImageApp称为“内部类”。它们不是内部类,而是静态成员类。如果您希望它们表现得像内部类,那么它们就不会。内部类使您可以访问父实例的字段。您的课程没有访问权限,因为它们具有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());
...
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句