View Javadoc

1   /*
2    *   Magic-Project is a turn based strategy simulator
3    *   Copyright (C) 2003-2007 Fabrice Daugan
4    *
5    *   This program is free software; you can redistribute it and/or modify it 
6    * under the terms of the GNU General Public License as published by the Free 
7    * Software Foundation; either version 2 of the License, or (at your option) any
8    * later version.
9    *
10   *   This program is distributed in the hope that it will be useful, but WITHOUT 
11   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
13   * details.
14   *
15   *   You should have received a copy of the GNU General Public License along  
16   * with this program; if not, write to the Free Software Foundation, Inc., 
17   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   * 
19   */
20  package net.sf.magicproject.ui;
21  
22  import java.awt.Color;
23  import java.awt.Graphics2D;
24  import java.awt.RenderingHints;
25  import java.awt.event.ActionEvent;
26  import java.awt.event.KeyEvent;
27  import java.awt.image.BufferedImage;
28  
29  import javax.swing.AbstractAction;
30  import javax.swing.InputMap;
31  import javax.swing.JButton;
32  import javax.swing.JComponent;
33  import javax.swing.JDialog;
34  import javax.swing.KeyStroke;
35  
36  /***
37   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
38   * @since 0.80
39   */
40  public final class ToolKit {
41  
42  	/***
43  	 * Create a new instance of this class.
44  	 */
45  	private ToolKit() {
46  		super();
47  	}
48  
49  	/***
50  	 * Force the escape key to call the same action as pressing the Cancel button.
51  	 * This does not always work. See class comment.
52  	 * 
53  	 * @param dialog
54  	 * @param cancelButton
55  	 */
56  	public static void addCancelByEscapeKey(JDialog dialog,
57  			final JButton cancelButton) {
58  		KeyStroke escapeKey = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
59  		InputMap inputMap = dialog.getRootPane().getInputMap(
60  				JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
61  		inputMap.put(escapeKey, CANCEL_ACTION_KEY);
62  
63  		AbstractAction cancelAction = new AbstractAction() {
64  
65  			public void actionPerformed(ActionEvent e) {
66  				cancelButton.doClick();
67  			}
68  		};
69  		dialog.getRootPane().getActionMap().put(CANCEL_ACTION_KEY, cancelAction);
70  	}
71  
72  	/***
73  	 * Convenience method that returns a scaled instance of the provided
74  	 * {@code BufferedImage}.
75  	 * 
76  	 * @param img
77  	 *          the original image to be scaled
78  	 * @param targetWidth
79  	 *          the desired width of the scaled instance, in pixels
80  	 * @param targetHeight
81  	 *          the desired height of the scaled instance, in pixels
82  	 * @param borderWidth
83  	 *          the border width.
84  	 * @param borderColor
85  	 *          the border color.
86  	 * @return a scaled version of the original {@code BufferedImage}
87  	 */
88  	public static BufferedImage getScaledInstance(BufferedImage img,
89  			int targetWidth, int targetHeight, int borderWidth, Color borderColor) {
90  		final int type = BufferedImage.TYPE_INT_ARGB;
91  		BufferedImage ret = img;
92  		int w, h;
93  		/*
94  		 * Use multi-step technique: start with original size, then scale down in
95  		 * multiple passes with drawImage() until the target size is reached
96  		 */
97  		w = img.getWidth();
98  		h = img.getHeight();
99  		BufferedImage tmpBorder = new BufferedImage(w + borderWidth * 2, h
100 				+ borderWidth * 2, type);
101 		Graphics2D g2Border = tmpBorder.createGraphics();
102 		g2Border.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
103 				RenderingHints.VALUE_INTERPOLATION_BICUBIC);
104 		g2Border.setRenderingHint(RenderingHints.KEY_RENDERING,
105 				RenderingHints.VALUE_RENDER_QUALITY);
106 		g2Border.setColor(borderColor);
107 		g2Border.fillRoundRect(0, 0, w + borderWidth * 2, h + borderWidth * 2,
108 				borderWidth * 2, borderWidth * 2);
109 		g2Border.drawImage(ret, borderWidth, borderWidth, w, h, null);
110 		ret = tmpBorder;
111 
112 		do {
113 			if (w > targetWidth) {
114 				w /= 2;
115 				if (w < targetWidth) {
116 					w = targetWidth;
117 				}
118 			}
119 
120 			if (h > targetHeight) {
121 				h /= 2;
122 				if (h < targetHeight) {
123 					h = targetHeight;
124 				}
125 			}
126 
127 			BufferedImage tmp = new BufferedImage(w, h, type);
128 			Graphics2D g2 = tmp.createGraphics();
129 			g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
130 					RenderingHints.VALUE_INTERPOLATION_BICUBIC);
131 			g2.drawImage(ret, 0, 0, w, h, null);
132 			g2.dispose();
133 
134 			ret = tmp;
135 		} while (w != targetWidth || h != targetHeight);
136 
137 		return ret;
138 	}
139 
140 	private static final String CANCEL_ACTION_KEY = "CANCEL_ACTION_KEY";
141 
142 }