001/* 002 * This file is part of McIDAS-V 003 * 004 * Copyright 2007-2016 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 029package edu.wisc.ssec.mcidasv.ui; 030 031import static java.awt.RenderingHints.KEY_ANTIALIASING; 032import static java.awt.RenderingHints.KEY_INTERPOLATION; 033import static java.awt.RenderingHints.KEY_RENDERING; 034import static java.awt.RenderingHints.VALUE_ANTIALIAS_ON; 035import static java.awt.RenderingHints.VALUE_INTERPOLATION_BICUBIC; 036import static java.awt.RenderingHints.VALUE_RENDER_QUALITY; 037import static java.awt.geom.AffineTransform.getScaleInstance; 038 039import java.awt.Color; 040import java.awt.Graphics; 041import java.awt.Graphics2D; 042import java.awt.Image; 043import java.awt.RenderingHints; 044import java.awt.image.BufferedImage; 045 046import javax.swing.JPanel; 047 048/** 049 * Extend JPanel to draw an optionally anti-aliased image filling the panel. 050 */ 051public class JPanelImage extends JPanel { 052 053 /** Image to draw within the {@code JPanel}. May be {@code null}. */ 054 private Image theImage; 055 056 /** Whether or not anti-aliasing is being used. */ 057 private boolean antiAlias; 058 059 /** 060 * Create a {@code JPanelImage} without specifying an {@link Image}. Note 061 * that this sets {@link #antiAlias} to {@code true}. 062 */ 063 public JPanelImage() { 064 antiAlias = true; 065 } 066 067 /** 068 * Create a {@code JPanelImage} that contains the specified {@link Image}. 069 * Note that this sets {@link #antiAlias} to {@code true}. 070 * 071 * @param panelImage {@code Image} to use within the created panel. 072 * {@code null} is allowed. 073 */ 074 public JPanelImage(Image panelImage) { 075 antiAlias = true; 076 theImage = panelImage; 077 } 078 079 /** 080 * Determine whether or not this {@code JPanelImage} is using 081 * anti-aliasing. 082 * 083 * @return {@code true} if anti-aliasing is enabled, {@code false} 084 * otherwise. Default value is {@code true}. 085 */ 086 public boolean getAntiAlias() { 087 return antiAlias; 088 } 089 090 /** 091 * Set whether or not this {@code JPanelImage} should use anti-aliasing. 092 * 093 * @param setAA {@code true} if anti-aliasing should be enabled, 094 * {@code false} otherwise. 095 */ 096 public void setAntiAlias(boolean setAA) { 097 antiAlias = setAA; 098 } 099 100 /** 101 * Return the {@link Image} being used within this {@code JPanelImage}. 102 * 103 * @return {@code Image} being used, or {@code null} if the {@code Image} 104 * has not been provided. 105 */ 106 public Image getImage() { 107 return theImage; 108 } 109 110 /** 111 * Set the {@link Image} to use within this {@code JPanelImage}. 112 * 113 * @param panelImage {@code Image} to use. {@code null} is permitted. 114 */ 115 public void setImage(Image panelImage) { 116 theImage = panelImage; 117 } 118 119 /** 120 * Update the panel after a change. Note that this is the same as calling 121 * {@link #paint(java.awt.Graphics)}. 122 * 123 * @param g Specified graphics window. Should not be {@code null}. 124 */ 125 @Override public void update(Graphics g) { 126 paint(g); 127 } 128 129 /** 130 * Paint the {@code JPanelImage}. 131 * 132 * @param g Specified graphics window. Should not be {@code null}. 133 */ 134 @Override public void paint(Graphics g) { 135 int width = getWidth(); 136 int height = getHeight(); 137 if (antiAlias) { 138 BufferedImage newImage = 139 new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 140 double scaleX = theImage.getWidth(null) / (double)width; 141 double scaleY = theImage.getHeight(null) / (double)height; 142 double scaleXY = 1.0 / Math.max(scaleX, scaleY); 143 Graphics2D g2d = newImage.createGraphics(); 144 g2d.setBackground(Color.black); 145 g2d.clearRect(0, 0, width, height); 146 RenderingHints hints = new RenderingHints(null); 147 hints.put(KEY_ANTIALIASING, VALUE_ANTIALIAS_ON); 148 hints.put(KEY_INTERPOLATION, VALUE_INTERPOLATION_BICUBIC); 149 hints.put(KEY_RENDERING, VALUE_RENDER_QUALITY); 150 g2d.setRenderingHints(hints); 151 g2d.drawImage(theImage, getScaleInstance(scaleXY, scaleXY), null); 152 g.drawImage(newImage, 0, 0, null); 153 } else { 154 g.drawImage(theImage, 0, 0, width, height, null); 155 } 156 } 157}