需要帮助纠正我的小Swing代码中的绘画

时间:2013-11-14 15:07:18

标签: java swing user-interface

我想让内容窗格透明,蓝色条纹是正常的深蓝色。但是在使contentPane透明化的过程中,我也将那条条纹变成暗色(因为它上面涂着黑色的颜色) )不小心。

enter image description here

我该如何纠正?

(注释掉paint方法并注意strip的变化。这是我想要的最终结果)

以下是代码:

class Home extends JFrame
{
    int width=GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().width;
    int height=GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().height;
    public Home()
    {
        super("WiND");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setUndecorated(true);
        setSize(width,height);
        setBackground(new Color(0,0,0,0));
        setUndecorated(true);
        setVisible(true);
        setLayout(new FlowLayout());

        JPanel p=new JPanel();
        p.setBackground(new Color(0x0D70E8));
        p.setPreferredSize(new Dimension(width,height/10));
        add(p);
    }
    public void paint(Graphics g)
    {
        super.paint(g);
        Graphics2D g2=(Graphics2D)g;
        LinearGradientPaint p=new LinearGradientPaint(0,0,0,height,new float[]{0f,1f},new Color[]{new Color(0.2498f,0.2498f,0.2498f,0.3f),new Color(0.1598f,0.1598f,0.1598f,0.8f)});
        g2.setPaint(p);
        g2.fillRect(0, 0, width,height);
    }
}

(一年前我做了同样的事情,但现在一年后我忘了我是怎么做的)

修改

我只根据@ Sage对paint()方法进行了更改。我得到以下输出 正确的蓝色条带,但现在灰色的半透明背景已经消失。 enter image description here

1 个答案:

答案 0 :(得分:2)

public void paint(Graphics g)
    {
        super.paint(g);
        Graphics2D g2=(Graphics2D)g;
        LinearGradientPaint p=new LinearGradientPaint(0,0,0,height,new float[]{0f,1f},new Color[]{new Color(0.2498f,0.2498f,0.2498f,0.3f),new Color(0.1598f,0.1598f,0.1598f,0.8f)});
        g2.setPaint(p);
        g2.fillRect(0, 0, width,height);
    }

当您使用paint()函数进行绘制时,在绘制子组件g后,您将使用图形实例panel进行绘制。转到super.paint(g)函数的源代码,您将看到正在调用三个后续函数:

  • protected void paintComponent(Graphics g):这个画你的组件,例如:背景
  • protected void paintBorder(Graphics g):这个绘制组件的边框
  • protected void paintChildren(Graphics g):这个描绘了其中组件的子项

因此,在此super.paint(g)调用之后,您绘制的任何内容都将显示在使用上述三个函数绘制的所有绘图上方:因此在子组件上方,对于您的上下文,panel具有蓝色背景。

现在,解决方案是:

 public void paint(Graphics g)
{

            Graphics2D g2 = (Graphics2D)g.create();
                 // note, we are creating a graphics object here for safe painting
            LinearGradientPaint p=new LinearGradientPaint(0,0,0,height,new float[]{0f,1f},new Color[]{new Color(0.2498f,0.2498f,0.2498f,0.3f),new Color(0.1598f,0.1598f,0.1598f,0.8f)});
            g2.setPaint(p);
            g2.fillRect(0, 0, width,height);
            g2.dispose(); // disposing the object which we created 

            super.paint(g);

    }

然而,不是这样做,而是使用诸如MyCanvas extends JComponent之类的类并覆盖它的paintComponent(Graphics)函数并在其中绘制。然后,您可以使用MyCanvas函数将JFrame的实例设置为setContentPane(component)的内容窗格。

查看A Closer Look at the Paint Mechanism


编辑您的用例的小型演示实现:

class AMyContainer extends JComponent
{

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g); 

        Graphics2D g2 = (Graphics2D)g.create();
                 // note, we are creating a graphics object here for safe painting
            LinearGradientPaint p=new LinearGradientPaint(0, 0, 0, getHeight(),new float[]{0f,1f},new Color[]{new Color(0.2498f,0.2498f,0.2498f,0.3f),new Color(0.1598f,0.1598f,0.1598f,0.8f)});
            g2.setPaint(p);
            g2.fillRect(0, 0, getWidth(), getHeight());
            g2.dispose(); // disposing the object which we created 
    }

}

class Home extends JFrame
{
    int width=GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().width;
    int height=GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().height;
    public Home()
    {
        super("WiND");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setUndecorated(true);
        setSize(width,height);
        setBackground(new Color(0,0,0,0));
        setUndecorated(true);

        JComponent container = new AMyContainer();
        container.setLayout(new FlowLayout());
        add(container);

        JPanel p=new JPanel();
        p.setBackground(new Color(0x0D70E8));
        p.setPreferredSize(new Dimension(width,height/10));
        container.add(p);
    }



    public static void main(String[] args)
    {
        new Home().setVisible(true);

    }
}