如何在JavaFX中的矩形或圆形内添加图像?
问题描述:
假设我们有一个名为r的矩形
Suppose we have a rectangle called r
Rectangle r = new Rectangle(40, 20);
和一张名为image的图片
and an image called image
Image image = new Image("...src for image");
如何在矩形内拟合图像?另外,如果矩形移动,如何移动图像?如何为圆圈做同样的事情?代码示例非常感谢。
How do I fit the image inside the rectangle? Also, how can I make the image move if the rectangle moves too? How do I do the same thing for a circle? Code examples are greatly appreciated.
P.S。 Jewelsea,我在等你,哈哈!
P.S. Jewelsea, I'm waiting for you, lol!
答
如果你想按图片填充矩形,你可以按照下列说明: - 你的fxml文件中的
添加一个圆圈
if you want to fill Rectangle by image, you can follow this:- in your fxml file add a Circle
<Rectangle fx:id="imgMenuUser" />
在您的控制器中
@FXML
private Rectangle rectangle;
Image img = new Image("/image/rifat.jpg");
rectangle.setFill(new ImagePattern(img));