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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.