1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
| import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class Snippet245 {
/**
* Default constructor.
*/
public Snippet245() {
final Display display = new Display();
final Shell createdShell = createContent(display);
createdShell.open();
while (!createdShell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
public static void main(final String[] args) {
new Snippet245();
}
/**
* Draw an oval using a GC instance.
*/
public void launchTreatment(final SelectionEvent anEvent) {
final Runnable run = new Runnable() {
/**
* {@inheritDoc}
*/
@Override
public void run() {
// Utilisation du Thread IHM:
Display.getDefault().asyncExec(new Runnable() {
/**
* {@inheritDoc}
*/
@Override
public void run() {
final Display display = anEvent.widget.getDisplay();
final GC gc = new GC(display.getActiveShell());
gc.setAntialias(SWT.ON);
gc.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
gc.fillOval(50, 50, 100, 60);
gc.setBackground(display.getSystemColor(SWT.COLOR_BLACK));
gc.setLineWidth(2);
gc.drawOval(50, 50, 100, 60);
gc.dispose();
}
});
}
};
new Thread(run).start();
}
/**
* <p>
* Create the UI content.
* </p>
*
* @param aDisplay
* the display to use as a parent.
* @return the Shell created.
*/
private Shell createContent(final Display aDisplay) {
final Shell shell = new Shell(aDisplay);
shell.setLayout(new GridLayout());
final Button button = new Button(shell, SWT.PUSH);
button.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
button.setText("Launch");
button.addSelectionListener(new SelectionAdapter() {
/**
* {@inheritDoc}
*/
@Override
public void widgetSelected(final SelectionEvent anEvent) {
launchTreatment(anEvent);
}
});
final Rectangle clientArea = shell.getClientArea();
shell.setBounds(clientArea.x + 10, clientArea.y + 10, 200, 200);
return shell;
}
} |