Question

I am trying to implement look and feel in JFrame swing java, its throwing following exception

"java.lang.ClassCastException:javax.swing.DebugGraphics cannot be cast to java.awt.Graphics2D "

Its working on some forms. what is the reason for above error

Was it helpful?

Solution

Even though pretty much the whole context is missing, I have something to point out.

Ever since Java 1.4 or so, all Graphics objects passed between paint, paintComponent, paintBackground, paintChildren and whatnot became, in fact, Graphics2D objects. This is widely considered as fact (Graphics == Graphics2D).

However, some of your components seem to be using setDebugGraphicsOptions(int). This DOES make it so the former 'fact' is no longer true. As long as you have a robust implementation (such as instanceof Graphics2D checks), you will notice that, for example, RenderingHints (only available on Graphics2D) are no longer applied when using debug graphics. Otherwise, it will fail with the exact exception you are getting.

It would appear all your code assumes that Graphics are Graphics2D. So you have to either stop using graphic debug options, or manually fix all overridden paintComponent methods.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top