www.pudn.com > Caliph_v0.9.13+Emirv0.8.5-src.zip > ImageThumbPanel.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 
 */ 
package at.lux.components; 
 
/* 
 * Created by Mathias Lux, mathias@juggle.at 
 * User: Mathias Lux, mathias@juggle.at 
 * Date: 09.09.2002 
 * Time: 20:51:41 
 */ 
 
import at.lux.fotoannotation.AnnotationFrame; 
 
import javax.swing.*; 
import java.awt.*; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import java.awt.image.BufferedImage; 
import java.text.DecimalFormat; 
 
/** 
 * Description 
 * @author Mathias Lux, mathias@juggle.at 
 * 
 */ 
 
public class ImageThumbPanel extends JPanel { 
    private static int EMPTY_BORDER = 10; 
    private Image img = null; 
    private BufferedImage orig; 
    private int imgWidth, imgHeight; 
    private DecimalFormat df; 
    private double factor; 
    private boolean rescale; 
    private BufferedImage toPaint; 
    private int x; 
    private int y; 
    private boolean wait = false; 
    private int bitsPerPixel; 
 
    public ImageThumbPanel() { 
        super(); 
        addMouseListener(new MouseAdapter() { 
            public void mouseClicked(MouseEvent event) { 
                if (event.getButton() == MouseEvent.BUTTON1) { 
                    showImage(); 
                } else if (event.getButton() == MouseEvent.BUTTON3) { 
                    rescale = true; 
                    repaint(); 
                } 
            } 
        }); 
        df = (DecimalFormat) DecimalFormat.getInstance(); 
        df.setMaximumFractionDigits(1); 
        rescale = true; 
        bitsPerPixel = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDisplayMode().getBitDepth(); 
        this.setMinimumSize(new Dimension(120, 90)); 
    } 
 
 
    public void setImage(BufferedImage img) { 
        orig = img; 
        int bits = img.getColorModel().getPixelSize(); 
        this.img = img; 
        if (bits != bitsPerPixel) { 
            debug("setting colormode from " + bits + " to " + bitsPerPixel); 
            BufferedImage tmp = this.getGraphicsConfiguration().createCompatibleImage(img.getWidth(), img.getHeight()); 
            Graphics gTemp = tmp.getGraphics(); 
            debug("drawing new image"); 
            gTemp.drawImage(img, 0, 0, null); 
            img = tmp; 
            debug("finished new image"); 
        } 
        imgWidth = this.img.getWidth(this); 
        imgHeight = this.img.getHeight(this); 
        rescale = true; 
        repaint(); 
    } 
 
    public void paint(Graphics graphics) { 
        // super.paint(graphics); 
        Graphics2D g2 = (Graphics2D) graphics; 
        g2.clearRect(0, 0, this.getWidth(), this.getHeight()); 
 
        if (img != null & !wait) { 
            long mtime = System.currentTimeMillis(); 
            if (rescale) { 
                debug("rescaling image to drawable size... "); 
                factor = Math.max((double) imgWidth / ((double) (this.getWidth() - EMPTY_BORDER)), 
                        (double) imgHeight / ((double) (this.getHeight() - EMPTY_BORDER))); 
                x = (int) (imgWidth / factor); 
                y = (int) (imgHeight / factor); 
                toPaint = this.getGraphicsConfiguration().createCompatibleImage(x, y); 
                toPaint.getGraphics().drawImage(img, 0, 0, x, y, null); 
                rescale = false; 
                debug("finished rescaling"); 
            } 
            debug("painting image to panel ... "); 
            g2.drawImage(toPaint, (getWidth() - x) / 2, (getHeight() - y) / 2, null); 
            debug("finished painting"); 
            mtime = System.currentTimeMillis() - mtime; 
            debug("Painting & scaling lasted " + mtime + " ms"); 
        } else { 
            g2.drawRect(10, 10, this.getWidth() - 20, this.getHeight() - 20); 
            g2.drawLine(10, 10, this.getWidth() - 10, this.getHeight() - 10); 
            g2.drawLine(10, this.getHeight() - 10, this.getWidth() - 10, 10); 
        } 
 
        debug("Ressources: " + df.format(Runtime.getRuntime().freeMemory() / (1024.0 * 1024.0)) + "M of " 
                + df.format(Runtime.getRuntime().totalMemory() / (1024.0 * 1024.0)) + "M"); 
    } 
 
    private void showImage() { 
        if (img != null) { 
            ViewImageDialog d = new ViewImageDialog(orig); 
            d.setVisible(true); 
            // JOptionPane.showMessageDialog(this, "Image"); 
        } 
    } 
 
    public void redrawImage() { 
        rescale = true; 
        repaint(); 
    } 
 
    public void setWait(boolean wait) { 
        this.wait = wait; 
    } 
 
    private void debug(String message) { 
        if (AnnotationFrame.DEBUG) System.out.println("[at.lux.fotoannotation.ImageThumbPanel] " + message); 
    } 
}