舉例解析Java的圖像緩沖技術(shù)的使用
當圖像信息量較大,采用以上直接顯示的方法,可能前面一部分顯示后,顯示后面一部分時,由于后面一部分還未從文件讀出,使顯示呈斑駁現(xiàn)象。為了提高顯示效果,許多應(yīng)用程序都采用圖像緩沖技術(shù),即先把圖像完整裝入內(nèi)存,在緩沖區(qū)中繪制圖像或圖形,然后將緩沖區(qū)中繪制好的圖像或圖形一次性輸出在屏幕上。緩沖技術(shù)不僅可以解決閃爍問題,并且由于在計算機內(nèi)存中創(chuàng)建圖像,程序可以對圖像進行像素級處理,完成復雜的圖像變換后再顯示。
【例】小應(yīng)用程序程序演示圖像緩沖顯示技術(shù)。程序運行時,當鼠標在圖像區(qū)域內(nèi)按下時,圖像會出現(xiàn)邊框,托動鼠標時,圖像也隨之移動。抬起鼠標后,邊框消失。程序?qū)煞N狀態(tài)的圖像先放入兩個緩沖區(qū),當鼠標拖動時,不斷地在新的位置重繪鼠標按下樣式的圖像鼠標抬起時,重繪鼠標抬起樣式的圖像。
import java.applet.*; import java.awt.*; imprt java.awt.image. * ; import javax.swing.*; import java.event.*; public class Example7_6 extends Applet{ Image myPicture; /*init()方法中,先定義一個Image對象,并賦予createImage()方法的返回值,接著創(chuàng)建Graphics對象并賦予其圖形環(huán)境。最后,讓Graphics對象調(diào)用drawImage()方法顯示圖像。 由于這里的Graphics對象offScreenGc是非屏幕對象是,小程序窗口不會有圖像顯示*/ public void init(){ myPicture = getImage(getCodeBase(), "myPic.JPG"); Image offScreenImage = createImage(size().width, size().height); Graphics offScreenGc = offScreenImage.getGraphics(); new BufferedDemo(myPicture); } /*drawImage()方法的第四個參數(shù)是實現(xiàn)ImageObserver接口,在init()方法中,調(diào)用drawImage()方法的參數(shù)是this,所以小程序要定義imageUpdate()方法*/ public boolean imageUpdate(Image img, int infoFlg, int x, int y, int w, int h){ if (infoFlg = ALLBITS){ // 表示圖像已全部裝入內(nèi)存 repaint(); return false;// 防止線程再次調(diào)用imageUpdate()方法 } else return true; } } /*程序的執(zhí)行過程是,當小程序調(diào)用drawImage()方法時,drawImage()方法將創(chuàng)建一個調(diào)用 imageUpdate()方法的線程,在imageUpdate()方法中,測定圖像是否已在部分調(diào)入內(nèi)存。創(chuàng)建的線程不斷調(diào)用imageUpdate()方法,直到該方法返回false為止。參數(shù)infoFlg使小程序能知道圖像裝入內(nèi)存的情況。當infoFlg等于ALLBITS時,表示圖像已全部裝入內(nèi)存。當該方法發(fā)現(xiàn)圖像已全部裝入內(nèi)存后,置imageLoaded為真,并調(diào)用repaint()方法重畫小程序窗口。方法返回false防止線程再次調(diào)用imageUpdate()方法。*/ class BufferedDemo extends JFrame{ public BufferedDemo(Image img){ this.getContentPane().add(new PicPanel(img)); setTile("雙緩技術(shù)演示"); setSize(300, 300); setVisible(true); } } class PicPane extends JPanel implements MouseListener, MouseMotionListener{ int x = 0, y = 0, dx = 0, cy = 0; BufferedImage bimg1, bimg2; boolean upstate = true; public picPanel(Image img){ this.setBackground(Color.white); this.addMouseListener(this); this.addMouseMotionListener(this); bimg1 = new BufferedImage(img.getWidth(this), img.getHeight(this), BufferedImage.TYPE_INT_ARGB); bimg2 = new BufferedImage(img.getWidth(this), img.getHeight(this), BufferedImage.TYPE_INT_ARGB); Graphics2D g2D1 = bimg1.createGraphics(); Graphics2D g2D2 = bimg2.createGraphics(); g2D1.drawImage(img, 0, 0, this); g2D2.drawImage(img, 0, 0, this); g2D2.drawRect(1, 1, img.getWidth(this) - 3, img.getHeight(this) - 3); } public void paintComponent(Graphics g){ super.painComponent(g); Graphics2D g2D = (Graphics2D)g; if (upState) g2D.drawImage(bimg1, x, y, this); else g2D.drawImage(bimg2.x, y, this); } public void mousePress(MouseEvent e){ if (e.getX() >= x && e.getX() < x + bimg1.getWidth(this) && e.getY() >= y&& e.getY() < y + bimg1.getHeight(this)){ upstate = false; setCursor(Cursor.getPredefinedCursor(Coursor.HAND_CURSOR)); dx = e.getX() - x; dy = e.getY() - y; repain(); } } public void mouseExited(MouseEvent e){} public void mouseClicked(MouseEvent e){} public void mouseEntered(MouseEvent e){} public void MouseReleased(MouseEvent e){ this.setCursor(Cursor.getpredefinedCursor(Cursor.DEFAULT_CURSOR)); upState = true; repaint(); } public void mouseMoved(MouseEvent e){} public void mouseDragged(MouseEvent e){ if (!upState){ x = e.getX() - dx; y = e.getY() - dy; repaint(); } } }
程序要創(chuàng)建緩沖區(qū)圖像,需要引入java.awt.image包中的BufferedImage類。要創(chuàng)建一個緩沖區(qū)圖,可以調(diào)用createImage()方法,該方法返回一個Image對象,然后再將它轉(zhuǎn)換成一個BufferedImage對象。例如,代碼:
BufferedImage bimage = (BufferedImage)this.createImage(this.getWidth(),this.getHeight());
也可利用以下構(gòu)造方法來建立。
BufferedImage(int width,int heigh, int imageType);
其中參數(shù) imageType是圖像類型。
使用緩沖區(qū)顯示圖像,需先在緩沖區(qū)中準備好圖像,再將緩沖區(qū)中的圖像顯示在界面上。顯示圖像需要圖形對象Graphics,可以通過以下方法建立:
Graphics2D g2d = bimge.createGraphics();
相關(guān)文章
spring?boot項目自定義參數(shù)校驗規(guī)則示例詳解
這篇文章主要介紹了spring boot項目如何自定義參數(shù)校驗規(guī)則,自定義校驗規(guī)則和自帶的規(guī)則實現(xiàn)方式一樣,先自定義一個注解,然后指定校驗類,在校驗類里實現(xiàn)具體的校驗規(guī)則,本文結(jié)合示例代碼給大家介紹的非常詳細,需要的朋友可以參考下2023-07-07Java集合List和Map互轉(zhuǎn)的方法總結(jié)
有時候我們需要將給定的List轉(zhuǎn)換為Map,或者Map轉(zhuǎn)換為List,本文主要介紹了Java集合List和Map互轉(zhuǎn)的方法總結(jié),具有一定的參考價值,感興趣的可以了解一下2023-09-09淺談byte和長度為8的boolean數(shù)組互相轉(zhuǎn)換
下面小編就為大家?guī)硪黄獪\談byte和長度為8的boolean數(shù)組互相轉(zhuǎn)換。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-11-11java編程中字節(jié)流轉(zhuǎn)換成字符流的實現(xiàn)方法
下面小編就為大家?guī)硪黄猨ava編程中字節(jié)流轉(zhuǎn)換成字符流的實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-01-01