畢業(yè)設(shè)計(jì)有個(gè)遠(yuǎn)程協(xié)助功能,得到對(duì)方的屏幕后,老是會(huì)閃,很是不爽,今天用java的雙緩沖技術(shù)解決了。代碼如下,本類重寫了Swing中的JLabel,當(dāng)Label重繪時(shí),會(huì)默認(rèn)的調(diào)用它的update方法,主要用于清除界面,然后update方法會(huì)調(diào)用paint方法,再把界面畫上去,所以我現(xiàn)在update方法中創(chuàng)建了一個(gè)Image和Graphics對(duì)象Image off_screen_buf和考試,大提示off_screen_gc同時(shí)設(shè)置其大小和MyLabel對(duì)象的大小一樣,用于把要畫的東東先繪制到后臺(tái)內(nèi)存中,然后調(diào)用paint方法把要畫的圖像畫在上面。最后再把內(nèi)存中的圖像畫在前臺(tái)上用off_screen_buf作為參數(shù)。再調(diào)用repaint方法,repaint方法回默認(rèn)的調(diào)用update方法,這樣圖像就能夠不停的顯示了。
public class MyLabel extends JLabel
{
//雙緩沖技術(shù)
private Image off_screen_buf;
private Graphics off_screen_gc;
public void paint(Graphics g)
{
if(Myjxta.image!=null)
{
this.setPreferredSize(new Dimension(Myjxta.image.getWidth(),Myjxta.image.getHeight()));
g.drawImage(Myjxta.image, 0, 0, this);
}
try
{
Thread.sleep(200);
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void update(Graphics g)
{
if (Myjxta.image != null)
{
off_screen_buf =this.createImage(this.getWidth(),this.getHeight());
off_screen_gc = off_screen_buf.getGraphics();
paint(off_screen_gc);
off_screen_gc.dispose();
g.drawImage(off_screen_buf,0,0,null);
this.repaint() ;
}
}
}
public class MyLabel extends JLabel
{
//雙緩沖技術(shù)
private Image off_screen_buf;
private Graphics off_screen_gc;
public void paint(Graphics g)
{
if(Myjxta.image!=null)
{
this.setPreferredSize(new Dimension(Myjxta.image.getWidth(),Myjxta.image.getHeight()));
g.drawImage(Myjxta.image, 0, 0, this);
}
try
{
Thread.sleep(200);
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void update(Graphics g)
{
if (Myjxta.image != null)
{
off_screen_buf =this.createImage(this.getWidth(),this.getHeight());
off_screen_gc = off_screen_buf.getGraphics();
paint(off_screen_gc);
off_screen_gc.dispose();
g.drawImage(off_screen_buf,0,0,null);
this.repaint() ;
}
}
}

