Java自定义形状框架使用图像 [英] java custom shaped frame using image

查看:49
本文介绍了Java自定义形状框架使用图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢创建一个像这样的图像的Java jframe.我已经创建了具有不同形状的jframe,例如三角形,圆形,多边形和一些疯狂的形状.但是问题很难[99%不可能]创建这样的形状image.so如何制作这样的jframe.我使用此代码创建了成形窗口.

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);

现在我可以将此图像转换为形状.然后设置setshapes.任何想法吗? 还是有办法使jframe的完全透明和jlable(使图像完全可见)?

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?

推荐答案

要创建透明窗口,您需要将框架背景色的alpha设置为0.这可能是我一段时间以来见到的最不直观的调用,就像对其他任何Swing组件执行此操作一样,您将完全搞乱绘制过程.

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.

例如...

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

您不必使用JWindow,但这意味着我不需要自己对它进行装饰...

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

您还需要确保添加到窗口中的任何内容都是透明的(opaque = false),以便它不会隐藏"其下面的内容...

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...

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();
            }
        }
    }

}

此示例故意为内容添加线边框,因为您可以看到原始的窗口边界.它还使用EmptyBorder强制JButton到图形上,但这只是一个示例...

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...

这篇关于Java自定义形状框架使用图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆