Topic: 请问如何绘制不规则窗口?

  Print this page

1.请问如何绘制不规则窗口? Copy to clipboard
Posted by: happyshows
Posted on: 2005-03-30 16:18

如题,最好能做个简单的例子好参考一下。
比如一张原形图片做背景的APPLICATION。我知道C#调用WINDOWS的API可以做的,JAVA有什么简单点的办法做呢?
THX

2.Re:请问如何绘制不规则窗口? [Re: happyshows] Copy to clipboard
Posted by: Sunteya
Posted on: 2005-03-31 18:27

swing 可能使用 JWindow做吧。
下面的这段代码是看.SWT:The Standard Widget Toolkit, Volume.1 看到的
用SWT 做的,希望对你有用

这个是刚刚看到人家推荐的网站。可能上面也有吧 你去找找咯
http://www.java2s.com/ExampleCode/SWT-JFace-Eclipse1.htm


static final int POINTS = 11;

public static void main(String[] args) {
final Point center = new Point(0, 0);
final int[] radial = new int[POINTS * 2];

final Display display = new Display();
final Color black = display.getSystemColor(SWT.COLOR_BLACK);

final Shell shell = new Shell(display, SWT.NO_TRIM);
shell.setBackground(black);
shell.setSize(200, 200);

Rectangle bounds = shell.getClientArea();

center.x = bounds.x + bounds.width / 2;
center.y = bounds.y + bounds.height / 2;

int pos = 0;
for (int i = 0; i < POINTS; ++i) {
double r = Math.PI * 2 * pos / POINTS;
radial[i * 2] = (int) ((1 + Math.cos(r)) * center.x);
radial[i * 2 + 1] = (int) ((1 + Math.sin(r)) * center.y);
pos = (pos + POINTS / 2) % POINTS;

}

Listener listener = new Listener() {
int offsetX = 0, offsetY = 0;

public void handleEvent(Event e) {
switch (e.type) {
case SWT.MouseDown:
if (e.button == 1) {
offsetX = e.x;
offsetY = e.y;
}
break;
case SWT.MouseMove:
if ((e.stateMask & SWT.BUTTON1) != 0) {
Point pt = shell.toDisplay(e.x, e.y);
pt.x -= offsetX;
pt.y -= offsetY;
shell.setLocation(pt);
}
break;
case SWT.KeyDown:
shell.dispose();
break;
}
}
};

shell.addListener(SWT.MouseDown, listener);
shell.addListener(SWT.MouseMove, listener);
shell.addListener(SWT.KeyDown, listener);

Region region = new Region(display);
region.add(radial);

shell.setRegion(region);
shell.open();

while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
region.dispose();
display.dispose();
}

3.Re:请问如何绘制不规则窗口? [Re: happyshows] Copy to clipboard
Posted by: yipsilon
Posted on: 2005-03-31 21:41

RingShell样例可能是你需要的.


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);
// Make the shell movable by using the mouse pointer.
shell.addMouseListener(new MouseListener() {
public void mouseDoubleClick(MouseEvent e) {
shell.dispose(); // Double click to dispose the shell.
}

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();

// Set up the event loop.
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
// If no more entries in event queue
display.sleep();
}
}
display.dispose();
region.dispose();
}

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

4.Re:请问如何绘制不规则窗口? [Re: happyshows] Copy to clipboard
Posted by: happyshows
Posted on: 2005-04-02 14:44

谢谢,SWT没接触过,能不能说说SWING 的JWINDOW样例吗?


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923