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