2017-03-05 7 views
0

intersects을 사용하여 두 도형 간의 충돌을 감지하려고 시도했지만 감지가 작동하지 않습니다.Java 충돌 감지

Circle 개체를 인쇄하면 x 및 y 위치가 0으로 설정되어 있음을 알 수 있습니다. 그러나 위치 정보 getter 및 setter 메서드가 올바르게 작동하지 않는 것으로 의심됩니다. 그러나이 정보를 인쇄하면 0이 아닌 값이 나타납니다.

검색이 작동하지 않는 이유는 무엇입니까?

감사합니다.

편집 : 아래 코드가 작동 중입니다. 이슈/솔루션에 대한 세부 설명은 주석을 참조하십시오.

메인 클래스

import java.awt.Graphics; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.Random; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class Main extends JPanel { 

    public static List<Circle> circleList = new ArrayList<>(); 
    private static Random random = new Random();  

    public Main() { 
     for (int i = 0; i < 2; i++) { 
      circleList.add(new Circle(random.nextInt(500), random.nextInt(500))); 
     }  
    } 

    @Override 
    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     for (Circle CircleShape : circleList) { 
      CircleShape.collision(); 
      CircleShape.drawCircle(g); 
     } 
    } 

    public static void main(String[] args) { 
     JFrame frame = new JFrame(); 
     frame.setSize(500, 500); 
     frame.setResizable(true); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     frame.add(new Main()); 
    } 

} 

Circle 클래스

import java.awt.Graphics; 

public class Circle extends Shape { 

    public Circle(int x, int y) { 
     super(x, y); 
     super.setSize(200, 200); 
    } 

    public void drawCircle(Graphics g) { 
     g.setColor(colour); 
     g.fillOval(x, y, 200, 200); 
    } 

    public void collision() { 
     for (Circle CircleShape : Main.circleList) { 
      System.out.println(CircleShape); 
      System.out.println(CircleShape.getxPos()); 

      if (this.intersects(CircleShape)) { 
       System.out.println("collision detected"); 
      } else { 
       System.out.println("no collision detected"); 
      } 

     } 
    } 

} 

모양 Class 구성 intersects 방법은 X, Y, 폭, 높이, 속성 설정이 정확 것으로 예상

import java.awt.Color; 
import java.awt.Rectangle; 

public class Shape extends Rectangle { 

    //int x, y; 
    Color colour; 

    public Shape(int x, int y) { 
     //this.setxPos(x); 
     //this.setyPos(y); 
     this.setPos(x, y); 
    } 
/* 
    public int getxPos() { 
     return this.x; 
    } 

    public void setxPos(int x) { 
     this.x = x; 
    } 

    public int getyPos() { 
     return this.y; 
    } 

    public void setyPos(int y) { 
     this.y = y; 
    } 
*/ 

    public void setPos(int x, int y) { 
     super.setLocation(x, y); 
    } 

    public Point getPos() { 
     return new Point(x, y); 
    } 

} 
+0

'setxPos'와'setyPos' 메쏘드에서'super.setLocation (x, y)'를 호출해야합니다. 그렇지 않으면 좌표는 무시됩니다. 하위 클래스의 변수를 다시 정의해도 수퍼 클래스의 변수가 재정의되지 않습니다. – BackSlash

+0

답변 해 주셔서 감사합니다. super.setLocation (x, y)를 사용하여 새로운 getter 및 setter 메서드를 만들었지 만 충돌 검색이 여전히 작동하지 않습니다. – user1334130

+0

새 코드로 질문을 업데이트 할 수 있습니까? – BackSlash

답변

1

들은 때문에 물체가 다른 물체와 충돌하는지 탐지하는 데 사용됩니다.

따라서 setter 메소드에서 super.setLocation(newX, newY)을 호출해야하며 올바른 너비와 높이를 제공해야합니다. setLocation을 사용하여도

public Circle(int x, int y, int width, int height) { 
    super(x, y, width, height); 
} 

을 그리고 : 당신은 단지 클래스 Rectangle에서 이미 제공하는 방법을 사용할 수

public void setxPos(int x) { 
    this.x = x; 
    super.setLocation(x, y); 
} 

public void setyPos(int y) { 
    this.y = y; 
    super.setLocation(x, y); 
} 

public Circle(int x, int y) { 
    super(x, y); 
    super.setSize(200, 200); 
} 

을 또는 :

그래서, 그것은해야 setxPos 및대신.

전체 코드는 다음과 같이 될 것입니다 :

Shape 클래스

import java.awt.Color; 
import java.awt.Rectangle; 

public class Shape extends Rectangle { 

    Color colour; 

    public Shape(int x, int y, int width, int height) { 
     // provided by the Rectangle class. Needed for proper collision detection 
     super(x, y, width, height); 
    } 
} 

Circle 클래스 :

import java.awt.Graphics; 

public class Circle extends Shape { 

    public Circle(int x, int y, int width, int height) { 
     super(x, y, width, height); 
    } 

    public void drawCircle(Graphics g) { 
     g.setColor(super.colour); 
     // instead of writing values here, we get them from width and height fields 
     g.fillOval(x, y, (int) getWidth(), (int) getHeight()); 
    } 

    public void collision() { 
     for (Circle CircleShape : Main.circleList) { 
      System.out.println(CircleShape); 
      System.out.println(CircleShape.getLocation().x); 

      if (this.intersects(CircleShape)) { 
       System.out.println("collision detected"); 
      } else { 
       System.out.println("no collision detected"); 
      } 

     } 
    } 
} 

Main 클래스 :

import java.awt.Graphics; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.Random; 

import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class Main extends JPanel { 

    public static List<Circle> circleList = new ArrayList<>(); 
    private static Random random = new Random(); 

    public Main() { 
     for (int i = 0; i < 2; i++) { 
      // width and height are specified here instead of inside the Circle.drawCircle method. 
      circleList.add(new Circle(random.nextInt(500), random.nextInt(500), 200, 200)); 
     } 
    } 

    @Override 
    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     for (Circle CircleShape : circleList) { 
      CircleShape.collision(); 
      CircleShape.drawCircle(g); 
     } 
    } 

    public static void main(String[] args) { 
     JFrame frame = new JFrame(); 
     frame.setSize(500, 500); 
     frame.setResizable(true); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     frame.add(new Main()); 
    } 

}