[英]Add Element to Canvas JavaFX
So I want to add elements or items to a canvas through a GraphicsContext
. 所以我想通过GraphicsContext
将元素或项目添加到画布。 For example, to add a Rectangle, I don't want to use fillRect(...)
, I want to create a Rectangle rect = new Rectangle(...)
and add it to my GraphicsContext, So I can perform changes on that rect
. 例如,要添加一个Rectangle,我不想使用fillRect(...)
,我想创建一个Rectangle rect = new Rectangle(...)
并将其添加到我的GraphicsContext中,因此我可以在那rect
。
Something like this : 像这样的东西:
Rectangle rect = new Rectangle();
rect.setHeight(100);
rect.setWidth(100);
rect.setFill(Color.BLACK);
DragResizeMod.makeResizable(rect, null);
then added it to 然后将其添加到
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.add(rect); //somehow
There is a solution which is to use a Pane
instead of Canvas
but I don't think it the best way, since a GraphicsContext
run on a single thread. 有一种解决方案是使用Pane
而不是Canvas
但我认为这不是最好的方法,因为GraphicsContext
在单个线程上运行。
Thanks in advance. 提前致谢。
Adding Rectangles to a properly configured Pane is the way to go. 将矩形添加到正确配置的窗格中的方法。 I don't understand what your problem with this approach is. 我不明白您使用此方法所遇到的问题。 This is the way JavaFX works and you will probably not be able to change this. 这就是JavaFX的工作方式,您可能将无法更改它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.