2013-02-23 44 views
0

我有一個圖像的幀和緩衝區值。我無法在框架中顯示圖像。我使用的代碼如下:在awt中顯示圖像

byte [] payload = new byte[payload_length]; 
rtp_packet.getpayload(payload); 
Toolkit toolkit = Toolkit.getDefaultToolkit(); 
Image image = toolkit.createImage(payload, 0, payload_length); 
icon = new ImageIcon(image); 
iconLabel.setIcon(icon); 

我還試圖使用的代碼直接添加它到框架:

f.setIconImage(image); 

現在如何顯示圖像?爲什麼它不工作?

+0

這是使用Swing或AWT組件。 'ImageIcon'是Swing,但你已經添加了AWT標籤。 – 2013-02-23 05:15:20

+0

我試過在鞦韆也...但我想它nwt ...我也嘗試添加一個畫布和顯示噸... – user123456789 2013-02-23 05:26:06

+0

*「但我想它nwt」*爲什麼? Swing是一個完全啓用的GUI工具包。 – 2013-02-23 05:27:31

回答

-1

您想使用圖形對象。您的AWT框架應該已經有一個和你打電話通過您的圖形對象...

  BufferedImage img = javaImage; // You replace this with your image 
      Graphics g = this.getGraphics(); // this is what you need to call 
      g.drawImage(img, 0, 0, null); // you then call draw image 

你的情況,你根本

  g.drawImage(image, 0, 0, null); // you can look up the parameters 

這應該爲你做它。

+2

-1可怕的答案。 1)不要調用'getGraphics()'。這是一個暫時的對象,可能會立即被覆蓋。請參閱教程中的[執行自定義繪畫](http://docs.oracle.com/javase/tutorial/uiswing/painting/)以獲取正確的方法。 2)'Frame'實現'ImageObserver',所以'g.drawImage(image,0,0,null);'應該是'g.drawImage(image,0,0,this);'。 – 2013-02-23 05:31:05

+0

你能清楚嗎?我應該使用g.drawImage(iamge,0,0,this);或不? – user123456789 2013-02-23 05:37:15

+0

圖像仍然沒有消失...這是我使用的代碼... Image image = toolkit.createImage(payload,0,payload_length); \t BufferedImage img =(BufferedImage)image; 圖形g = img.getGraphics(); g.drawImage(img,0,0,mainPanel); – user123456789 2013-02-23 08:47:48