如何在JavaFX中的矩形或圆形内添加图像? [英] How do I add an image inside a rectangle or a circle in JavaFX?

查看:889
本文介绍了如何在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));

这篇关于如何在JavaFX中的矩形或圆形内添加图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆