Код: Выделить всё
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class GcDrawingTiming {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
shell.setText("SWT GC Timing Question");
int width = 40, height = 40;
// 1. Create the original image
Image image1 = new Image(display, width, height);
// 2. Get a GC to draw on it
GC gc = new GC(image1);
gc.setBackground(display.getSystemColor(SWT.COLOR_RED));
gc.fillRectangle(0, 0, width, height);
// 3. Create the first copy BEFORE gc.dispose()
Image image2 = new Image(display, image1, SWT.IMAGE_COPY);
// 4. Dispose of the GC
gc.dispose();
// 5. Create the second copy AFTER gc.dispose()
Image image3 = new Image(display, image1, SWT.IMAGE_COPY);
// Canvas to display the results
Canvas canvas = new Canvas(shell, SWT.NONE);
canvas.addListener(SWT.Paint, e -> {
// image1 is red (as expected)
e.gc.drawImage(image1, 10, 10);
// image2 is black (unexpected)
e.gc.drawImage(image2, 60, 10);
// image3 is red (as expected)
e.gc.drawImage(image3, 110, 10);
});
shell.setSize(200, 100);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/797 ... lank-black