清洁图形清洁所有图形

时间:2015-07-03 10:45:51

标签: java button graphics transparent

我对Java Graphics有一点问题,我有一个特殊的JComponent,一个纹理按钮。当鼠标打开时,按钮变得更亮(它被半透明的白色填充),但图形不清洁,因此透明白色在组件上形成全白色背景,即使鼠标是不在按钮上。它看起来像这样:

My bugged button

所以我找到了解决方案,我补充说:

$initialDate = DateTime::createFromFormat('j-n-Y', '1-6-2015');

$initialDate->setTime(mt_rand(0,12),mt_rand(0,59),mt_rand(0,59)); 

$originalDate = clone $initialDate;

在我的组件paintComponent方法上,所以它工作了!但是,它并不仅仅清洁按钮背景,而是清除它背后的一切!没有按钮就是这样:

Without the button

使用按钮:

With the button

您可以在后台看到IDEA。

这是我的代码(请参阅fr.theshark34.swinger.textured.STexturedButton类): https://github.com/TheShark34/Swinger

1 个答案:

答案 0 :(得分:1)

你似乎对Swing中的绘画效果缺乏了解或误解,在任何一种情况下,它都是一种可怕的。

您可能希望仔细查看Painting in AWT and SwingPerforming Custom Painting,了解有关绘画如何在Swing中工作的详细信息

STexturedButton课程中,您不需要......

((Graphics2D) g).setComposite(AlphaComposite.getInstance(AlphaComposite.SRC));
g.setColor(Swinger.TRANSPARENT);
g.fillRect(0, 0, this.getWidth(), this.getHeight());

因为Swing已经(基本上)已经为你完成了(因为STexturedButton扩展自AbstractButton类,它从JComponent延伸,默认情况下是透明的,如果组件不是&透明,做你已经做过的事情本来就是危险的,并且结束了额外奇怪的油漆文物。)

所以在我的测试中,我把它拿出来了。

另外,我更倾向于覆盖getPreferredSize而不是提供您自己的setBounds方法,这对于一个开始而言是令人困惑的(因为该组件已经拥有了它{&1;}使用setBounds方法并使用getPreferredSize可以很好地使用现有的Swing API

由于您没有提供任何类型的可运行示例,我必须自己制作,但这些更改对我来说还不错。

Normal Highlighted

import fr.theshark34.swinger.textured.STexturedButton;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.LinearGradientPaint;
import java.awt.Point;
import java.awt.Rectangle;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Main {

    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());

            try {
                STexturedButton btn = new STexturedButton(ImageIO.read(getClass().getResource("/Pony.png")));
                add(btn);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            LinearGradientPaint lgp = new LinearGradientPaint(
                    new Point(0, 0),
                    new Point(0, getHeight()),
                    new float[]{
                        0.1428571428571429f,
                        0.2857142857142858f,
                        0.4285714285714287f,
                        0.5714285714285716f,
                        0.7142857142857145f,
                        0.8571428571428574f,
                        1f
                    },
                    new Color[]{Color.RED, Color.ORANGE, Color.GREEN, Color.YELLOW, Color.BLUE, Color.MAGENTA, Color.PINK}
            );
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setPaint(lgp);
            g2d.fill(new Rectangle(0, 0, getWidth(), getHeight()));
            g2d.dispose();
        }

    }

}

现在有一个工作透明窗口示例

Transparent Panel and Window

import fr.theshark34.swinger.textured.STexturedButton;
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.LinearGradientPaint;
import java.awt.Point;
import java.awt.Rectangle;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;

public class Main {

    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setUndecorated(true);
                frame.setBackground(new Color(0, 0, 0, 0));
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            setOpaque(false);

            try {
                STexturedButton btn = new STexturedButton(ImageIO.read(getClass().getResource("/Pony.png")));
                add(btn);
            } catch (IOException ex) {
                ex.printStackTrace();
            }

            setBorder(new LineBorder(Color.RED));
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

    }

}