📄 resultthumbnailpanel.java
字号:
/*
* This file is part of Caliph & Emir.
*
* Caliph & Emir is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Caliph & Emir is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Caliph & Emir; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Copyright statement:
* --------------------
* (c) 2002-2005 by Mathias Lux (mathias@juggle.at)
* http://www.juggle.at, http://caliph-emir.sourceforge.net
*/
package at.lux.fotoretrieval.panels;
import at.lux.components.ViewImageDialog;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.URI;
/**
* ResultThumbnailPanel
*
* @author Mathias Lux, mathias@juggle.at
*/
public class ResultThumbnailPanel extends JPanel {
private BufferedImage image;
private Dimension size = new Dimension(120, 120);
private String fileUri;
public ResultThumbnailPanel(Image image, String filePath) {
this.fileUri = filePath;
this.setPreferredSize(size);
this.setMinimumSize(size);
this.setMaximumSize(size);
if (image.getHeight(null) == image.getWidth(null) && (image.getWidth(null) == 120)) {
this.image = new BufferedImage(120, 120, BufferedImage.TYPE_INT_RGB);
Graphics g2 = this.image.getGraphics();
g2.drawImage(image, 0, 0, null);
} else {
int x, y, max;
double factor;
x = image.getWidth(null);
y = image.getHeight(null);
if (x > y) {
max = x;
factor = 110.0 / x;
} else {
max = y;
factor = 110.0 / y;
}
x = (int) Math.floor((double) x * factor);
y = (int) Math.floor((double) y * factor);
this.image = new BufferedImage(110, 110, BufferedImage.TYPE_INT_RGB);
Graphics g2 = this.image.getGraphics();
g2.setColor(Color.black);
g2.fillRect(0, 0, 110, 110);
g2.drawImage(image, (110 - x) / 2, (110 - y) / 2, x, y, null);
}
// apply a view dialog on click :)
addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
BufferedImage imgToShow = null;
try {
if (fileUri.startsWith("file:") || fileUri.startsWith("http:")) {
// URI tmpFileUri = URI.create(fileUri);
// imgToShow = ImageIO.read(new File(tmpFileUri));
imgToShow = ImageIO.read(new File(fileUri.substring("file:/".length())));
} else {
imgToShow = ImageIO.read(new File(fileUri));
}
} catch (Exception ex) {
System.err.println("Error reading "+ fileUri);
ex.printStackTrace();
}
if (imgToShow != null) {
ViewImageDialog vid = new ViewImageDialog(imgToShow);
vid.setVisible(true);
} else {
JOptionPane.showMessageDialog(null, "Image for preview not found.");
}
}
});
}
public ResultThumbnailPanel(String fileUri) {
this.image = null;
this.fileUri = fileUri;
// size = new Dimension(120, 120);
this.setPreferredSize(size);
this.setMinimumSize(size);
this.setMaximumSize(size);
this.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
loadImage();
}
});
}
private void loadImage() {
try {
if (!(image != null)) {
BufferedImage img = ImageIO.read(new URI(fileUri).toURL());
int x, y, max;
double factor;
x = img.getWidth(null);
y = img.getHeight(null);
if (x > y) {
max = x;
factor = 110.0 / x;
} else {
max = y;
factor = 110.0 / y;
}
x = (int) Math.floor((double) x * factor);
y = (int) Math.floor((double) y * factor);
this.image = new BufferedImage(110, 110, BufferedImage.TYPE_INT_RGB);
Graphics g2 = this.image.getGraphics();
g2.setColor(Color.black);
g2.fillRect(0, 0, 110, 110);
g2.drawImage(img, (110 - x) / 2, (110 - y) / 2, x, y, null);
}
} catch (Exception e) {
e.printStackTrace();
}
repaint();
}
public void paint(Graphics g) {
// super.paint(g);
Graphics2D g2 = (Graphics2D) g;
// g2.setColor(Color.black);
// g2.clearRect(0, 0, 110, 110);
if (image != null) {
g2.drawImage(image, 5, 5, 110, 110, null);
} else {
g2.drawRect(5, 5, 110, 110);
g2.drawLine(5, 5, 115, 115);
g2.drawLine(5, 115, 115, 5);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -