JNI jintArray的第一个值不正确

时间:2016-04-25 14:21:38

标签: java c++ arrays java-native-interface

我尝试做的是将Java中的多边形绘制到直接的2d c ++窗口。我有2个数组包含多边形的x点和y点。打印数组时,除了两个数组中的第一个元素外,数组显示正确。

在Java方面:

Polygon fillPoly = new Polygon(new int[] {200, 250, 300}, new int[] {400, 350, 400}, 3);
// 200, 400 | 250, 350 | 300, 400

g.fillPolygon(fillPoly);

//...

public void fillPolygon(final Polygon polygon) {
    fillPoly(polygon.xpoints, polygon.ypoints, polygon.npoints);
}

private native void fillPoly(final int[] xpoints, final int ypoints[], final int numPoints);

但是data字符串的输出是这样的:

start: 693635144, 693771992 | Path: 693635144, 693771992 - 0, 0 - 718079568, 693635144 - | numPoints: 3
start: 35, 29 | Path: 35, 29 - 250, 350 - 300, 400 - | numPoints: 3
start: 35, 32 | Path: 35, 32 - 250, 350 - 300, 400 - | numPoints: 3
start: 35, -1437401059 | Path: 35, -1437401059 - 250, 4 - 300, 2949120 - | numPoints: 3
start: 35, 39573896 | Path: 35, 39573896 - 250, 1 - 300, 0 - | numPoints: 3
start: 47, 44 | Path: 47, 44 - 250, 350 - 300, 400 - | numPoints: 3
start: 53, 47 | Path: 53, 47 - 250, 350 - 300, 400 - | numPoints: 3
start: 53, 39589128 | Path: 53, 39589128 - 250, 1 - 300, 0 - | numPoints: 3
start: 56, 53 | Path: 56, 53 - 250, 350 - 300, 400 - | numPoints: 3
start: 56, 50 | Path: 56, 50 - 250, 350 - 300, 400 - | numPoints: 3
start: 39591176, 39591176 | Path: 39591176, 39591176 - 1, 12 - 99607256, 0 - | numPoints: 3
start: 71, 59 | Path: 71, 59 - 250, 350 - 300, 400 - | numPoints: 3
start: 71, 68 | Path: 71, 68 - 250, 350 - 300, 400 - | numPoints: 3

如果我自己提供分数,那么它可以使用jintArrays。如何正确检索jint*的int值并从中创建D2D1::Point2F

1 个答案:

答案 0 :(得分:0)

您的代码存储指向jxpoints的指针,jypoints供以后使用,并释放Java数组。难怪有时这些指针指向随机变化的数据。您可能需要将坐标复制到一些持久的C ++数组,例如通过shared_pointer<vector<jint>>

vector<jint> jxpoints(numPoints);
env->GetIntArrayRegion(xpoint, 0, numPoints, &jxpoints[0]);