Troubleshooting

A Flicker-Free Drawing Surface

Whenever I try to change the font from inside a drawing surface's paint method, the contents of the drawing surface flicker. What's going on?

The problem originates from calling the wrong setFont method. Instead of calling setFont via the Graphics argument, you are calling the inherited setFont method from the Component class. When you call this method, a recursive loop is established and the drawing surface flickers. Here is a code fragment that demonstrates incorrectly and correctly setting the font:

public void paint (Graphics g)

Get Special Edition Using Java 2 Standard 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.