views:

602

answers:

3

Hello!

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.

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.

But, can I, or can I not apply soft borders to custom shaped JWindow which is placed just on the desktop?

I am primely referring to following post:
http://today.java.net/pub/a/today/2008/03/18/translucent-and-shaped-swing-windows.html

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.

A: 

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.

Jon
Sorry, it is not what I'm looking for. The given example displays a soft-clipped window **inside** another Swing window. It seems, there is no simple solution. I guess one could achieve my intention by capturing the desktop and involve that data to simulate soft clipping, ... Too complex! ;)
ivan_ivanovich_ivanoff
No, it's not. Both windows are top level windows. Try running the SoftClippedWindow example and moving the original window. The code is trying to show too many things at once, but if you trim out the reflection code, you can get a soft clipped top level window.
Devon_C_Miller
A: 

import java.awt.*;

public class First extends Applet { public void paint(Graphics g) { g.drawRect(100,50,500,800); } } /* */

sa
+1  A: 

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;
    }
}
Devon_C_Miller