Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add picture out of JFrame, attached to border

Tags:

java

jframe

Hi I Would like add to my JFrame border some image. Is this Possible to attach picture to borders for JFrame and create it as 1 object ?

Something like this:

enter image description here

like image 460
MTo Avatar asked Mar 08 '14 22:03

MTo


1 Answers

I'm not sure if it's possible to add the image directly to the border of a JFrame (suggestions welcome). I decided to solve this issue by using a transparent content pane, and using an inner frame to "appear" like the outer frame.

The code is pretty simple, however, let me know if you'd like an explanation of how the code works.

Here's the minimum code you'll need to get up and running.

You'll need to provide your own transparent-phone.png image, in the root of the classpath (i.e. next to your PhoneWindow.java file, in the root package).

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;

public class PhoneWindow {

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

    public PhoneWindow() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    final JFrame frame = new JFrame();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    // create the  inner frame
                    final JInternalFrame frame2 = new JInternalFrame("My Telephone");
                    frame2.setClosable(true);
                    frame2.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
                    // add elements to the outer frame
                    frame.setUndecorated(true);
                    frame.setBackground(new Color(0, 0, 0, 0));
                    JPanel pane = new TranslucentPane();
                    frame.setContentPane(pane);
                    frame.setLayout(new BorderLayout());
                    // add inner frame and phone picture
                    frame.add(frame2, BorderLayout.CENTER);
                    frame.add(new JLabel(new ImageIcon(ImageIO.read(getClass().getResource("/transparent-phone.png")))), BorderLayout.EAST);
                    frame.setLocationRelativeTo(null);
                    frame.setMinimumSize(new Dimension(400, 300));
                    frame.pack();
                    // show
                    frame2.setVisible(true);
                    frame.setVisible(true);
                } catch (Throwable ex) {
                    ex.printStackTrace();
                }

            }
        });
    }

    public class TranslucentPane extends JPanel {

        public TranslucentPane() {
            setOpaque(false);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setComposite(AlphaComposite.SrcOver.derive(0f));
            g2d.setColor(getBackground());
            g2d.fillRect(0, 0, getWidth(), getHeight());
        }

    }

}

Here's the full Java class (including close and draggable behaviour)

https://gist.github.com/nickgrealy/16901a6428cb79d4f179

And here's a screenshot of the final product

N.B. the transparent sections inside/outside the phone.

The final result with transparency.

References:

  • How to make a transparent JFrame but keep everything else the same?
  • Trying to disable dragging of a JInternalFrame
like image 153
Nick Grealy Avatar answered Oct 18 '22 16:10

Nick Grealy