Java 2d graphics paint flicker when updating

This article shows how to use a GC to draw onto an Image, or onto a control through its paint Event callback.The Canvas control, specifically designed for drawing operations, has a number of constructor style bits that allow you to determine when and how painting occurs, and the article shows how to use these.The code below creates a GC with an image as its argument and draws two lines on it, one from the top left (0,0) to the bottom right, and one from the top right to the bottom left.

java 2d graphics paint flicker when updating-69java 2d graphics paint flicker when updating-18

The correct way to draw onto a control is by adding a listener to its paint event. The Paint Event includes a GC that you can send message to, that is already prepared to draw onto the control and includes the damaged area.

The following code adds a paint listener to a Shell, and in the paint Control callback method draws a line from the origin to bottom right corner.

For more information on how to manage SWT resources see SWT: The Standard Widget Toolkit.

A GC that's instantiated by the program should be drawn upon and then disposed as soon as possible.

Graphics can be drawn on anything that implements is a graphics context that encapsulates the drawing operations that can be performed.