0

enter image description here

i like to create a java jframe look like this image.i have already crated jframes with different shapes like triangles ,circles ,polygons and some crazy shapes .but the problem it's too hard[99% impossible ] to create shape like this image.so how can i make a jframe like this.i used this code for create shaped window..

setUndecorated(true);
Polygon polygon = new Polygon();
polygon.addPoint(0, 0);
polygon.addPoint(100,100);

GeneralPath path = new GeneralPath();
path.append(polygon, true);
setShape(path);

now can i convert this image to a shape .then set setshapes.any idea? or is there anyway to make jframe's fully transperent and jlable which hold image completely visible?

while true
  • 816
  • 1
  • 11
  • 26
  • To be honest, you'll get a better result in you use a transparent frame and paint the image into the background, you will get antialiasing instead of sharp dropped pixels. With a little bit of clever work, you can also generate a clipping rectangle if you need it – MadProgrammer Oct 05 '14 at 20:25
  • @MadProgrammer when i search for that i found number of answers answered by you.can you explain bit more how to do that.i don't need code for that but the procedure. i'm not fluent in English.i tried by seting frame opacity to 0.but all frame hidden .and i try to set opaque false. it didn't work either – while true Oct 05 '14 at 23:56
  • so @MadProgrammer i remembered i did that in c# using transparent key.but i still didn't found a way to make window transparent and image completely visible – while true Oct 06 '14 at 00:06
  • 1
    Sorry that should be **don't** change the opacity, as it affects the frame and it's contents. Instead, you want to set the frames background color's alpha to 0 and make what ever you add to it transparent (opaque = false) – MadProgrammer Oct 06 '14 at 00:09
  • I've added a runnable example to demonstrate, with a little more explanation... – MadProgrammer Oct 06 '14 at 00:17

2 Answers2

4

To make a transparent window, you need to set the frames background color's alpha to 0. This is probably the most counter intuitive call I've seen in a while, as if you do this to any other Swing component, you will completely screw up the paint process.

You don't want to change the opacity of the window, as it effectives the entire window and it's contents equally.

For example...

JWindow frame = new JWindow();
frame.setBackground(new Color(0, 0, 0, 0));

You don't have to use a JWindow, but this means I don't need to undecorate it myself...

You also need to make sure that whatever content you add to the window is transparent (opaque = false), so that it doesn't "hide" what's underneath it...

Leaf Window

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JWindow;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

public class LeafWindow {

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

    public LeafWindow() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JWindow frame = new JWindow();
                frame.setBackground(new Color(0, 0, 0, 0));
                frame.setContentPane(new LeafPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
                frame.setAlwaysOnTop(true);
            }
        });
    }

    public class LeafPane extends JPanel {

        private BufferedImage leaf;

        public LeafPane() {

            setBorder(new CompoundBorder(
                            new LineBorder(Color.RED),
                            new EmptyBorder(0, 0, 250, 0)));

            try {
                leaf = ImageIO.read(getClass().getResource("/Leaf.png"));
            } catch (IOException ex) {
                ex.printStackTrace();
            }

            setOpaque(false);
            setLayout(new GridBagLayout());

            JButton button = new JButton("Close");
            button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                }
            });

            add(button);

        }

        @Override
        public Dimension getPreferredSize() {
            return leaf == null ? new Dimension(200, 200) : new Dimension(leaf.getWidth(), leaf.getHeight());
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (leaf != null) {
                Graphics2D g2d = (Graphics2D) g.create();
                g2d.drawImage(leaf, 0, 0, this);
                g2d.dispose();
            }
        }
    }

}

This example deliberate adds a line border to the content as you can see what the original window bounds would be. It also uses a EmptyBorder to force the JButton onto the graphics, but this is just an example...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

You'll have to create a shape based on your image. There are different threads here on SO that provide some way how to do this. The best one (based on the description, I didn't try it myself) might be Java - Create a shape from border around image. Another option for more complex images could be Image/Graphic into a Shape.

Another solution might be to use an undecorated frame along with "per-pixel transparency", as explained by Oracle here: http://docs.oracle.com/javase/tutorial/uiswing/misc/trans_shaped_windows.html

Community
  • 1
  • 1
cello
  • 5,356
  • 3
  • 23
  • 28