2013-11-26 32 views
1

我正在开发在Windows 7上使用NetBeans 7.3.1上的Java SE。我编写了以下代码。正在初始化java.awt.geom.Point2D

import java.awt.geom.Point2D; 

static void setDisplayParams(Vector<Point2D> coords, double xMin, double xMax, double yMin, double yMax){ 
    Point2D newCoords, oldCoords; 
    Vector<Point2D> displayCoords = new Vector<Point2D>(); 

    for (int i=0; i<coords.size(); ++i){ 
       oldCoords=coords.elementAt(i); 
       newCoords.setLocation(oldCoords.getX(), yMax-oldCoords.getY()); 
       displayCoords.add(newCoords); 
    } 
} 

在生产线

newCoords.setLocation(oldCoords.getX(), yMax-oldCoords.getY()); 

我得到

variable newCoords might not have been initialzed 

我Google

java.awt.geom.Point2D initializing java 

和阅读here

消息
Point2D.Double() 

应该初始化一个java.awt.geom.Point2D变量。然而newCoords没有字段Double。

我的for循环开始

   for (int i=0; i<coords.size(); ++i){ 
       newCoords=coords.elementAt(i); 
       newCoords.setLocation(newCoords.getX(), yMax-newCoords.getY()); 
       displayParams.displayCoords.add(newCoords); 
      } 

这并没有给我任何错误消息,但它在我不想做COORDS改变的值。

+0

为了更好地提供帮助,请发布[SSCCE](http://sscce.org/)。 –

回答

2

您使用这样的静态引用。

for (int i=0; i<coords.size(); ++i){ 
    newCoords=coords.elementAt(i); 
    displayParams.displayCoords.add(new Point2D.Double(newCoords.getX(), yMax-newCoords.getY())); 
} 

这将创建一个新的Point2D并将newCoords(数组中的元素)对象保持不变。

+0

工作!非常感谢! – OtagoHarbour