2011-08-26 63 views
3

這裏是我的代碼:如何在JLabel中調整Image/IconImage的大小?

String s = "/Applications/Asphalt6.app"; 
JFileChooser chooser = new JFileChooser(); 

File file = new File(s); 
Icon icon = chooser.getIcon(file); 

// show the icon 
JLabel ficon = new JLabel(s, icon, SwingConstants.LEFT); 

現在,從圖標中提取的圖像是非常小的。我怎樣才能調整它?

+1

例如參見[此](http://www.javalobby.org/articles/ultimate-image/#11)。但是一旦它變得更大,你的圖標可能會相當醜陋。 – toto2

回答

7

Big Icon

import java.awt.*; 
import java.awt.image.*; 
import javax.swing.*; 
import java.io.*; 

class BigIcon { 

    public static void main(String[] args) { 
     JFileChooser chooser = new JFileChooser(); 
     File f = new File("BigIcon.java"); 
     Icon icon = chooser.getIcon(f); 

     int scale = 4; 

     BufferedImage bi = new BufferedImage(
      scale*icon.getIconWidth(), 
      scale*icon.getIconHeight(), 
      BufferedImage.TYPE_INT_ARGB); 
     Graphics2D g = bi.createGraphics(); 
     g.scale(scale,scale); 
     icon.paintIcon(null,g,0,0); 
     g.dispose(); 

     JOptionPane.showMessageDialog(
      null, 
      new JLabel(new ImageIcon(bi))); 
    } 
} 
+1

另請參閱此相關的[示例](http://stackoverflow.com/questions/2900801/wanting-a-type-of-grid-for-a-pixel-editor/2901472#2901472)。 – trashgod

+0

非常感謝 –

相關問題