Question

What I want to do: In PySide, one could override the paintEvent() method of a QWidget to draw a custom widget. The bounding rect can be customized by overriding the boundingRect() method.

How does one do this in JavaFX? My goal is to create a custom Rectangle object, that draws itself smaller than it's bounding rect.


In context: I'm creating an MS Paint clone in JavaFX. I'm working on the selection box that you use to select/move pixels around. I want the cursor to change to the appropriate resizing cursor when it is near the outside of the selection box.

However, the bounding rect is the same size as the selection box drawn on the screen, so the cursor only changes when it is on top of the box, but not when near. My solution is to set the bounding rect to larger than the actual selection box is, so the cursor change will occur. Then, override it's paintEvent() equivalent to draw a smaller selection box.

Thanks for your help.

Was it helpful?

Solution

I have a similar use case to you, and asked a similiar question here : Drawing transform independent layout bounds in JavaFX.

The JavaFX API is much higher level though than Java2D or PySide (I am assuming from your snippets, because I actually never heard of it ;) ), it does not allow you to override painting of Nodes, nor can you stop a Node from inheriting its parents transform.

This means that you need a seperate Group parallel to your content where you create the selection box and update it according to your needs (content changes etc.).

Example SceneGraph:

  • Scene
    • contentGroup
      • someShapeFromUser
    • selectionBoxGroup
      • selectionBoxOfSomeShapeFromUser
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top