9.11. Graphics Operations

In traditional applets and applications, the paint method is used to implement custom drawing. It takes a Graphics object as an argument; this object is used to draw onto the window. Methods outside paint can obtain the current Graphics object by calling getGraphics. Note that it is not reliable to simply call getGraphics once and store the Graphics object in an instance variable because subsequent invocations of paint get new versions. However, it is reliable to pass the Graphics object to other methods that draw and then return before paint returns.

Similarly, in newer Swing applets and applications, the Graphics object provides a means to perform simple drawing. In Swing, the paintComponent method, not paint, is used ...

Get Core Web Programming, Second Edition now with O’Reilly online learning.

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