Java Tutorial/SWT 2D Graphics/GC

Материал из Java эксперт
Перейти к: навигация, поиск

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

  1. The GC class (short for graphical context) forms the core of SWT"s graphics engine.
  2. GC offers all the methods required for drawing shapes, text, and images.
  3. You can draw on Controls, Devices, or other Images.

Generally, drawing lifecycles consists of the following:

  1. Creating or obtaining a GC to draw on the desired target
  2. Drawing
  3. 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>