java 图标类Images - ImageIcon
来源:互联网 发布:你知我所想我知你所要 编辑:程序博客网 时间:2024/06/05 04:54
Images - 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.
Choice: Wait until loaded or overlap loading with other execution
- 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.
To load an ImageIcon from a URL
java.net.URL where = new URL("http://www.yahoo.com/logo.jpeg");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.
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.
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, WebStart applications, and executable jar files. See 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.
- 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/cardplayerdirectory, so you have src/cardplayer/cardimages/ad.gif, etc. - 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.
- Use the following code to load the images:
ClassLoader cldr = this.getClass().getClassLoader();java.net.URL imageURL = cldr.getResource("cardplayer/cardimages/ad.gif");ImageIcon aceOfDiamonds = new ImageIcon(imageURL);
- Clean and Build Project.
- 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
ImageIcon leftArrow = new ImageIcon("leftarrow.gif");JButton left = new JButton(leftArrow);
To draw (paint) an ImageIcon
An ImageIcon, img, can be drawn on components (a JComponent or JPanel) using
img.paintIcon(Component c, Graphics g, int x, int y);
Display the image on a subclass of JPanel used for graphics. Put the paintIcon
call in the paintComponent
method of that panel. To paint the ImageIcon img
on the current panel (ie, this
), use a call like:
public void paintComponent(Graphics g) { super.paintComponent(g); img.paintIcon(this, g, 100, 100);}
Other ImageIcon methods
You can find the width and height of an image with
int w = img.getIconWidth();int h = img.getIconHeight();
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来生成合适的IconImageIcon smallIcon = new ImageIcon(smallImage);// 最后设置它为按钮的图片JButton iconButton = new JButton(smallIcon);
- java 图标类Images - ImageIcon
- ImageIcon类 图标 java
- java ------ImageIcon类的使用
- 【Java基础 008】 ImageIcon 图标相对路径解析
- java ImageIcon路径问题
- Java - Image VS ImageIcon
- Java中Image类与ImageIcon类的区别
- JAVA图形类问题:ImageIcon和Image类区别
- Java中swing使用ImageIcon类添加图片
- ImageIcon图标文件相对路径研究总结
- ImageIcon图标文件相对路径研究总结
- Java中ImageIcon的使用
- Java ImageIcon 不能显示问题
- java中ImageIcon路径问题
- MyEclipse的JAVA项目中ImageIcon不能用
- JAVA 关于Icon,Image,ImageIcon的简单的对比参考
- JAVA中如何解决 new ImageIcon()的路径问题
- Java关于Image和ImageIcon的小阴谋
- Algorithm学习笔记 --- 三角形求周长
- Transport (VMDB) error 44 Message
- 编译MT7620内核出现问题
- LeetCode之Binary Tree Inorder Traversal
- 第四周(项目四)——数组做数据成员。
- java 图标类Images - ImageIcon
- 关于RSA算法的例子实践(第二周作业)
- 双击.dsw无反应,图标异常,打开方式中找不到vc
- POJ 1308 与 HDU 1325
- CakePHP是什么?为什么要使用它呢?
- codeforces 221D Little Elephant and Array
- Velocity布局(layout)功能介绍
- Zdenek Kalal跟踪算法
- EXCEL与SQL数据库间导入导出之傻瓜闲扯淡