程式扎記: [ Java 專題介紹 ] Swing : 如何使用 ImageIcon

標籤

2010年7月29日 星期四

[ Java 專題介紹 ] Swing : 如何使用 ImageIcon


轉載自 這裡
前言 :
javax.swing.ImageIcon is used for images, both to use on buttons and labels, and to draw in a graphics panel. The supported formats are .gif, .jpg, and .png.

載入時機 :
再載入 ImageIcon 時, 你可以有兩個選擇並說明如下 :

- Wait until loaded (recommended where timing is not critical)
This simple approach loads the image "synchronously", meaning that whenever you request an image to be loaded, the program waits until the image loading is finished. If you do this, you don't have the extra complication of using ImageObserver. For a small number of small images from disk, this is the best choice. The examples here are written in this style.

- Overlap (use only for performance problems)
Start loading each image in its own thread, and proceed with other initialization. To check the status of the load (disks, and the Internet are so slow compared to the CPU), use ImageObserver. Unless you are loading many images, or a large image, over the Internet, I don't recommend the extra complication.


載入方法 :
在載入 ImageIcon 的來源可能有以下幾種, 分別說明如下 :
- To load an ImageIcon from a URL
載入 ImageIcon 從 Internet, 這時你需要一個 URL, 範例代碼如下 :
- 範例代碼 (從url 載入 ImageIcon) :
  1. java.net.URL where = new URL("http://www.yahoo.com/logo.jpeg");  
  2. ImageIcon anotherIcon = new ImageIcon(where);  

- To load an ImageIcon from a file
A file name in an ImageIcon constructor specifies the file name relative to the location of the class file. This constructor doesn't return until the ImageIcon is completely loaded. Sample code as below :
  1. ImageIcon myIcon = new ImageIcon("images/myPic.gif");  
Warning: Just putting the file name or path in the ImageIcon constructor won't work in general for applets or executable jar files. See discussion of class loader in the NetBeans section below.

- Bundling images in your .jar file using NetBeans and ClassLoader
Let's say you have a directory (cardimages) of images (cardimages/ad.gif, ...), and the program is in a package called cardplayer, and you're trying to load the image within the class Card :
1. Your Java source files will be in the src/cardplayer directory, as is normal for a NetBeans project with a cardplayer package. Add the directory containing the images (cardimages) to the src/cardplayer directory, so you have src/cardplayer/cardimages/ad.gif, etc.
2. ClassLoader. Using a file path is not possible when running a program that's in a jar file, especially if the program is being loaded as an applet or WebStart application. The way to find images that are bundled in the jar file is to ask the Java class loader, the code that loaded your program, to get the files. It knows where things are.
3. Use the following code to load the images :
  1. ClassLoader cldr = this.getClass().getClassLoader();  
  2. java.net.URL imageURL   = cldr.getResource("cardplayer/cardimages/ad.gif");  
  3. ImageIcon aceOfDiamonds = new ImageIcon(imageURL);  
4. Clean and Build Project.
5. The double-clickable jar file (located at dist/cardplayer.jar) can now be run, or the program can be executed in NetBeans.

使用介紹 :
- To use an ImageIcon in a JButton
- 範例代碼 :
  1. ImageIcon leftArrow = new ImageIcon("leftarrow.gif");  
  2. JButton left = new JButton(leftArrow);  

- Other ImageIcon methods
You can find the width and height of an image with :
int w = img.getIconWidth();
int h = img.getIconHeight();


補充說明 :
* 常用 Icon 的下載網址如下 :
Icon Finder
iconexperience $
iconfactory free and $
stockicons $$
icon-king free

Online JDK API (javax.swing.ImageIcon)
Class.getResource與ClassLoader.getResource()區別
在使用ClassLoader的getResource方法的時候,永遠是以Classpath為直接起點開始尋找資源的. 不用擔心從什麼包開始尋找的問題. 用Class.getResource不加/就是從當前包開始找,用ClassLoader.getResource不加/就是直接從Classpath的起點開始尋找.
This message was edited 8 times. Last update was at 28/07/2010 10:28:51

沒有留言:

張貼留言

網誌存檔

關於我自己

我的相片
Where there is a will, there is a way!