我想让内容窗格透明,蓝色条纹是正常的深蓝色。但是在使contentPane透明化的过程中,我也将那条条纹变成暗色(因为它上面涂着黑色的颜色) )不小心。
我该如何纠正?
(注释掉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()方法进行了更改。我得到以下输出 正确的蓝色条带,但现在灰色的半透明背景已经消失。
答案 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);
}
}