Reputation: 303
For the purpose a graphical program I am creating right now I am creating forks of a Graphics2D instance using Graphics2D g = (Graphics2D) gOrig.create();
. What I am wondering is if I should be using the g.dispose();
method after I am finished with this fork, or if that is handled automatically by the under workings of Java.
Upvotes: 2
Views: 175
Reputation: 285405
Yes, definitely dispose of this Graphics object. Dispose of all Graphics or Graphics2D objects that you create as this will help clean up resources that are no longer needed.
But dispose of none of the Graphics objects given to you by the JVM as a parameter to a painting method such as public void paint(Graphics g)
or protected void paintComponent(Graphics g)
. Disposing of these risks breaking the painting chain as these Graphics objects are often needed down-stream by child components, borders, and the like.
Upvotes: 4