Java garbage collection and graphics processing methods
I am creating a game (snake clone) as a hobby I'm looking at the dispose method of the graphics class in the Java API When I annotate the dispose method, my animation works the same way, whether it has it or not In the Java API, the dispose method does this - freeing the system resources being used by the graphics context Isn't the memory of the Java garbage collection Manager similar to what dispose is doing? Should I keep the handling method?
The API is not very helpful in explaining synchronization methods But from what I read in other forums, the sync method of toolkit class is to ensure that the drawing operation (such as the paintcomponent method I want) is refreshed to the graphics card Is the job of the graphics card to clean up the rest of the program's previous graphics context?
This is the code:
public void paintComponent(Graphics g) { super.paintComponent(g); Toolkit.getDefaultToolkit().sync(); g.dispose(); }
Solution
When it comes to graphics, there is a simple principle
If you explicitly create it (for example, buffereimage. CreateGraphics ()) and then dispose of it
The Otoh instance g in the paint assembly (Figure g) is provided by the kit and processed as needed Doing this in your own code will result in "unpredictable" rendering