2012-12-02 28 views
1

多重像我已經做了JLabel,我顯示我的圖片是這樣的:用的setIcon

BufferedImage myimage; 
imageLabel.setIcon(new ImageIcon(myimage)); 

是否有可能繪製圖像,並在其與setIcon命令繪製一個較小的圖像(圖標)?我該怎麼做?

例如:

BufferedImage myimage1; 
BufferedImage myLittleIcon; 
imageLabel.setIcon(new ImageIcon(myimage1)); 
imageLabel.setIcon(new ImageIcon(myLittleIcon)); 

上面剛剛繪製小圖標。

+0

您已經爲'JLabel'提供的代碼,但你標記它的題爲它作爲'JFrame' - 這是什麼呢? – wchargin

回答

4

調用setIcon將覆蓋圖標。但是,您可以嘗試如下所示:

// Assumed that these are non-null 
BufferedImage bigIcon, smallIcon; 

// Create a new image. 
BufferedImage finalIcon = new BufferedImage(
    bigIcon.getWidth(), bigIcon.getHeight(), 
    BufferedImage.TYPE_INT_ARGB)); // start transparent 

// Get the graphics object. This is like the canvas you draw on. 
Graphics g = finalIcon.getGraphics(); 

// Now we draw the images. 
g.drawImage(bigIcon, 0, 0, null); // start at (0, 0) 
g.drawImage(smallIcon, 10, 10, null); // start at (10, 10) 

// Once we're done drawing on the Graphics object, we should 
// call dispose() on it to free up memory. 
g.dispose(); 

// Finally, convert to ImageIcon and apply. 
imageLabel.setIcon(new ImageIcon(finalIcon)); 

這會創建一個新圖像,繪製大圖標,然後繪製小圖標。

你也可以畫其他的東西,比如outlining a rectanglefilling an oval

對於更先進的圖形功能,儘量轉換成一Graphics2D對象。

+1

1)不要忘記'處理()'的圖形實例。 2)請鏈接到最新的公共JRE的文檔,而不是來自1.4.2的文檔。 3)Re。您在JLabel和JFrame之間的查詢我決定自代碼屬性名稱隱含標籤,以此標記它。 –

+1

當然,你對'dispose()'是對的。謝謝! – wchargin

+0

爲什麼你從0,0到10,10畫? –

相關問題