2016-04-25 39 views
0

我想要做的是從Java繪製一個多邊形到一個直接的2d C++窗口。我有2個包含多邊形的x點和y點的數組。打印數組時,除了兩個數組中的第一個元素外,數組顯示正確。JNI jintArray的第一個值不正確

在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

+0

在本地代碼中有什麼'xpoints'和'ypoints'?我可以看到'xpoint' /'ypoint'和'jxpoints' /'jypoints'是什麼,但是我沒有看到'xpoints' /'ypoints'在任何地方被聲明。 – Michael

+0

我添加了標題信息 – MircoProgram

+0

我仍然看不到'xpoints'和'ypoints'在你顯示的C++代碼中的任何地方被聲明。 – Michael

回答

0

您的代碼存儲指向jxpoints,指向以後使用的指針,並釋放Java數組。難怪有時這些指針指向隨機更改的數據。您可能需要將座標複製到某個持久性C++數組,例如通過shared_pointer<vector<jint>>

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

謝謝,現在就像一個魅力! – MircoProgram

相關問題