所以我在另一个面板顶部有一个图像,该图像是透明的,因此您可以看到它下方的面板。我想要做的是使用 repaint() 将图像(使用 java.awt.Graphics 中的 drawImage() 方法绘制)淡出,直到它完全透明,这样你就可以清楚地看到它下面的面板。截至目前,图像只是淡入黑色而不是透明纹理。
这是我现在的一些代码:
油漆组件方法:
public void paintComponent(Graphics g)
{
super.paintComponent(g);
float alpha = 1f-(.01f*(float)opcounter);
Graphics2D g2d = (Graphics2D)g;
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_IN, alpha);
g2d.drawImage(img, 0, 0, null);
}
为计时器调用的 actionPerformed 方法
public void actionPerformed(ActionEvent e)
{
opcouner++;
panel.repaint();
}
我的代码的更长(未压缩)版本:(包括用于计时器的paintComponent和Mover类)
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Dimension framesize = frame.getSize();
this.setBounds(0,0,framesize.width, framesize.height-61);
if (buff)
{
//this.add(buffer);
if (opcounter <= 255)
{
buffer.setForeground(new Color(250, 250, 250, 0+opcounter));
}
else
{
opcounter = 0;
buff = false;
hand = true;
}
}
if (hand)
{
this.add(handson);
if (opcounter <= 255)
{
handson.setForeground(new Color(250, 250, 250, 0+opcounter));
}
else
{
opcounter = 0;
hand = false;
log = true;
}
}
if (log)
{
this.add(logic);
if (opcounter <= 255)
{
logic.setForeground(new Color(250, 250, 250, 0+opcounter));
}
else
{
opcounter = 0;
log = false;
pan = true;
}
}
if (pan)
{
this.add(panic);
if (opcounter <= 255)
{
panic.setForeground(new Color(250, 250, 250, 0+opcounter));
}
else
{
opcounter = 0;
pan = false;
first = false;
second = true;
try
{
//Thread.sleep(2000);
}
catch(Exception e)
{
System.out.println("thread not slept");
}
System.out.println("opcounter = " + opcounter);
black.setVisible(false);
handson.setVisible(false);
logic.setVisible(false);
panic.setVisible(false);
tutpic = true;
}
}
if (tutpic)
{
if (opcounter <= 200)
{
Graphics2D g2d = (Graphics2D)g.create();
float alpha = 1f-(.01f*(float)opcounter);
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_IN, alpha));
g2d.drawImage(tut, 0, 0, null);
g2d.dispose();
}
else
{
opcounter = 0;
tutpic = false;
}
}
}
class Mover implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if (!tutpic)
opcounter+=4;
else
{
opcounter++;
}
tutorial.repaint();
}
}
任何帮助,将不胜感激。谢谢!