2017-10-05 3 views
-2

실린더를 만들어야하는 클래스를 작성하려고했습니다. 그러나 내 ArrayList을 그리려고 할 때 같은 값을 가진 180 개의 객체가 있습니다. 내 ArrayList에 쿼드를 추가하려고 할 때 거기에 무엇이 잘못되었는지 이해하지 못합니다.ArrayList에 요소 추가

public class Planet { 
    public static ArrayList<Quad> createRing(int x, int y, int z, int radius) { 
     ArrayList<Quad> quads = new ArrayList<Quad>(); 
     for (int i = 0; i < 360; i++) { 
      float x1 = (float) (Math.sin(Math.toRadians(i)) * radius); 
      float y1 = (float) (Math.cos(Math.toRadians(i)) * radius); 
      i++; 
      float x2 = (float) (Math.sin(Math.toRadians(i)) * radius); 
      float y2 = (float) (Math.cos(Math.toRadians(i)) * radius); 

      quads.add(new Quad(new Vector3f(x1, y1, 4), new Vector3f(x2, y2, 4), new Vector3f(x2, y2, 0), 
        new Vector3f(x1, y1, 0))); 
     } 
     return quads; 
    } 
} 

----QUAD.java 

import org.lwjgl.opengl.GL11; 
import org.lwjgl.util.vector.Vector3f; 

public class Quad { 
//have to be non-static 
static Vector3f Cord1; 
static Vector3f Cord2; 
static Vector3f Cord3; 
static Vector3f Cord4; 
//have to be non-static 
public Quad(Vector3f cord1,Vector3f cord2,Vector3f cord3,Vector3f cord4) { 
Cord1 = cord1; 
Cord2 = cord2; 
Cord3 = cord3; 
Cord4 = cord4; 
} 
public void draw() { 
//GL11.glBegin(GL11.GL_QUADS); 
GL11.glColor3f(1, 0, 0); 
GL11.glVertex3f(Cord1.x, Cord1.y, Cord1.z); 
GL11.glVertex3f(Cord2.x, Cord2.y, Cord2.z); 
GL11.glVertex3f(Cord3.x, Cord3.y, Cord3.z); 
GL11.glVertex3f(Cord4.x, Cord4.y, Cord4.z); 
} 
public void out() { 

System.out.println(Cord1.x+" "+ Cord1.y+" "+ Cord1.z); 
System.out.println(Cord2.x+" "+ Cord2.y+" "+ Cord2.z); 
System.out.println(Cord3.x+" "+ Cord3.y+" "+ Cord3.z); 
System.out.println(Cord4.x+" "+ Cord4.y+" "+ Cord4.z); 
} 
} 
+0

당신은'System.out.println (eins);을 시도 했습니까? System.out.println (zwei); 등등과 같은지 확인하십시오. 매번 다른 값을 할당 하시겠습니까? –

+1

나는 for 루프의 값을 변경하여 테스트했고 매번 다른 실린더가있는 부분을 얻었습니다. 하지만 나는 한 번에 전체 실린더를 그릴 수 없습니다. –

+0

'Quad'는 당신이 정의한 클래스인가요? 그 구성원은 정적입니까? 해당 클래스의 코드를 추가하십시오. 정적 인 경우 비 정적으로 만듭니다. –

답변

0
public class Quad { 
static Vector3f Cord1; 
static Vector3f Cord2; 
static Vector3f Cord3; 
static Vector3f Cord4; 
public Quad(Vector3f cord1,Vector3f cord2,Vector3f cord3,Vector3f cord4) { 
Cord1 = cord1; 
Cord2 = cord2; 
Cord3 = cord3; 
Cord4 = cord4; 
} 

당신은 CordN 변수 static을 만들었습니다, 각각의 필드는 클래스의 모든 인스턴스간에 공유 단지 1 값을 가지고 있습니다. 따라서 클래스의 새 인스턴스를 만들 때마다 이전 값을 덮어 씁니다.

static 수정자를 제거하십시오.


또한,이 멤버 변수를 만들기 위해 좋은 연습 있다는 final 당신이 절대적으로하지 그들을 필요로하지 않는주의. 이 경우에는 나타나지 않으므로 final을 작성하십시오. 생성자에서 static final 필드를 재 할당 할 수 없으므로이 문제가 자동으로 발견되었습니다.