我看到一个是做方形控件的例子,想把它改成是圆形的,不知道怎么弄。
请问各路到手达人,帮着看看,谢谢。
/*******************************************************************************
* Copyright (c) 2000, 2005 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.swt.snippets;import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
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.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;/**
* Region snippet: Create non-rectangular shell from an image with transparency
*
* For a list of all SWT example snippets see
* http://www.eclipse.org/swt/snippets/
*
* @since 3.2
*/
public class Snippet219 {
public static void main(String[] args) {
final Display display = new Display();
final Image image = display.getSystemImage(SWT.ICON_INFORMATION);
final Shell shell = new Shell(display, SWT.NO_TRIM);
Region region = new Region();
final ImageData imageData = image.getImageData();
if (imageData.alphaData != null) {
Rectangle pixel = new Rectangle(0, 0, 1, 1);
for (int y = 0; y < imageData.height; y++) {
for (int x = 0; x < imageData.width; x++) {
if (imageData.getAlpha(x, y) == 255) {
pixel.x = imageData.x + x;
pixel.y = imageData.y + y;
region.add(pixel);
}
}
}
} else {
ImageData mask = imageData.getTransparencyMask();
Rectangle pixel = new Rectangle(0, 0, 1, 1);
for (int y = 0; y < mask.height; y++) {
for (int x = 0; x < mask.width; x++) {
if (mask.getPixel(x, y) != 0) {
pixel.x = imageData.x + x;
pixel.y = imageData.y + y;
region.add(pixel);
}
}
}
}
shell.setRegion(region); Listener l = new Listener() {
int startX, startY; public void handleEvent(Event e) {
if (e.type == SWT.KeyDown && e.character == SWT.ESC) {
shell.dispose();
}
if (e.type == SWT.MouseDown && e.button == 1) {
startX = e.x;
startY = e.y;
}
if (e.type == SWT.MouseMove && (e.stateMask & SWT.BUTTON1) != 0) {
Point p = shell.toDisplay(e.x, e.y);
p.x -= startX;
p.y -= startY;
shell.setLocation(p);
}
if (e.type == SWT.Paint) {
e.gc.drawImage(image, imageData.x, imageData.y);
}
}
};
shell.addListener(SWT.KeyDown, l);
shell.addListener(SWT.MouseDown, l);
shell.addListener(SWT.MouseMove, l);
shell.addListener(SWT.Paint, l); shell.setSize(imageData.x + imageData.width, imageData.y
+ imageData.height);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
region.dispose();
image.dispose();
display.dispose();
}
}
请问各路到手达人,帮着看看,谢谢。
/*******************************************************************************
* Copyright (c) 2000, 2005 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.swt.snippets;import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
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.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;/**
* Region snippet: Create non-rectangular shell from an image with transparency
*
* For a list of all SWT example snippets see
* http://www.eclipse.org/swt/snippets/
*
* @since 3.2
*/
public class Snippet219 {
public static void main(String[] args) {
final Display display = new Display();
final Image image = display.getSystemImage(SWT.ICON_INFORMATION);
final Shell shell = new Shell(display, SWT.NO_TRIM);
Region region = new Region();
final ImageData imageData = image.getImageData();
if (imageData.alphaData != null) {
Rectangle pixel = new Rectangle(0, 0, 1, 1);
for (int y = 0; y < imageData.height; y++) {
for (int x = 0; x < imageData.width; x++) {
if (imageData.getAlpha(x, y) == 255) {
pixel.x = imageData.x + x;
pixel.y = imageData.y + y;
region.add(pixel);
}
}
}
} else {
ImageData mask = imageData.getTransparencyMask();
Rectangle pixel = new Rectangle(0, 0, 1, 1);
for (int y = 0; y < mask.height; y++) {
for (int x = 0; x < mask.width; x++) {
if (mask.getPixel(x, y) != 0) {
pixel.x = imageData.x + x;
pixel.y = imageData.y + y;
region.add(pixel);
}
}
}
}
shell.setRegion(region); Listener l = new Listener() {
int startX, startY; public void handleEvent(Event e) {
if (e.type == SWT.KeyDown && e.character == SWT.ESC) {
shell.dispose();
}
if (e.type == SWT.MouseDown && e.button == 1) {
startX = e.x;
startY = e.y;
}
if (e.type == SWT.MouseMove && (e.stateMask & SWT.BUTTON1) != 0) {
Point p = shell.toDisplay(e.x, e.y);
p.x -= startX;
p.y -= startY;
shell.setLocation(p);
}
if (e.type == SWT.Paint) {
e.gc.drawImage(image, imageData.x, imageData.y);
}
}
};
shell.addListener(SWT.KeyDown, l);
shell.addListener(SWT.MouseDown, l);
shell.addListener(SWT.MouseMove, l);
shell.addListener(SWT.Paint, l); shell.setSize(imageData.x + imageData.width, imageData.y
+ imageData.height);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
region.dispose();
image.dispose();
display.dispose();
}
}
解决方案 »
- java Swing中如何给 大小动态变化的图片设置滚动条?
- RE:关于BufferedInputStream读取网络流的问题
- WSDL文件生成
- 关于HashMap的问题
- 请问谁有采用k-均值方法进行聚类的java程序实现呢?
- 在JPanel上绘制图形后,改变窗口大小,图形就消失了,如何解决?
- jvm 致命问题
- 集成情况下的session
- 请问为什么我从数据库中读数据正常,而写数据是乱码呢?
- 如果在action里弹出一个jsp小窗口 还要把action里的一个list传过去
- SQL problems: 接收 TDS 预登录响应时发生 I/O 错误
- 怎样防止别人用类的反射机制调用单例模式的private构造方法
final Display display = new Display();
final Shell shell = new Shell(display, SWT.NO_TRIM);
Region region = new Region();
region.add(circle(20, 500, 300)); shell.setRegion(region);
shell.setBackground(display.getSystemColor(SWT.COLOR_DARK_GRAY));
Listener l = new Listener() {
int startX, startY; public void handleEvent(Event e) {
if (e.type == SWT.KeyDown && e.character == SWT.ESC) {
shell.dispose();
}
if (e.type == SWT.MouseDown && e.button == 1) {
startX = e.x;
startY = e.y;
}
if (e.type == SWT.MouseMove && (e.stateMask & SWT.BUTTON1) != 0) {
Point p = shell.toDisplay(e.x, e.y);
p.x -= startX;
p.y -= startY;
shell.setLocation(p);
}
}
};
shell.addListener(SWT.KeyDown, l);
shell.addListener(SWT.MouseDown, l);
shell.addListener(SWT.MouseMove, l);
shell.addListener(SWT.Paint, l); shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
region.dispose();
display.dispose(); } static int[] circle(int r, int offsetX, int offsetY) {
int[] polygon = new int[8 * r + 4];
// x^2 + y^2 = r^2
for (int i = 0; i < 2 * r + 1; i++) {
int x = i - r;
int y = (int)Math.sqrt(r*r - x*x);
polygon[2*i] = offsetX + x;
polygon[2*i+1] = offsetY + y;
polygon[8*r - 2*i - 2] = offsetX + x;
polygon[8*r - 2*i - 1] = offsetY - y;
}
return polygon;
}
不过你给的代码都是界面的。要是界面的话 你已经达到目的了。只是要注意的是加载图片的大小会影响速度。
如果是要不规则的button、label之类的控件的话,我没有直接解决的方法,只能说说
我的方式是,利用图片的显示效果,如控件背景图片的底色与界面的底色相同,然后上面画个不规则的按钮,
同样准备几张相同的图片,做出鼠标选中、离开等效果。
在鼠标事件里设置一下,不同事件显示不同图片。基本上就可以达到目的了。一点浅见,不知道能不能帮到你。
把image改成你想要的图片就可以了,但是 需要注意的是图片的背景色得用透明的,不然显示的效果和没用Region的效果是一样的
Good Luck
--------------------------------------------------
Quietly through ....