如题↑↑↑
package eclipsecookbook;import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Region;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
public class NonRectanglegularClass {
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display, SWT.NO_TRIM);
Region region = new Region();
region.add(createCircle(50, 50, 50));
region.subtract(createCircle(50, 50, 20));
shell.setRegion(region);
shell.setSize(region.getBounds().width, region.getBounds().height);
shell.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
Button button = new Button(shell, SWT.PUSH);
button.setText("Exit");
button.setBounds(35, 6, 35, 20);
button.addListener(SWT.Selection, new Listener() { @Override
public void handleEvent(Event arg0) {
shell.close();
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose(); } static int[] createCircle(int xOffset, int yOffset, int radius) {
int[] circlePoints = new int[10 * radius];
for (int loopIndex = 0; loopIndex < 2* radius + 1; loopIndex++) {
int xCurrent = loopIndex - radius;
int yCurrent = (int) Math.sqrt(radius * radius - xCurrent
* xCurrent);
int doubleLoopIndex = 2 * loopIndex;
circlePoints[doubleLoopIndex] = xCurrent + xOffset;
circlePoints[doubleLoopIndex + 1] = yCurrent + yOffset;
circlePoints[10 * radius - doubleLoopIndex - 2] = xCurrent
+ xOffset;
circlePoints[10 * radius - doubleLoopIndex - 1] = -yCurrent
+ yOffset;
}
return circlePoints;
}
}

解决方案 »

  1.   

    楼主你想问的是,程序是如何计算出点阵的么?是用直角三角计算公式来算的:
    for (int loopIndex = 0; loopIndex < 2* radius + 1; loopIndex++) { // 注意它循环到半径×2
      int xCurrent = loopIndex - radius; // 得到x坐标,结合循环起至条件,相当于从 (圆心-半径) --> (圆心+半径)
      int yCurrent = int (int) Math.sqrt(radius * radius - xCurrent * xCurrent); // 直角三角形,已知斜边和直角边的边长,求另一个直角边
    这种计算过程,每次实际上一个x坐标会计算出两个y坐标(以圆心所在Y轴的轴对称上),所以后续赋值的时候,是两个点:
                circlePoints[doubleLoopIndex] = xCurrent + xOffset;
                circlePoints[doubleLoopIndex + 1] = yCurrent + yOffset; // 这个是正的
                circlePoints[10 * radius - doubleLoopIndex - 2] = xCurrent + xOffset;
                circlePoints[10 * radius - doubleLoopIndex - 1] = -yCurrent + yOffset; // 这个是负的(对称)
    楼主可以把 yOffset 和 xOffset 先设置为0,也即圆心就是原点,然后简化这个过程来想一想。