Painting is complicated, for better or for worse. Are you sure about your coordinate system, scaling applied after, etc?
https://www.oracle.com/java/technologies/painting.html#callbacks
If you want to 'cache' an ephemeral value, you can use get
or putClientProperty
to store/retrieve arbitrary objects. They'll be stored directly on your component instance (the paintable canvas in this case); since you're sensitive to your component's size, you might want to use the component dimensions as the cache key.