我正在为学校开展一个项目,并且已经实施了一种检查两个物体是否相交的方法。我已经定义了两个矩形,但是当它检查它们是否相交时,它总是正确的,即使它们位于JFrame的两侧。布尔冲突在开始时设置为false,x和y是屏幕上两张照片的坐标。
public Rectangle Bounds() {
return (new Rectangle(x, y, 225, 225)) ;
}
public Rectangle TrollBounds() {
return (new Rectangle(x, y, 24, 24)) ;
}
Rectangle hitbox = Bounds() ;
Rectangle Hitbox2 = TrollBounds() ;
检查交叉点的部分
if (hitbox.intersects(Hitbox2)) {
collision = true ;
System.out.println("collision") ;
} else {
collision = false ;
整个班级
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
public class Screen extends JPanel implements Runnable, KeyListener {
int health = 1200;
boolean collision = false ;
BufferedImage Troll ;
int cubex = 600 ;
int cubey = 100 ;
int cubexx = 200 ;
int cubeyy = 200 ;
public boolean running = true ;
BufferedImage Player;
int x = 100 ;
int y = 100 ;
boolean Jumping = false ;
int startJump = 10 ;
public void Jump() {
}
boolean up = false;
boolean down = false;
boolean left = false;
boolean right = false;
public Screen() {
loadImages();
Thread thread = new Thread(this);
thread.start();
}
private void loadImages() {
try {
Player = ImageIO.read(getClass().getResource("/sanic.png"));
Troll = ImageIO.read(getClass().getResourceAsStream("/Trollface.png")) ;
} catch (IOException e) {
e.printStackTrace();
}
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_W) {
up = true ;
}
if (e.getKeyCode() == KeyEvent.VK_A) {
left = true ;
}
if (e.getKeyCode() == KeyEvent.VK_D) {
right = true ;
}
if (e.getKeyCode() == KeyEvent.VK_S) {
down = true ;
}
if (e.getKeyCode() == 31) {
Jump() ;
}
}
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == 87) {
up = false ;
}
if (e.getKeyCode() == 65) {
left = false;
}
if (e.getKeyCode() == 68) {
right = false ;
}
if (e.getKeyCode() == 83) {
down = false ;
}
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void run() {
while(running) {
//gravity
//gravity
//Player movement
if (left) {
if (x <= -10) {
x = 1464 ;
}
x = x - 2 ;
}
if (up) {
if(y <= -112) {
y = 910 ;
}
y = y - 2 ;
}
if (right) {
if (x >= 1416) {
x = -24 ;
}
x = x + 2;
}
if (down) {
if (y >= 900) {
y = -10 ;
}
y = y + 2 ;
}
//Player movement
//ball movement
if (cubey > y) {
cubey-- ;
}
if(cubey < y) {
cubey++ ;
}
if (cubex > x) {
cubex-- ;
}
if (cubex < x) {
cubex++ ;
}
//ball movement
if (hitbox.intersects(Hitbox2)) {
collision = true ;
System.out.println("collision") ;
} else {
collision = false ;
}
if (collision) {
health -- ;
}
System.out.println(collision) ;
repaint() ;
try {
Thread.sleep(3) ;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
//Graphical loop start
g.drawImage(Player, x, y, null) ;
g.drawImage(Troll, cubex, cubey, null) ;
g.drawString("SANIC", x, y) ;
g2d.fillRect(220, 200, health, 50) ;
//Graphical loop end
}
public Rectangle Bounds() {
return (new Rectangle(x,y,cubex+225,cubey+225)) ;
}
public Rectangle TrollBounds() {
return (new Rectangle(cubex,cubey,cubex+24,cubey+24)) ;
}
Rectangle hitbox = Bounds() ;
Rectangle Hitbox2 = TrollBounds() ;
}
答案 0 :(得分:2)
如果运行此代码的方式是如何排序的,将hitbox和Hitbox2紧接在一起并且不改变其间的x和y,那么它应该返回true。
它将形成一个从x,y到255,255的矩形,另一个从x,y到24,24,这些矩形相交。
这是Rectangle
上的JavaDoc&amp; Shape.intersects()
。