摆动:如何使非矩形窗口软边界? [英] Swing: How to make non-rectangular windows with soft borders?
问题描述
我如何才能让非矩形窗口在Java软边界?
软边界(也称为软削波)是无边界的锯齿瑕疵。的
How could I make non-rectangular windows with soft borders in Java?
Soft borders (also known as soft clipping) are borders without aliasing artifacts.
我在网上搜索了很多,发现了大约几个职位半透明和/或
非矩形窗口。
I searched the web a lot and found several posts about translucent and/or non-rectangular windows.
主题软边界是混乱。看来这个信息,我发现交易
与应用软边界组件,它是的在另一个Java组件。
The topic "soft border" is confusing. It seems that the information I found deals with applying soft borders to component which are inside another Java components.
但是,我,或者我可以不应用软边界自定义形状的JWindow这是
刚放置在桌面上?
But, can I, or can I not apply soft borders to custom shaped JWindow which is placed just on the desktop?
我很好地指的是下面的帖子:
<一href=\"http://today.java.net/pub/a/today/2008/03/18/translucent-and-shaped-swing-windows.html\">http://today.java.net/pub/a/today/2008/03/18/translucent-and-shaped-swing-windows.html
I am primely referring to following post:
http://today.java.net/pub/a/today/2008/03/18/translucent-and-shaped-swing-windows.html
当涉及到软限幅,文章转发到
<一href=\"http://weblogs.java.net/blog/campbell/archive/2006/07/java%5F2d%5Ftricker.html\">http://weblogs.java.net/blog/campbell/archive/2006/07/java_2d_tricker.html
但在这里,在现有Graphics2D对象软削波描述。
When it comes to soft clipping, the article forwards to
http://weblogs.java.net/blog/campbell/archive/2006/07/java_2d_tricker.html
But here, soft clipping on an existing Graphics2D object is described.
推荐答案
下面是我拿一个柔性裁剪,形,顶层窗口。注:形状的窗户使用专有的API(com.sun.awt.AWTUtilities),并不能保证在非Sun的JVM工作。然而,在JDK 7就变成窗口类的一部分。
Here's my take on a soft-clipped, shaped, top-level window. Note: shaped windows use a proprietary API (com.sun.awt.AWTUtilities) and is not guaranteed to work on non-Sun JVMs. However, in JDK 7 it becomes part of the Window class.
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class MySoftClippedWindow extends JPanel {
public MySoftClippedWindow() {
super();
setLayout(new GridBagLayout());
JButton button = new JButton(new AbstractAction("Close") {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
button.setOpaque(false);
add(button);
}
@Override
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g.create();
int width = getWidth();
int height = getHeight();
// Create a soft clipped image for the background
BufferedImage img = java_2d_tricker(g2d, width, height);
g2d.drawImage(img, 0, 0, null);
g2d.dispose();
}
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
final JWindow w = new JWindow();
Container cp = w.getContentPane();
cp.setLayout(new BorderLayout());
cp.add(new MySoftClippedWindow());
w.setAlwaysOnTop(true);
com.sun.awt.AWTUtilities.setWindowOpaque (w, false);
w.setSize(200, 200);
w.setVisible(true);
}
});
}
/*
* This code is taken from
* http://weblogs.java.net/blog/campbell/archive/2006/07/java_2d_tricker.html
*/
private BufferedImage java_2d_tricker(Graphics2D g2d, int width, int height) {
GraphicsConfiguration gc = g2d.getDeviceConfiguration();
BufferedImage img = gc.createCompatibleImage(width, height, Transparency.TRANSLUCENT);
Graphics2D g2 = img.createGraphics();
g2.setComposite(AlphaComposite.Clear);
g2.fillRect(0, 0, width, height);
g2.setComposite(AlphaComposite.Src);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(Color.WHITE);
g2.fillOval(width / 4, height / 4, width / 2, height / 2);
g2.setComposite(AlphaComposite.SrcAtop);
g2.setPaint(new GradientPaint(0, 0, Color.RED, 0, height, Color.YELLOW));
g2.fillRect(0, 0, width, height);
g2.dispose();
return img;
}
}
这篇关于摆动:如何使非矩形窗口软边界?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!