import javax.swing.*;
import java.awt.*;
public class F extends JFrame implements Runnable{ public Image buffer;
private Graphics bg;
public Image[] imgLOGO;
private Thread runner;
private boolean isPainted=false;
private static int Frame_w=400,Frame_h=400;
private int buf_w=Frame_w,buf_h=Frame_h;
public F()
{
Init();
}
public void Init()
{
imgLOGO=new Image[2];
ImageIcon imgIcon = new ImageIcon("img/logo0.png");
imgLOGO[0]=imgIcon.getImage();
imgIcon = new ImageIcon("img/logo1.png");
imgLOGO[1]=imgIcon.getImage();
start();
}
public void update(Graphics g)
{
paint(g);
}
public void paint(Graphics g)
{
super.paint(g);
Dimension d=size();
Insets in =insets();

int client_w=d.width-in.right-in.left;
int client_h=d.height-in.bottom-in.top;
if(client_w!=buf_w || client_h!=buf_h)
{
buf_w=client_w;
buf_h=client_h;
}
buffer=createImage(buf_w,buf_h);
bg=buffer.getGraphics();
bg.drawImage(imgLOGO[0],0,0,null);
for(int i=0;i<=buf_w/imgLOGO[0].getWidth(null);i++)
{
for(int j=0;j<=buf_h/imgLOGO[0].getHeight(null);j++)
{
if(i+j>0)bg.copyArea(0,0,imgLOGO[0].getWidth(null),imgLOGO[0].getHeight(null),
i*imgLOGO[0].getWidth(null),j*imgLOGO[0].getHeight(null));
}
}
bg.drawImage(imgLOGO[1],(buf_w-imgLOGO[1].getWidth(null))/2,buf_h/8,null);
bg.dispose();
g.drawImage(buffer,in.left,in.top,this);
// g.drawImage(imgLOGO[0],0,0,getWidth(),getHeight(),this);
// g.drawImage(imgLOGO[1],0,0,getWidth(),getHeight(),this);
isPainted=true;
}
public void start()
{
if(runner==null)
{
runner=new Thread(this);
runner.start();
// System.out.println("runner is start");
}
}
public void run()
{
while(runner!=null)if(isPainted)
{
isPainted=false;
repaint();
try{Thread.sleep(200);}
catch(InterruptedException e){}
System.out.println("runner is run");
}
}
public static void main(String[] args) {
JFrame f=new F();
f.setTitle("LOGO");
f.setSize(300,300);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}
}

解决方案 »

  1.   

    不是用了双缓冲就可以避免闪烁的。双缓冲只是减少了闪烁感
    闪烁主要是由于绘制的颜色反差大,而且绘制速度慢造成的。
    你的绘制里面有一个for循环,你看看这段代码可以优化吗,如果这个绘制需要很长的时间,肯定有闪烁的
    再者,Swing里面不建议用paint方法,更何况你是在JFrame 里做的。JFrame是底层容器,不要把绘制的东西放到容器上。你在JFrame上面加一个JPanel,在JPanel里面绘制,用paintComponents来绘制。估计会好一点很久没有做GUI的东西了,可能不太正确
      

  2.   

    public void paint(Graphics g)这个方法的super.paint(g);注释掉
    接分