Creating a Graphics

When implementing a method that is passed a Graphics reference, it is best to ensure that the method results in no net change to the Graphics. In other words, when the method returns, the Graphics should be in the same state that it was in before the method was invoked.

There are exceptions to this rule, of course. For instance, we can have a high degree of certainty that callers of paint(Graphics) will merely dispose of the Graphics when the call to paint() returns. Therefore, it is acceptable to modify the Graphics passed to paint() with wanton disregard to maintaining its state.

However, in other situations it is not so clear as to whether the Graphics must retain its initial state. In such cases, it is best to take the ...

Get Graphic Java™ 1.2, Volume I: AWT, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.