6

如何在 Java 中制作带有软边框的非矩形窗口?
软边框(也称为软剪裁)是没有锯齿伪影的边框。

我在网上搜索了很多,发现了几篇关于半透明和/或非矩形窗口的帖子。

“软边界”这个话题令人困惑。我发现的信息似乎涉及将软边框应用于另一个 Java 组件内的组件。

但是,我可以或不可以将软边框应用于仅放置在桌面上的自定义形状的 JWindow 吗?

我主要指的是以下帖子:
http ://today.java.net/pub/a/today/2008/03/18/translucent-and-shape-swing-windows.html

说到软剪辑,文章转发到
http://weblogs.java.net/blog/campbell/archive/2006/07/java_2d_tricker.html
但是这里描述的是对现有Graphics2D对象的软剪辑。

4

3 回答 3

3

这是我对软剪裁、成形的顶级窗口的看法。注意:异形窗口使用专有 API (com.sun.awt.AWTUtilities),不能保证在非 Sun JVM 上工作。但是,在 JDK 7 中,它成为 Window 类的一部分。

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;
    }
}
于 2010-05-07T20:56:11.483 回答
0

Have you read this article:

http://www.pushing-pixels.org/?p=272

It mentions soft clipping and the previous articles you mentioned, but also includes some source code to implement a soft clipped window, the direct link is here:

http://www.pushing-pixels.org/wp-content/uploads/2008/03/softclippedwindow.java

That should provide you with a possible solution for what you want to do.

于 2009-06-13T17:57:27.920 回答
-1
import java.awt.*;

public class First extends Applet
   {
   public void paint(Graphics g)
             {
             g.drawRect(100,50,500,800);
             }
   }
/*<Applet code="First.class"height=500 width=500>
  </Applet>
*/
于 2010-05-06T07:16:33.953 回答