作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在制作一款基于 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);
}
}
关于java - 构图 : How to make a shape's dimensions fit around an image?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55319298/
我是一名优秀的程序员,十分优秀!