java自定义形状框架使用图像

在此处输入图像描述

我喜欢创建一个java jframe看起来像这个image.i已经创建了具有不同形状的jframes,如三角形,圆形,多边形和一些疯狂的形状。但问题是它太难[99%不可能]创建像这个图像的形状。所以我如何制作像这样的jframe.i使用此代码创建形状窗口..

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.any的想法? 或者无论如何使jframe的完全transperent和jlable保持图像完全可见?

要创建透明窗口,您需要将帧背景颜色的alpha设置为0 。 这可能是我在一段时间内看到的最反直觉的调用,就好像你对任何其他Swing组件这样做,你将完全搞砸了绘画过程。

您不希望更改窗口的不透明度,因为它会使整个窗口及其内容同等有效。

例如…

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

你不必使用JWindow ,但这意味着我不需要自己解开它…

您还需要确保添加到窗口的任何内容都是透明的(opaque = false),这样它就不会“隐藏”它下面的内容……

叶窗

 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进入图形,但这只是一个例子……

您必须根据图像创建形状。 SO上有不同的线程提供了一些如何做到这一点的方法。 最好的一个(基于描述,我没有自己尝试)可能是Java – 从图像边界创建一个形状 。 更复杂图像的另一种选择可以是图像/图形到形状 。

另一种解决方案可能是使用未修饰的框架以及“每像素透明度”,如Oracle在此处所解释的: http : //docs.oracle.com/javase/tutorial/uiswing/misc/trans_shaped_windows.html