📄 imagepreview.java
字号:
/* * Copyright 2006-2007 JavaAtWork All rights reserved. * Use is subject to license terms. */package com.javaatwork.mydownloader.dialog;import java.awt.Dimension;import java.awt.Graphics;import java.awt.Image;import java.net.URL;import javax.swing.ImageIcon;import javax.swing.JComponent;import javax.swing.ListSelectionModel;import javax.swing.event.ListSelectionEvent;import javax.swing.event.ListSelectionListener;import com.javaatwork.mydownloader.DownloadFile;import com.javaatwork.mydownloader.FileTable;/** * Class for displaying a thumbnail. * * @author Johannes Postma */public class ImagePreview extends JComponent implements ListSelectionListener { /** * serialVersionUID */ private static final long serialVersionUID = -1228147931360116788L; private ImageIcon thumbnail = null; private WorkerThread thread = null; private Object lock = new Object(); private URL thumbURL = null; private FileTable table = null; private ListSelectionModel rowSM = null; /** * Creates a new ImagePreview. * * @param table The FileTable. */ public ImagePreview(FileTable table) { this.table = table; setPreferredSize(new Dimension(100, 50)); // setPreferredSize(new Dimension(50, 25)); // setMaximumSize(new Dimension(50, 25)); rowSM = table.getSelectionModel(); rowSM.addListSelectionListener(this); thread = new WorkerThread(); thread.setPriority(Thread.MIN_PRIORITY); thread.setDaemon(true); thread.start(); } /** * Will be invoked when the value of ListSelectionEvent is changed. * * @param e ListSelectionEvent */ public void valueChanged(ListSelectionEvent e) { ListSelectionModel lsm = (ListSelectionModel) e.getSource(); if (lsm.isSelectionEmpty()) { synchronized (lock) { thumbURL = null; lock.notifyAll(); } } else { int selectedRow = lsm.getMinSelectionIndex(); int selectedMaxRow = lsm.getMaxSelectionIndex(); if (selectedRow == selectedMaxRow) { int index = table.getSelectedRow(); DownloadFile file = table.getDownloadFile(index); if (file != null) { synchronized (lock) { thumbURL = file.getUrl(); lock.notifyAll(); } } else { synchronized (lock) { thumbURL = null; lock.notifyAll(); } } // multiple selection -> remove icon } else { synchronized (lock) { thumbURL = null; lock.notifyAll(); } } } } /** * Paints the component. * * @param g Graphics */ protected void paintComponent(Graphics g) { if (thumbnail != null) { int x = getWidth() / 2 - thumbnail.getIconWidth() / 2; int y = getHeight() / 2 - thumbnail.getIconHeight() / 2; if (y < 0) { y = 0; } if (x < 5) { x = 5; } thumbnail.paintIcon(this, g, x, y); } } /** * A thread for reading the images and creating the thumbnails. */ class WorkerThread extends Thread { URL url = null; public void run() { images: while (true) { try { synchronized (lock) { if (thumbURL == null) { thumbnail = null; repaint(); lock.wait(); continue images; } else { // the thumbfile must be read in an not synchronized block otherwise the normal thread will hang // therefore a copy must be made of the original thumbfile url = thumbURL; } } ImageIcon tmpIcon = new ImageIcon(url); // check if the thumbFile has changed synchronized (lock) { if (thumbURL == null || !url.equals(thumbURL)) { thumbnail = null; repaint(); continue images; } } // create thumbnail if (tmpIcon.getIconWidth() > 90) { thumbnail = new ImageIcon(tmpIcon.getImage().getScaledInstance(90, -1, Image.SCALE_AREA_AVERAGING)); } else { // no need to miniaturize thumbnail = tmpIcon; } synchronized (lock) { // check if the thumbFile has changed if (thumbURL == null || !url.equals(thumbURL)) { thumbnail = null; repaint(); } // paint the thumbnail else { repaint(); lock.wait(); } } } catch (Exception exc) { // do nothing } } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -