【发布时间】:2015-08-27 16:43:26
【问题描述】:
我想创建一个具有自定义形状和透明度的对话框,想想指向某个组件的信息气泡。
为此,我将JPanel 添加到JDialog 并覆盖面板的paintComponent(Graphics) 方法。面板本身包含常规的JLabels 和JButtons。
工作正常,但只要我在面板绘制代码中使用Graphics2D.setClip(Shape),组件就会被背景透支。如果我不设置剪辑(设置为全新的Graphics2D 对象,不少于),一切正常。
这让我很困惑,我不知道我能做些什么来解决它。
P.S.:我不能在 JDialog 上使用 setShape(Shape),因为那里没有抗锯齿功能。
P.P.S.:实际用例是绘制一个大的背景图像,必须完全按照信息气泡的形状进行切割。
以下 SSCCE 演示了当您将鼠标多次悬停在右上角的“x”上时出现的问题。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import java.awt.Polygon;
import java.awt.Shape;
public class Java2DTransparencySSCE extends JDialog {
JPanel panel;
private JButton close;
private JLabel headline;
private JLabel mainText;
public Java2DTransparencySSCE() {
setLayout(new BorderLayout());
setFocusable(false);
setFocusableWindowState(false);
setUndecorated(true);
setBackground(new Color(0, 0, 0, 0));
panel = new JPanel(new GridBagLayout()) {
@Override
public void paintComponent(final Graphics g) {
super.paintComponent(g);
Graphics2D gImg = (Graphics2D) g.create();
// if the line below is removed, everything works..
Shape oldClip= gImg.getClip();
// both the shape and a standard rectangular clip break everything
// gImg.setClip(50, 50, 50, 50);
Polygon shape = new Polygon(new int[] {0, 100, 100, 50, 0}, new int[] {200, 200, 100, 50, 100}, 5);
gImg.setClip(shape);
gImg.setColor(new Color(255, 0, 0, 50));
gImg.fill(shape);
gImg.setClip(oldClip);
gImg.dispose();
}
};
panel.setOpaque(false);
add(panel, BorderLayout.CENTER);
headline = new JLabel("This is a title");
mainText = new JLabel("<html><div style=\"line-height: 150%;width:100px \">This is some sort of text which is rather long and thus pretty boring to actually read. Thanks for reading anyway!</div></html>");
close = new JButton("X");
close.setBorderPainted(false);
close.setContentAreaFilled(false);
close.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
layoutPanel();
}
private void layoutPanel() {
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = 0;
constraints.insets = new Insets(10, 10, 10, 10);
constraints.fill = GridBagConstraints.BOTH;
constraints.weightx = 1;
panel.add(headline, constraints);
constraints.gridx += 1;
constraints.weightx = 0;
constraints.fill = GridBagConstraints.NONE;
panel.add(close, constraints);
constraints.gridx = 0;
constraints.gridy += 1;
constraints.gridwidth = 2;
constraints.weightx = 1;
constraints.weighty = 1;
constraints.fill = GridBagConstraints.BOTH;
panel.add(mainText, constraints);
pack();
}
public static void main(String[] args) {
JFrame parent = new JFrame();
JPanel pane = new JPanel(new BorderLayout());
pane.add(new JTextArea("This is a text."));
parent.setContentPane(pane);
parent.setSize(400, 400);
parent.setLocation(400, 300);
parent.setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE);
parent.setVisible(true);
JDialog dialog = new Java2DTransparencySSCE();
dialog.setLocation(500, 400);
dialog.setAlwaysOnTop(true);
dialog.setVisible(true);
}
}
编辑:为了规避 Windows 上的这个特定错误,我添加了以下内容,在我的用例中不会导致问题或性能下降(可能因您而异):
JDialog dialog = new Java2DTransparencySSCE() {
@Override
public void paint(Graphics g) {
g.setClip(null);
super.paint(g);
}
};
【问题讨论】:
-
当你把 gImg.setClip(null);在 gImg.dispose() 之前; ?我猜 gImg.setClip() 也会影响 g,所以其他按钮会被剪掉。
-
不改变任何东西。将其归零或将其设置回旧剪辑都没有帮助。
-
在 Mac OS X 10.9、Java 8 上看起来不错,
-
@trashgod 好奇!没想到值得一提的是这发生在 Windows 7、Java8 上。
-
在 Linux、Java(TM) SE 运行时环境(内部版本 1.8.0_05-b13)上看起来也不错。不过,我也可以确认 Windows 上的损坏行为。