zoukankan      html  css  js  c++  java
  • 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 andheight 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.

     


     

     




     



  • 相关阅读:
    OC 消息机制本质
    Lua中的闭包
    Lua中的函数
    Lua中的语句
    Lua中的表达式
    Lua中的类型与值
    Unity3D之通过C#使用Advanced CSharp Messenger
    C#中sealed关键字
    C#委托delegate、Action、Func、predicate 对比用法
    Unity3D之IOS&Android收集Log文件
  • 原文地址:https://www.cnblogs.com/youxin/p/2494792.html
Copyright © 2011-2022 走看看