java - 构图:如何使形状的尺寸适合图像?
问题描述
我正在制作一个基于 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);
我在这里有我的例子(我不知道为什么堆栈溢出不允许我上传图片):
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);
}
}
推荐阅读
- javascript - 如何使用 cytoscape.js 在单个动画中沿特定方向移动所有节点?
- linkedin - 包含 timeRange 参数时,LinkedIn V2 API 错误“参数中存在未经允许的字段”
- node.js - 在nodejs中需要模块时,如何让基本目录避免使用../../../?
- c# - 当长度低于padleft的长度时,Padleft不在字符串中添加零?
- python - 在 y-achse 上找不到标签
- react-native - React Native 的“Slingshot”样式 UI 控件
- java - 编译为在 Java 6 上运行但在 Java 8 JVM 上运行的应用程序与在 Java 8 上编译的相同 App 之间的差异
- python - pip 是否将软件包安装到我的 conda 环境中?
- python - 在 SQL For 循环中迭代数组列
- java - Java:如何以用户身份执行外部命令?