Java Tutorial/SWT 2D Graphics/GC
Avoid creating a GC
You can avoid creating a GC and just use the one from the event. In this case, you shouldn"t dispose that GC.
<source lang="java">
shell.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent event) { event.gc.drawRectangle(); }
});</source>
Graphics
- The GC class (short for graphical context) forms the core of SWT"s graphics engine.
- GC offers all the methods required for drawing shapes, text, and images.
- You can draw on Controls, Devices, or other Images.
Generally, drawing lifecycles consists of the following:
- Creating or obtaining a GC to draw on the desired target
- Drawing
- If you created the GC, disposing the GC
In code, the drawing lifecycle looks like this:
<source lang="java">
GC gc = new GC(display); gc.drawRectangle(...); gc.drawText(...); gc.drawImage(...); gc.dispose();</source>