SWT(JFace)体验之圆环状(戒指型)_java

演示代码:

复制代码 代码如下:

package swt_jface.demo1;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.graphics.Region;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class RingShell {

int[] createCircle(int radius, int centerX, int centerY) {
int[] points = new int[360 * 2];
for(int i=0; i<360; i++) {
points[i*2] = centerX + (int)(radius * Math.cos(Math.toRadians(i)));
points[i*2+1] = centerY + (int)(radius * Math.sin(Math.toRadians(i)));
}
return points;
}

Point originalPosition = null;
public RingShell() {

final Display display = new Display();
final Shell shell = new Shell(display, SWT.NO_TRIM | SWT.ON_TOP);
shell.setBackground(display.getSystemColor(SWT.COLOR_DARK_MAGENTA));

Region region = new Region();
region.add(createCircle(100, 100, 100));
region.subtract(createCircle(50, 100, 100));
shell.setRegion(region);

shell.addMouseListener(new MouseListener() {
public void mouseDoubleClick(MouseEvent e) {
display.dispose();
}
public void mouseDown(MouseEvent e) {
originalPosition = new Point(e.x, e.y);
}
public void mouseUp(MouseEvent e) {
originalPosition = null;
}
});

shell.addMouseMoveListener(new MouseMoveListener() {
public void mouseMove(MouseEvent e) {
if(originalPosition == null) return;
Point point = display.map(shell, null, e.x, e.y);
shell.setLocation(point.x - originalPosition.x, point.y - originalPosition.y);
System.out.println("Moved from: " + originalPosition + " to " + point);
}
});

Rectangle regionBounds = region.getBounds();
shell.setSize(regionBounds.width, regionBounds.height);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
region.dispose();
}

public static void main(String[] args) {
new RingShell();
}
}

时间: 2024-10-03 07:48:55

SWT(JFace)体验之圆环状(戒指型)_java的相关文章

SWT(JFace)体验之Icon任我变_java

代码如下 复制代码 代码如下: package swt_jface.demo; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.layout.GridData; import or

SWT(JFace)体验之图片的动态渐变效果_Java编程

1.渐变: 复制代码 代码如下: package swt_jface.demo10; import org.eclipse.swt.SWT; import org.eclipse.swt.events.PaintEvent; import org.eclipse.swt.events.PaintListener; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.graphics.ImageData; import org

SWT(JFace)体验之Sash(活动控件)_Java编程

演示代码如下: 复制代码 代码如下: package swt_jface.demo9; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Sash; import org.eclipse.swt

SWT(JFace)体验之GridLayout布局_Java编程

GridLayout布局 GridLayout 布局的功能非常强大,也是笔者常用的一种布局方式.GridLayout是网格式布局,它把父组件分成一个表格,默认情况下每个子组件占据一个单元格的空间,每个子组件按添加到父组件的顺序排列在表格中.GridLayout提供了很多的属性,可以灵活设置网格的信息.另外,GridLayout 布局提供了GridData类,子组件可以设置相应的GridData,例如 "dogPhoto.setLayoutData(gridData)",GridData

SWT(JFace)体验之ViewForm的使用_Java编程

代码如下: 复制代码 代码如下: package swt_jface.demo9; import org.eclipse.swt.SWT; import org.eclipse.swt.custom.ViewForm; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.s

SWT(JFace) 体验之FontRegistry_Java编程

复制代码 代码如下: package swt_jface.demo; import org.eclipse.jface.resource.FontRegistry; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.FontData; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.ecli

SWT(JFace)体验之StyledText类_Java编程

WrapLines.java 复制代码 代码如下: package swt_jface.demo4; import org.eclipse.swt.SWT; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.

SWT(JFace)体验之StackLayout布局_Java编程

测试代码如下: 复制代码 代码如下: package swt_jface.demo2; import org.eclipse.swt.SWT; import org.eclipse.swt.custom.StackLayout; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.events.SelectionListener; import org.eclipse.swt.widgets.Button; i

SWT(JFace)体验之RowLayout布局_Java编程

RowLayout布局  相对于FillLayout来说,RowLayout比较灵活,功能也比较强.用户可以设置布局中子元素的大小.边距.换行及间距等属性. RowLayout的风格 RowLayout中可以以相关的属性设定布局的风格,用户可以通过"RowLayout.属性"的方式设置RowLayout的布局风格,RowLayout中常用的属性如下.Wrap:表示子组件是否可以换行(true为可换行).Pack:表示子组件是否为保持原有大小(true为保持原有大小).Justify:表