演示代码:
int[] createCircle(int rayon, int centreX, int centreY) {
int[] points = nouveau int[360 * 2];
pour(int i=0; i<360; i++) {
points[i*2] = centerX + (int)(rayon * Math.cos(Math.toRadians(i)));
points[i*2+1] = centerY + (int)(radius * Math.sin(Math.toRadians(i)));
}
points de retour ;
}
Point originalPosition = null ;
public RingShell() {
affichage final display = new Display();
shell final shell = new Shell (affichage, SWT.NO_TRIM | SWT.ON_TOP);
shell.setBackground(display.getSystemColor(SWT.COLOR_DARK_MAGENTA));
Région région = new Région ();
région.add(createCircle(100, 100, 100));
région.subtract(createCircle(50, 100, 100));
shell.setRegion(région);
shell.addMouseListener(new MouseListener() {
public void mouseDoubleClick (MouseEvent e) {
display.dispose();
}
public void mouseDown (MouseEvent e) {
originalPosition = new Point (ex, ey);
}
public void mouseUp (MouseEvent e) {
Positionoriginale = null ;
}
});
shell.addMouseMoveListener(nouveau MouseMoveListener() {
public void mouseMove (MouseEvent e) {
if(originalPosition == null) return;
Point point = display.map(shell, null, ex, ey);
shell.setLocation(point.x - originalPosition.x, point.y - originalPosition.y);
System.out.println("Déplacé de : " + originalPosition + " vers " + point);
}
});
Rectangle regionBounds = région.getBounds();
shell.setSize(regionBounds.width, regionBounds.height);
shell.open();
tandis que (!shell.isDisposed()) {
si (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
région.dispose();
}
public static void main (String[] arguments) {
nouveau RingShell();
}
}