我想使内容窗格透明,而使正常的深蓝色蓝色条带化。但是在使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.:backgroundprotected void paintBorder(Graphics g)
: this one paints the border of the componentprotected void paintChildren(Graphics g)
: This one paints the children of the component in itSo 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)
功能并在其内部进行绘制。然后,您可以将实例设置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);
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句