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

疯狂的编码器

我想使内容窗格透明,而使正常的深蓝色蓝色条带化。但是在使contentPane透明的过程中,我也意外地使该条带成暗淡的颜色(因为上面涂有黑色)。

在此处输入图片说明

我该如何纠正?

(注释油漆方法并注意条带中的变化,这是我想要的最终结果)

这是代码:

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()方法进行了更改。我得到以下输出Correct Blue Strip,但现在灰色的半透明背景已经消失了。 在此处输入图片说明

智者
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);
    }

When you are painting with paint() function you are painting with graphics instance g after your child component panel is painted. Go to the source of the super.paint(g) function and you will see that three subsequent function is being called:

  • protected void paintComponent(Graphics g): this one paint your component, e,g.:background
  • protected void paintBorder(Graphics g): this one paints the border of the component
  • protected void paintChildren(Graphics g): This one paints the children of the component in it

So after this super.paint(g) call, anything you draw will appear above all the painting made with these three above function: hence above the children components, for your context the panel with blue background.

Now, a solution would be:

 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)功能并在其内部进行绘制。然后,您可以将实例设置MyCanvasJFrame使用该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);

    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用Image Pointer处理图像平滑-需要帮助来纠正我的代码

来自分类Dev

使用Image Pointer处理图像平滑-需要帮助来纠正我的代码

来自分类Dev

需要帮助在我的python代码中添加受控循环

来自分类Dev

需要帮助来简化我的代码中的方法

来自分类Dev

需要帮助来修复我的JavaScript代码中的链接

来自分类Dev

我需要帮助处理 Jsx 代码中的 javascript 变量

来自分类Dev

我需要帮助理解 grep 函数中的模式代码

来自分类Dev

在我的 C 代码中需要帮助 - 获得意外输出

来自分类Dev

需要我编写的代码的帮助(需要理解)

来自分类Dev

需要指导我在Jython / Python中的小插图代码

来自分类Dev

需要帮助以减少代码(我不是专家)

来自分类Dev

我需要帮助以本机代码修改InAppBrowser

来自分类Dev

我需要修复代码的帮助-非常脆弱

来自分类Dev

在纠正横幅图像尺寸方面需要帮助

来自分类Dev

我在项目中需要帮助,如何在RecyclerView中更改Intent,这是我的代码

来自分类Dev

我需要代码方面的帮助,我无法在javascript中运行

来自分类Dev

需要帮助以了解python中的代码

来自分类Dev

需要帮助以了解python中的代码

来自分类Dev

需要帮助以C ++中的简单代码

来自分类Dev

请纠正我的熵代码

来自分类Dev

如何纠正我的Java代码?

来自分类Dev

我正在使用C ++,需要帮助来识别代码中的错误

来自分类Dev

需要帮助-错误:“;” 即使我在代码中包含了半冒号,也会显示预期的

来自分类Dev

需要帮助来修复我的数学网格迷宫求解器中的代码段

来自分类Dev

需要帮助修复错误并在我的 Android 应用中添加 FCM 代码

来自分类Dev

我需要帮助让我的代码看起来像这样:

来自分类Dev

我需要帮助以获取JavaScript中的URL

来自分类Dev

我在选择语句查询中需要帮助

来自分类Dev

在我的Quickbase Jquery脚本中需要帮助

Related 相关文章

  1. 1

    使用Image Pointer处理图像平滑-需要帮助来纠正我的代码

  2. 2

    使用Image Pointer处理图像平滑-需要帮助来纠正我的代码

  3. 3

    需要帮助在我的python代码中添加受控循环

  4. 4

    需要帮助来简化我的代码中的方法

  5. 5

    需要帮助来修复我的JavaScript代码中的链接

  6. 6

    我需要帮助处理 Jsx 代码中的 javascript 变量

  7. 7

    我需要帮助理解 grep 函数中的模式代码

  8. 8

    在我的 C 代码中需要帮助 - 获得意外输出

  9. 9

    需要我编写的代码的帮助(需要理解)

  10. 10

    需要指导我在Jython / Python中的小插图代码

  11. 11

    需要帮助以减少代码(我不是专家)

  12. 12

    我需要帮助以本机代码修改InAppBrowser

  13. 13

    我需要修复代码的帮助-非常脆弱

  14. 14

    在纠正横幅图像尺寸方面需要帮助

  15. 15

    我在项目中需要帮助,如何在RecyclerView中更改Intent,这是我的代码

  16. 16

    我需要代码方面的帮助,我无法在javascript中运行

  17. 17

    需要帮助以了解python中的代码

  18. 18

    需要帮助以了解python中的代码

  19. 19

    需要帮助以C ++中的简单代码

  20. 20

    请纠正我的熵代码

  21. 21

    如何纠正我的Java代码?

  22. 22

    我正在使用C ++,需要帮助来识别代码中的错误

  23. 23

    需要帮助-错误:“;” 即使我在代码中包含了半冒号,也会显示预期的

  24. 24

    需要帮助来修复我的数学网格迷宫求解器中的代码段

  25. 25

    需要帮助修复错误并在我的 Android 应用中添加 FCM 代码

  26. 26

    我需要帮助让我的代码看起来像这样:

  27. 27

    我需要帮助以获取JavaScript中的URL

  28. 28

    我在选择语句查询中需要帮助

  29. 29

    在我的Quickbase Jquery脚本中需要帮助

热门标签

归档