gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 06:32:04 25 4
gpt4 key购买 nike

我想让内容 Pane 透明,而那个蓝色 strip 是正常的深蓝色。但是在使 contentPane 透明的过程中,我也将该 strip 设为暗色(因为黑色被涂在上面) )不小心。

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()方法进行了更改。我得到以下输出正确的蓝色 strip ,但现在灰色半透明背景消失了。 enter image description here

最佳答案

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()函数进行绘画时,您将在绘制子组件panel之后使用图形实例g进行绘画。转到 super.paint(g) 函数的源代码,您将看到正在调用三个后续函数:

  • protected void PaintComponent(Graphics g):这个绘制您的组件,例如:背景
  • protected void PaintBorder(Graphics g):绘制组件的边框
  • protected void PaintChildren(Graphics g):此函数绘制其中组件的子组件

因此,在此 super.paint(g) 调用之后,您绘制的任何内容都将出现在使用上述三个函数制作的所有绘画之上:因此,在子组件之上,对于您的上下文,蓝色背景的面板

现在,解决方案是:

 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) 函数并在其中进行绘制。然后,您可以使用 setContentPane(component) 函数将 MyCanvas 的实例设置为 JFrame 的内容 Pane 。

查看 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);

}
}

关于java - 需要帮助来纠正我的小 Swing 代码中的绘画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19981153/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com