我在javafx程序中有两个ImageView。它们都已经旋转和平移了几次。我知道他们的初始角度和位置(layoutX,layoutY),并且我也有他们经过的变换和旋转的列表。我怎么知道它们现在是否彼此重叠?
图像如下:
苹果的图片
箭头图像:
如果我可以确定箭头的尖端是否在苹果图像内,这也将非常有帮助。但是,如果我只能说出图像是否碰撞,就可以了。
苹果类:
class Apple {
public double height, width, x1, x2, y1, y2;
public ImageView image;
Apple(double x1, double y1, double height, double width) {
this.x1 = x1;
this.y1 = y1;
this.height = height;
this.width = width;
}
Apple(double x1, double y1) {
height = 20;
width = 20;
this.x1 = x1;
this.y1 = y1;
}
public boolean isCollision(double ax, double ay) {
x2 = x1 + width;
y2 = y1 + height;
if (ax > x1 && ax < x2 && ay > y1 && ay < y2) {
return true;
} else {
return false;
}
}
}
创建苹果的代码:
Apple generateApple() {
double x1, y1, rx, ry;
x1 = 300;
y1 = 250;
rx = 150;
ry = 150;
double xa, ya;
xa = randomno(x1, x1 + rx);
ya = randomno(y1, y1 + ry);
Apple apl = new Apple(xa, ya);
createAppleImage(apl);
return apl;
}
void createAppleImage(Apple apple) {
ImageView appleImage = null;
FileInputStream inputstream5 = null;
try {
inputstream5 = new FileInputStream("C:\\Users\\MAHDI\\Documents\\NetBeansProjects\\ThreadTesting\\apple.jpg");
Image img4 = new Image(inputstream5);
appleImage = new ImageView(img4);
appleImage.setFitHeight(apple.height);
appleImage.setFitWidth(apple.width);
appleImage.setLayoutX(apple.x1);
appleImage.setLayoutY(apple.y1);
System.out.println(" " + apple.x1 + " " + apple.y1);
gameLayout.getChildren().add(appleImage);
} catch (FileNotFoundException ex) {
Logger.getLogger(AppleShooter.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
inputstream5.close();
} catch (IOException ex) {
Logger.getLogger(AppleShooter.class.getName()).log(Level.SEVERE, null, ex);
}
}
apple.image = appleImage;
}
创建箭头的代码:
FileInputStream inputstream3 = new FileInputStream("C:\\Users\\MAHDI\\Documents\\NetBeansProjects\\ThreadTesting\\arrowpic.png");
Image img2 = new Image(inputstream3);
arrow = new ImageView(img2);
arrow.setFitHeight(arrowheight);
arrow.setFitWidth(arrowwidth);
arrow.setLayoutX(40);
arrow.setLayoutY(420);
arrow.setRotate(-45);
答案 0 :(得分:0)
我建议您先看看Shape.intersects
我还想补充一点,我对JavaFX完全一无所知,并且通过阅读JavaDocs和其他一些示例基本上将其混为一谈
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Shape;
import javafx.stage.Stage;
import javafx.util.Duration;
public class Test extends Application {
@Override
public void start(Stage primaryStage) {
Rectangle box1 = new Rectangle(10, 10, 100, 100);
box1.setFill(Color.RED);
Rectangle box2 = new Rectangle(120, 10, 100, 100);
box2.setFill(Color.BLUE);
Pane root = new Pane();
root.getChildren().add(box1);
root.getChildren().add(box2);
Scene scene = new Scene(root, 230, 120);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
Timeline timeline = new Timeline(60, new KeyFrame(Duration.millis(500), new EventHandler<ActionEvent>() {
private double delta1 = 0.5;
private double delta2 = -5;
private double angle1 = 0;
private double angle2 = 0;
private Shape oldCollision;
@Override
public void handle(ActionEvent event) {
angle1 += delta1;
angle2 += delta2;
box1.setRotate(angle1);
box2.setRotate(angle2);
if (oldCollision != null) {
root.getChildren().remove(oldCollision);
oldCollision = null;
}
Shape collision = Shape.intersect(box1, box2);
if (collision != null) {
collision.setFill(Color.AQUA);
root.getChildren().add(collision);
}
oldCollision = collision;
// if (box1.intersects(box1.parentToLocal(box2.getBoundsInParent()))) {
// box1.setFill(Color.AQUA);
// box2.setFill(Color.AQUA);
// } else {
// box1.setFill(Color.RED);
// box2.setFill(Color.BLUE);
// }
}
}));
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.play();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
我只想指出,添加和删除collision
Shape
仅用于演示!
现在在向我指出Rectangle
不是ImageView
的方式之前,请确保您花时间阅读ImageView
和Rectangle
的JavaDocs。我为何选择此示例的原因