java中Icon ,imageIcon ,image 區別


  public interface Icon

A small fixed size picture, typically used to decorate components.

 int getIconHeight() 
          Returns the icon's height.
 int getIconWidth() 
          Returns the icon's width.
 void paintIcon(Component c, Graphics g, int x, int y) 
          Draw the icon at the specified location.


javax.swing .ImageIcon

   An implementation of the Icon interface that paints Icons from Images. Images that are created from a URL, filename or byte array are preloaded using MediaTracker to monitor the loaded state of the image.

 java.awt.Image
public abstract class Image
The abstract class Image is the superclass of all classes that represent graphical images. The image must be obtained in a platform-specific manner.

 

ImageIcon(Image image) 
          Creates an ImageIcon from an image object.
ImageIcon(Image image, String description) 
          Creates an ImageIcon from the image.
jframe.pack();依據你放置的組件設定窗口的大小 使之正好能容納你放置的所有組件

JLabel label=new JLabel(Icon image) ;

                      JLabel(Icon image, int horizontalAlignment) 

                      JLabel(String text, Icon icon, int horizontalAlignment) 

alignment - One of the following constants defined in SwingConstants: LEFT, CENTER (the default for image-only labels), RIGHT, LEADING (the default for text-only labels) or TRAILING.

Interface SwingConstants 接口

LEFT RIGHT BOTTOM TOP
等等
所有JLabel.LEFT 等同於SwingConstants.LEFT;
 
常見的操作 JLabel 設置圖片
import java.awt.Container;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class Icon extends JFrame {
    private ImageIcon icon;
    private JLabel label1;
    private JPanel panel;
    public Icon()
    {
        Container container=getContentPane();
        setSize(1000,1000); //注意setSize()不能放在后面,是有先后執行順序的
        
        icon=createImageIcon("image/yanzi.png","燕姿");
        //label1=new JLabel( "icon display",icon,JLabel.CENTER);
        label1=new JLabel(icon);
        label1.setBounds(0, 0,300,500);
        this.add(label1);
        setVisible(true);
        setLayout(null);
        
        setTitle("icon");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
    }
    protected  ImageIcon createImageIcon(String path, String description) {
        // TODO Auto-generated method stub
        URL imgURL=getClass().getResource(path);
        if(imgURL!=null)
            return new ImageIcon(imgURL,description);
        else
            System.out.println("couldn't find file "+path);
        return null;
    }
    public static void main(String[] args)
    {
        Icon icon=new Icon();
    }
    

}
 

下面這個程序我們經常寫:

/** Returns an ImageIcon, or null if the path was invalid. */
protected ImageIcon createImageIcon(String path,
                                           String description) {
    java.net.URL imgURL = getClass().getResource(path);
    if (imgURL != null) {
        return new ImageIcon(imgURL, description);
    } else {
        System.err.println("Couldn't find file: " + path);
        return null;
    }
}

You should use the Class getResource method to obtain the path to the image

object 類方法 getClass() 
          返回此 Object 的運行時類

When you specify a filename or URL to an ImageIcon constructor, processing is blocked until after the image data is completely loaded or the data location has proven to be invalid. If the data location is invalid (but non-null), an ImageIcon is still successfully created; it just has no size and, therefore, paints nothing. As shown in the createImageIcon method, it is advisable to first verify that the URL points to an existing file before passing it to the ImageIcon constructor. This allows graceful error handling when the file isn't present. If you want more information while the image is loading, you can register an observer on an image icon by calling its setImageObserver method.

Under the covers, 在幕后 each image icon uses an Image object to hold the image data.

 

Image類一般尺寸過大,不適合作ImageIcon類 把大圖片轉化為小圖片
ImageIcon imageIcon = new ImageIcon("duke.gif");    // Icon由圖片文件形成
Image image = imageIcon.getImage();                         // 但這個圖片太大不適合做Icon
//    為把它縮小點,先要取出這個Icon的image ,然后縮放到合適的大小
Image smallImage = image.getScaledInstance(30,20,Image.SCALE_FAST);
//    再由修改后的Image來生成合適的Icon
ImageIcon smallIcon = new ImageIcon(smallImage);
//   最后設置它為按鈕的圖片
JButton iconButton = new JButton(smallIcon);

 

ImageIcon類到Image類,可以通過:

ImageIcon imageIcon = new ImageIcon("duke.gif");    // Icon由圖片文件形成

JFrame jf=null;

jf.setImageIcon(ImageIcon.getImage());  ******錯誤

jframe 方法是 setIconImage( image 類,不能是imageIcon);

用法 setIconImage(new ImageIcon(" ").getImage());

 

ImageIcon getImage() 
          Returns this icon's Image.

public Image getScaledInstance(int width,
                               int height,
                               int hints)
Creates a scaled version of this image. A new  Image object is returned which will render the image at the specified  width and height by default. The new Image object may be loaded asynchronously even if the original source image has already been loaded completely.
Parameters:
width - the width to which to scale the image.
height - the height to which to scale the image.
hints - flags to indicate the type of algorithm to use for image resampling.
Returns:
a scaled version of the image.

 


 

 




 




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM