前提・実現したいこと
クラスが同じdouble型とboolean型をArrayListで同じリストに格納し、格納した要素をイテレータで取得し、if文で要素のtrueかfalseによって動作を分けたい。
発生している問題・エラーメッセージ
イテレータは、要素があるとtrueが返してしまうため、すべてのboolean型の要素がtrueになってしまう。
試したこと
boolean型の変数にprivateをつけ、外部から変更できないようにし、trueかfalseの判別はゲッターを用いて行った。
また、イテレータではなく、拡張for文で行った。
java
1class Figure3D{ 2 static ArrayList<Point3D> list = new ArrayList<Point3D>(); 3 static void prim(double r, double th, double phi, boolean draw){ 4 double D2R = Math.PI/180.0; 5 double x, y, z; 6 x = r*Math.sin(th*D2R)*Math.sin(phi*D2R); 7 y = r*Math.cos(th*D2R); 8 z = r*Math.sin(th*D2R)*Math.cos(phi*D2R); 9 list.add(new Point3D(x, y, z, draw)); 10 } 11 static void display(double alpha, double beta, int px, int py, Graphics g){ 12 int _dx = 0, _dy = 0, dx = 0, dy = 0; 13 Point p2d; 14 int N = list.size(); 15 int i = 0; 16 for(Point3D p: list){ 17 p2d = p.get2D(alpha, beta, px, py); 18 dx = (int)p2d.getX(); 19 dy = (int)p2d.getY(); 20 if(i++ > 0){ //ここで、trueかfalseを判別し以下の動作を行いたい 21 g.setColor(Color.getHSBColor((float)i/N, 1.0f, 1.0f)); 22 g.drawLine(_dx, _dy, dx, dy); 23 } 24 _dx = dx; 25 _dy = dy; 26 } 27 } 28} 29 30class Point3D{ 31 private double x, y, z; 32 boolean d; 33 Point3D(double _x, double _y, double _z, boolean _d){ 34 x = _x; 35 y = _y; 36 z = _z; 37 d = _d; 38 } 39 Point get2D(double alpha, double beta, int px, int py){ 40 41 } 42} 43
回答1件
あなたの回答
tips
プレビュー