2017-09-08 3 views
1

キャンバス上に異なる色で点を描こうとしています。基本的に、現在のポイントは青で、ポイントは緑とポイントの現在のポイントの前に描画され、ポイントは赤で現在のポイントの後に描画されます。コードを参照してくださいキャンバス上での位置((x、y)座標)に応じて異なる色のSWTポイントを描く

private void setElemColor(GC g, int pos) { 
    int currentPoint = messtisch.getPointPos(); //current point, number 
    if (pos > currentPoint) { 
     g.setForeground(cRed); 
    } else if (pos == currentPoint) { 
     g.setForeground(cBlue); 
    } else if (pos < currentPoint) { 
     g.setForeground(cGreen); 
    } 
} 

理解を高めるために。これは完璧に動作します。しかし、私はIntの代わりにPointを使用して、同じことを行い、論理的な権利が得られないようにしようとしています。

のとおり
private void setPointColor(GC g, Point cpoint) { 
    if (cpoint.equals(currentPoint)) { // the current point itself 
     g.setForeground(cBlue); 
    } else if (!cpoint.equals(currentPoint)) { 
     if (cpoint.x > currentPoint.x || cpoint.y > currentPoint.y) { 
      g.setForeground(cRed); 
     } else { 
      g.setForeground(cGreen); 
     }  
    } 
} 

のように私を助けてください。

答えて

0

私は新しいArrayListを使用してこれを行いました。保存ポイントは既にcurrentPointです。緑色で古い点として描画します。ここに私のコードサンプルがあります。

private ArrayList<Point> oldpoints = new ArrayList<Point>(); 

private void setPointColor(GC g, Point cpoint) { 
    if (oldpoints.contains(cpoint)) { 
     g.setForeground(cGreen); 
    } else if (!oldpoints.contains(cpoint)) { 
     g.setForeground(cRed); 
    } 

    if (cpoint.equals(currentPoint)) { 
     g.setForeground(cBlue); 
     oldpoints.add(cpoint); 
    } 
} 

これは効率的で論理的ではないため、他のmedhodをお勧めします。よろしくお願いします。

0

あなたのソリューションは合理的だと思いますが、リストの代わりにセットを使用することを検討してください。たとえば、TreeSetまたはHashSetは、リスト(O(n))を繰り返すよりもはるかに速く(木はO(log n)、ハッシュ・バケットはO(1))、既存のエントリを確認することができます。 - 線形時間)。

private Set<Point> oldpoints = new HashSet<Point>(); 
+0

ありがとうございます@BoffinbrainN、後で試してみてください。 –

関連する問題