📄 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-2004 by Mathias Lux (mathias@juggle.at) * http://www.juggle.at *//* * Created by Mathias Lux, mathias@juggle.at * User: Mathias Lux, mathias@juggle.at * Date: 09.09.2002 * Time: 20:51:41 */package at.lux.fotoretrieval.panels;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.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) { 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); } } 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 + -