001    /*
002     * $Id: JPanelImage.java,v 1.6 2012/02/19 17:35:51 davep Exp $
003     *
004     * This file is part of McIDAS-V
005     *
006     * Copyright 2007-2012
007     * Space Science and Engineering Center (SSEC)
008     * University of Wisconsin - Madison
009     * 1225 W. Dayton Street, Madison, WI 53706, USA
010     * https://www.ssec.wisc.edu/mcidas
011     * 
012     * All Rights Reserved
013     * 
014     * McIDAS-V is built on Unidata's IDV and SSEC's VisAD libraries, and
015     * some McIDAS-V source code is based on IDV and VisAD source code.  
016     * 
017     * McIDAS-V is free software; you can redistribute it and/or modify
018     * it under the terms of the GNU Lesser Public License as published by
019     * the Free Software Foundation; either version 3 of the License, or
020     * (at your option) any later version.
021     * 
022     * McIDAS-V is distributed in the hope that it will be useful,
023     * but WITHOUT ANY WARRANTY; without even the implied warranty of
024     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
025     * GNU Lesser Public License for more details.
026     * 
027     * You should have received a copy of the GNU Lesser Public License
028     * along with this program.  If not, see http://www.gnu.org/licenses.
029     */
030    
031    package edu.wisc.ssec.mcidasv.ui;
032    
033    import java.awt.Color;
034    import java.awt.Graphics;
035    import java.awt.Graphics2D;
036    import java.awt.Image;
037    import java.awt.RenderingHints;
038    import java.awt.geom.AffineTransform;
039    import java.awt.image.BufferedImage;
040    
041    import javax.swing.JPanel;
042    
043    /**
044     * Extend JPanel to draw an optionally anti-aliased image filling the panel
045     */
046    public class JPanelImage extends JPanel {
047            
048            // The image to draw in the panel
049            private Image theImage;
050            
051            // Use anti-aliasing
052            private boolean aa = true;
053            
054            public JPanelImage() {}
055            
056            public JPanelImage(Image panelImage) {
057                    theImage = panelImage;
058            }
059            
060            public boolean getAntiAlias() {
061                    return aa;
062            }
063            
064            public void setAntiAlias(boolean setAA) {
065                    aa = setAA;
066            }
067            
068            public Image getImage() {
069                    return theImage;
070            }
071            
072            public void setImage(Image panelImage) {
073                    theImage = panelImage;
074            }
075            
076            @Override
077            public void update(Graphics g) {
078                    paint(g);
079            }
080            
081            @Override
082            public void paint(Graphics g) {
083                    if (aa) {
084                            BufferedImage newImage = new BufferedImage(
085                                            this.getWidth(), this.getHeight(), BufferedImage.TYPE_INT_RGB);
086                            double scaleX = (double)theImage.getWidth(null) / (double)this.getWidth();
087                            double scaleY = (double)theImage.getHeight(null) / (double)this.getHeight();
088                            double scaleXY = 1.0 / (Math.max(scaleX, scaleY));
089                            Graphics2D g2d = newImage.createGraphics();
090                            g2d.setBackground(Color.black);
091                            g2d.clearRect(0, 0, this.getWidth(), this.getHeight());
092    
093                            RenderingHints hints = new RenderingHints(null);
094                            hints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
095                            hints.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
096                            hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
097                            g2d.setRenderingHints(hints);
098    
099                            g2d.drawImage(theImage, AffineTransform.getScaleInstance(scaleXY, scaleXY), null);
100                            g.drawImage(newImage, 0, 0, null);
101                    }
102                    else {
103                            g.drawImage(theImage, 0, 0, this.getWidth(), this.getHeight(), null);
104                    }
105            }
106    }