非常奇怪的Java2D setClip()效果 - bug?

时间:2015-06-11 19:46:45

标签: java swing java-2d

我想创建一个具有自定义形状和透明度的对话框,认为信息气泡指向某个组件。

为此,我向JPanel添加了JDialog并覆盖了该面板的paintComponent(Graphics)方法。该面板本身包含常规JLabelsJButtons。 工作正常,但只要我在面板绘制代码中使用Graphics2D.setClip(Shape),组件就会被后台透支。如果我没有设置剪辑(对于一个完全新鲜的Graphics2D对象,不能少),一切正常。 这对我来说非常令人费解,我不知道我能做些什么来解决它。

P.S。:我无法在setShape(Shape)上使用JDialog,因为那里没有抗锯齿功能。 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);
        }
};

4 个答案:

答案 0 :(得分:1)

您应该在自定义绘图之前存储原始剪辑并在之后恢复。

Shape oldCLip=g2d.getClip();
...
your code
...
g2d.setClip(oldClip);

答案 1 :(得分:1)

在Mac OS X 10.9,Java 8上,我没有看到异常。我看到与下面的变体相同的外观,其中既不创建也不处理派生的图形上下文。 API建议程序员只有在直接从组件或其他Graphics对象创建时才使用Graphics对象调用dispose。&#34;我不确定这些实现在内部有何不同,但这可能是罪魁祸首。

image

@Override
public void paintComponent(final Graphics g) {
    super.paintComponent(g);
    Graphics2D gImg = (Graphics2D) g;
    Shape oldClip= gImg.getClip();
    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);
}

答案 2 :(得分:0)

当鼠标悬停在JButton上时,面板重新绘制自己只需要重绘所需的边界 - 按钮的边界(如果你检查oldClip它应该是{{1}的边界}})。更改剪辑边界会导致Alpha颜色成为每个先前绘制调用的合成,因为剪辑未被JButton清除,并且JDialog的背景完全透明。

  

我想创建一个具有自定义形状和透明度的对话框,认为信息气泡指向某个组件。

考虑使用轻量级组件方法 - 您可以通过设置包含对话框项目的JFrame的玻璃窗格,根据需要切换玻璃窗格的可见性和/或内容。

答案 3 :(得分:-1)

您应该在所有绘制代码​​之后调用super.paintComponent(g)(它将是覆盖的最后一行)。

这样您的绘图将位于组件子项下方。