Java冲突检测
我试图使用相交检测两个形状之间的碰撞,但是,检测不起作用 打印出圆对象显示x和y位置设置为0。我怀疑position getter和setter方法可能工作不正常,但是,打印出这些信息会显示非零值 为什么检测不起作用 多谢各位 编辑:下面的代码现在正在工作。有关问题/解决方案的详细信息,请参见备注 主类Java冲突检测,java,drawing,collision,Java,Drawing,Collision,我试图使用相交检测两个形状之间的碰撞,但是,检测不起作用 打印出圆对象显示x和y位置设置为0。我怀疑position getter和setter方法可能工作不正常,但是,打印出这些信息会显示非零值 为什么检测不起作用 多谢各位 编辑:下面的代码现在正在工作。有关问题/解决方案的详细信息,请参见备注 主类 import java.awt.Graphics; import java.util.ArrayList; import java.util.List; import java.util.Ran
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());
}
}
形状类
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);
}
}
“相交”方法要求正确设置x、y、宽度和高度属性,因为它们用于检测对象是否与另一个对象碰撞
因此,在setter方法中,您需要调用super.setLocationnewX、newY,并且还应该提供有效的宽度和高度
因此,它应该是:
public void setxPos(int x) {
this.x = x;
super.setLocation(x, y);
}
public void setyPos(int y) {
this.y = y;
super.setLocation(x, y);
}
及
或者,您可以使用类Rectangle中已经提供的方法:
也可以使用setLocation而不是setxPos和setyPos
完整的代码如下所示:
形状类
圆圈类:
主要类别:
您应该在setxPos和setyPos方法中调用super.setLocationx,y,否则坐标将被忽略。在子类中重新定义变量不会覆盖超类中的变量。感谢您的回复。我已经使用super.setLocationx,y创建了一个新的getter和setter方法,但是,冲突检测仍然不起作用。您能用新代码更新问题吗?完成。请参见上面的编辑。您还需要使用setSize。在drawCircle方法中填充200x200椭圆,但该类没有此信息。将super.setSize200、200放入循环构造函数中。如果仍然不起作用,则需要调试并检查类中的所有值。
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);
}
public Circle(int x, int y, int width, int height) {
super(x, y, width, height);
}
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);
}
}
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");
}
}
}
}
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());
}
}