博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java选择文件时提供图像缩略图[转]
阅读量:5264 次
发布时间:2019-06-14

本文共 4113 字,大约阅读时间需要 13 分钟。

项目要用,网上找的
出处: 
1 package ImageAPI;   2 import javax.swing.*;   3 import java.awt.*;   4 import java.awt.event.*;   5 import java.beans.*;   6 import java.io.File;   7   8 /**   9  * @author matthew  10  *  11  */  12 public class ImagePreviewPanel extends JPanel implements PropertyChangeListener {
13 14 private int width, height; 15 16 private ImageIcon icon; 17 18 private Image image; 19 20 private static final int ACCSIZE = 155; 21 22 private Color bg; 23 24 public ImagePreviewPanel() {
25 setPreferredSize(new Dimension(ACCSIZE, -1)); 26 bg = getBackground(); 27 } 28 29 public void propertyChange(PropertyChangeEvent e) {
30 String propertyName = e.getPropertyName(); 31 32 // Make sure we are responding to the right event. 33 if (propertyName.equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)) {
34 File selection = (File) e.getNewValue(); 35 String name; 36 37 if (selection == null) 38 return; 39 else 40 name = selection.getAbsolutePath(); 41 42 /* 43 * Make reasonably sure we have an image format that AWT can handle 44 * so we don't try to draw something silly. 45 */ 46 if ((name != null) && name.toLowerCase().endsWith(".jpg") 47 || name.toLowerCase().endsWith(".jpeg") 48 || name.toLowerCase().endsWith(".gif") 49 || name.toLowerCase().endsWith(".png")) {
50 icon = new ImageIcon(name); 51 image = icon.getImage(); 52 scaleImage(); 53 repaint(); 54 } 55 } 56 } 57 58 private void scaleImage() {
59 width = image.getWidth(this); 60 height = image.getHeight(this); 61 double ratio = 1.0; 62 63 /* 64 * Determine how to scale the image. Since the accessory can expand 65 * vertically make sure we don't go larger than 150 when scaling 66 * vertically. 67 */ 68 if (width >= height) {
69 ratio = (double) (ACCSIZE - 5) / width; 70 width = ACCSIZE - 5; 71 height = (int) (height * ratio); 72 } else {
73 if (getHeight() > 150) {
74 ratio = (double) (ACCSIZE - 5) / height; 75 height = ACCSIZE - 5; 76 width = (int) (width * ratio); 77 } else {
78 ratio = (double) getHeight() / height; 79 height = getHeight(); 80 width = (int) (width * ratio); 81 } 82 } 83 84 image = image.getScaledInstance(width, height, Image.SCALE_DEFAULT); 85 } 86 87 public void paintComponent(Graphics g) {
88 g.setColor(bg); 89 90 /* 91 * If we don't do this, we will end up with garbage from previous images 92 * if they have larger sizes than the one we are currently drawing. 93 * Also, it seems that the file list can paint outside of its rectangle, 94 * and will cause odd behavior if we don't clear or fill the rectangle 95 * for the accessory before drawing. This might be a bug in 96 * JFileChooser. 97 */ 98 g.fillRect(0, 0, ACCSIZE, getHeight()); 99 g.drawImage(image, getWidth() / 2 - width / 2 + 5, getHeight() / 2 100 - height / 2, this); 101 } 102 103 /** 104 * @param args 105 */ 106 public static void main(String[] args) {
107 JFileChooser chooser = new JFileChooser(); 108 ImagePreviewPanel preview = new ImagePreviewPanel(); 109 chooser.setAccessory(preview); 110 chooser.addPropertyChangeListener(preview); 111 chooser.showOpenDialog(null); 112 113 } 114 115 }

  

转载于:https://www.cnblogs.com/tianxuyuan/archive/2011/07/20/2111405.html

你可能感兴趣的文章
java.io.IOException: read failed, socket might closed or timeout, read ret: -1
查看>>
java 常用命令
查看>>
卷积中的参数
查看>>
51nod1076 (边双连通)
查看>>
Linux pipe函数
查看>>
java equals 小记
查看>>
2019春 软件工程实践 助教总结
查看>>
Zerver是一个C#开发的Nginx+PHP+Mysql+memcached+redis绿色集成开发环境
查看>>
java实现哈弗曼树
查看>>
程序的静态链接,动态链接和装载 (补充)
查看>>
关于本博客说明
查看>>
python常用模块之sys, os, random
查看>>
HDU 2548 A strange lift
查看>>
Linux服务器在外地,如何用eclipse连接hdfs
查看>>
react双组件传值和传参
查看>>
[Kaggle] Sentiment Analysis on Movie Reviews
查看>>
价值观
查看>>
mongodb命令----批量更改文档字段名
查看>>
MacOS copy图标shell脚本
查看>>
国外常见互联网盈利创新模式
查看>>