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 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.