Mar*_*rco 6 java swing java-2d
我想创建一个具有自定义形状和透明度的对话框,认为信息气泡指向某个组件.
为此,我添加了JPanel一个JDialog并覆盖paintComponent(Graphics)面板的方法.面板本身包含常规JLabels和JButtons.工作正常,但只要我Graphics2D.setClip(Shape)在面板中使用绘制代码,组件就会被背景透支.如果我没有设置剪辑(对于一个完全新鲜的Graphics2D对象,不能少),一切正常.这对我来说非常令人费解,我不知道我能做些什么来解决它.
PS:我无法使用setShape(Shape),JDialog因为那里没有抗锯齿功能.PPS:实际的用例是绘制一个大的背景图像,必须在信息气泡形状上切除.
当您多次鼠标悬停在右上角的"x"时,以下SSCCE会演示此问题.
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);
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:为了规避Windows上的这个特定错误,我添加了以下内容,但在我的用例中没有出现问题或性能命中(可能因您而异):
JDialog dialog = new Java2DTransparencySSCE() {
@Override
public void paint(Graphics g) {
g.setClip(null);
super.paint(g);
}
};
Run Code Online (Sandbox Code Playgroud)
当鼠标悬停在 上时JButton,面板仅使用需要重新绘制的边界重新绘制自身 - 按钮的边界(如果您选中oldClip它应该是 的边界JButton)。更改剪辑边界会导致 alpha 颜色成为每个先前绘制调用的合成,因为剪辑不会被清除super.paintComponent并且 JDialog 的背景是完全透明的。
我想创建一个具有自定义形状和透明度的对话框,认为信息气泡指向某个组件。
考虑使用轻量级组件方法 - 您可以通过设置包含对话框项的 JFrame 的玻璃窗格、根据需要切换玻璃窗格的可见性和/或内容来实现。