gpt4 book ai didi

java - 构图 : How to make a shape's dimensions fit around an image?

转载 作者:行者123 更新时间:2023-11-29 07:24:50 24 4
gpt4 key购买 nike

我正在制作一款基于 JavaFX 的游戏,该游戏使用宇宙飞船射击无人机来保卫您的基地。我想在图像周围包裹一个形状以为其创建一个点击框。

这将使激光在接触碰撞箱时对无人机造成伤害。这将使游戏看起来比激光击中看不见的墙(形状的尺寸)更好,而不是实际图像。

我的问题是,JavaFX是否提供了任何内置库来解决这个问题,还是必须使用微积分公式来解决这个问题?

我试图查看 JavaFX 的 API,但似乎找不到任何有用的东西。

// Getting the images for the shapes
Image spaceCity = new Image("com/images/spaceCity.jpg");
Image spaceShip = new Image("com/images/spaceShip.png");

// Making the graphics
Rectangle space_Ship = new Rectangle(0, positionOfShipY, 191, 300);
Rectangle background = new Rectangle(0, 0, STAGE_WIDTH, STAGE_HEIGHT);

我这里有我的例子(我不知道为什么 Stack overflow 不允许我上传图片):

https://docs.google.com/document/d/1A8HJ61jhthBhd7wr6xrZNLlsTcQJNLUhTzrucvNY3BY/view?usp=sharing

最佳答案

根据图像设置形状的尺寸,并将两者放在 StackPane 中:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class FxTest extends Application {

@Override
public void start(Stage primaryStage) throws Exception{

String imagePath = "https://png.pngtree.com/png-clipart/20190118/ourmid/"
+ "pngtree-hand-drawn-spaceship-grey-spaceship-alien-spaceship-blue"
+ "-spaceship-border-png-image_450067.jpg";
Image image = new Image(imagePath);
Rectangle rec = new Rectangle(0, 0, 50+image.getWidth(), 50+image.getHeight());
rec.setFill(Color.AQUA);

StackPane root = new StackPane();
root.getChildren().add(rec);
root.getChildren().add(new ImageView(image));
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}

public static void main(String[] args) {
launch(null);
}
}

enter image description here

关于java - 构图 : How to make a shape's dimensions fit around an image?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55319298/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com