001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.vmd.game.dialog;
042:
043: import java.awt.Color;
044: import java.awt.Dimension;
045: import java.awt.Graphics;
046: import java.awt.Graphics2D;
047: import java.awt.Image;
048: import java.awt.event.ComponentAdapter;
049: import java.awt.event.ComponentEvent;
050: import java.awt.image.BufferedImage;
051: import java.net.MalformedURLException;
052: import java.net.URL;
053:
054: import javax.swing.BorderFactory;
055:
056: import org.netbeans.modules.vmd.game.model.ImageUtils;
057:
058: /**
059: *
060: * @author kherink
061: */
062: public class PartialImageGridPreview extends
063: AbstractImagePreviewComponent {
064:
065: public static final boolean DEBUG = false;
066:
067: private static final int TILE_GAP = 4;
068:
069: private URL imageURL;
070: private BufferedImage originalImage;
071: private Image preview;
072: private int tileWidth;
073: private int tileHeight;
074:
075: public PartialImageGridPreview() {
076: this .setBorder(BorderFactory.createLineBorder(Color.GRAY));
077: Dimension d = new Dimension(50, 20);
078: this .setMinimumSize(d);
079: this .addComponentListener(new ComponentAdapter() {
080: public void componentResized(ComponentEvent e) {
081: if (DEBUG)
082: System.out.println("resized - updating preview"); // NOI18N
083: PartialImageGridPreview.this .createPartialPreview();
084: PartialImageGridPreview.this .repaint();
085: }
086: });
087: }
088:
089: public void setImageURL(URL imageURL) throws MalformedURLException,
090: IllegalArgumentException {
091: this .imageURL = imageURL;
092: if (imageURL == null)
093: return;
094: Image image = ImageUtils.loadImage(imageURL);
095: if (image == null) {
096: throw new IllegalArgumentException();
097: }
098: BufferedImage bufImg = new BufferedImage(image.getWidth(null),
099: image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
100: Graphics2D graphics = (Graphics2D) bufImg.getGraphics();
101: graphics.drawImage(image, 0, 0, null);
102: this .originalImage = bufImg;
103: this .tileWidth = originalImage.getWidth(this );
104: this .tileHeight = originalImage.getHeight(this );
105:
106: this .createPartialPreview();
107: this .repaint();
108: }
109:
110: public URL getImageURL() {
111: return this .imageURL;
112: }
113:
114: public Image getImage() {
115: return this .originalImage;
116: }
117:
118: private void createPartialPreview() {
119: if (this .originalImage == null)
120: return;
121:
122: BufferedImage tmpImg = null;
123:
124: int rows = originalImage.getHeight(this ) / this .tileHeight;
125: int cols = originalImage.getWidth(this ) / this .tileWidth;
126:
127: if (rows == 1 && cols == 1) {
128: this .preview = ImageUtils.getScaledImage(
129: this .originalImage, this .getWidth(), this
130: .getHeight());
131: return;
132: }
133:
134: else if (rows == 1 && cols != 1) {
135: int maxWidth = (this .getWidth() - TILE_GAP) / 2;
136: int maxHeight = this .getHeight();
137: Image left = ImageUtils.getScaledImage(
138: this .originalImage.getSubimage(0, 0,
139: this .tileWidth, this .tileHeight), maxWidth,
140: maxHeight);
141: Image right = ImageUtils.getScaledImage(this .originalImage
142: .getSubimage(this .tileWidth, 0, this .tileWidth,
143: this .tileHeight), maxWidth, maxHeight);
144:
145: tmpImg = new BufferedImage(left.getWidth(this ) * 2
146: + TILE_GAP, left.getHeight(this ),
147: BufferedImage.TYPE_INT_ARGB);
148:
149: Graphics2D g = tmpImg.createGraphics();
150: g.drawImage(left, 0, 0, this );
151: g.setColor(Color.WHITE);
152: g.fillRect(left.getWidth(this ), 0, TILE_GAP, left
153: .getHeight(this )
154: * 2 + TILE_GAP);
155: g.setColor(Color.BLACK);
156: g.fillRect(left.getWidth(this ) + TILE_GAP / 4, 0,
157: TILE_GAP / 2, left.getHeight(this ) * 2 + TILE_GAP);
158: g.drawImage(right, left.getWidth(this ) + TILE_GAP, 0, this );
159:
160: } else if (rows != 1 && cols == 1) {
161: int maxWidth = this .getWidth();
162: int maxHeight = (this .getHeight() - TILE_GAP) / 2;
163: Image top = ImageUtils.getScaledImage(
164: this .originalImage.getSubimage(0, 0,
165: this .tileWidth, this .tileHeight), maxWidth,
166: maxHeight);
167: Image bottom = ImageUtils.getScaledImage(this .originalImage
168: .getSubimage(0, this .tileHeight, this .tileWidth,
169: this .tileHeight), maxWidth, maxHeight);
170:
171: tmpImg = new BufferedImage(top.getWidth(this ), top
172: .getHeight(this )
173: * 2 + TILE_GAP, BufferedImage.TYPE_INT_ARGB);
174:
175: Graphics2D g = tmpImg.createGraphics();
176: g.drawImage(top, 0, 0, this );
177: g.setColor(Color.WHITE);
178: g.fillRect(0, top.getHeight(this ), top.getWidth(this ) * 2
179: + TILE_GAP, TILE_GAP);
180: g.setColor(Color.BLACK);
181: g.fillRect(0, top.getHeight(this ) + TILE_GAP / 4, top
182: .getWidth(this )
183: * 2 + TILE_GAP, TILE_GAP / 2);
184: g
185: .drawImage(bottom, 0, top.getHeight(this )
186: + TILE_GAP, this );
187: } else {
188: int maxWidth = (this .getWidth() - TILE_GAP) / 2;
189: int maxHeight = (this .getHeight() - TILE_GAP) / 2;
190: Image topLeft = ImageUtils.getScaledImage(
191: this .originalImage.getSubimage(0, 0,
192: this .tileWidth, this .tileHeight), maxWidth,
193: maxHeight);
194: Image topRight = ImageUtils.getScaledImage(
195: this .originalImage.getSubimage(this .tileWidth, 0,
196: this .tileWidth, this .tileHeight), maxWidth,
197: maxHeight);
198: Image bottomLeft = ImageUtils.getScaledImage(
199: this .originalImage.getSubimage(0, this .tileHeight,
200: this .tileWidth, this .tileHeight), maxWidth,
201: maxHeight);
202: Image bottomRight = ImageUtils.getScaledImage(
203: this .originalImage.getSubimage(this .tileWidth,
204: this .tileHeight, this .tileWidth,
205: this .tileHeight), maxWidth, maxHeight);
206:
207: tmpImg = new BufferedImage(topLeft.getWidth(this ) * 2
208: + TILE_GAP, topLeft.getHeight(this ) * 2 + TILE_GAP,
209: BufferedImage.TYPE_INT_ARGB);
210:
211: Graphics2D g = tmpImg.createGraphics();
212: g.drawImage(topLeft, 0, 0, this );
213: g.drawImage(topRight, topLeft.getWidth(this ) + TILE_GAP, 0,
214: this );
215: g.setColor(Color.WHITE);
216: g.fillRect(topLeft.getWidth(this ), 0, TILE_GAP, topLeft
217: .getHeight(this )
218: * 2 + TILE_GAP);
219: g.fillRect(0, topLeft.getHeight(this ), topLeft
220: .getWidth(this )
221: * 2 + TILE_GAP, TILE_GAP);
222: g.setColor(Color.BLACK);
223: g.fillRect(topLeft.getWidth(this ) + TILE_GAP / 4, 0,
224: TILE_GAP / 2, topLeft.getHeight(this ) * 2
225: + TILE_GAP);
226: g
227: .fillRect(0,
228: topLeft.getHeight(this ) + TILE_GAP / 4,
229: topLeft.getWidth(this ) * 2 + TILE_GAP,
230: TILE_GAP / 2);
231: g.drawImage(bottomLeft, 0, topLeft.getHeight(this )
232: + TILE_GAP, this );
233: g.drawImage(bottomRight, topLeft.getWidth(this ) + TILE_GAP,
234: topLeft.getHeight(this ) + TILE_GAP, this );
235: }
236: this .preview = tmpImg;
237: }
238:
239: public void setTileWidth(int width) {
240: if (DEBUG)
241: System.out.println("setting tile width to: " + width); // NOI18N
242: this .tileWidth = width;
243: this .createPartialPreview();
244: this .repaint();
245: }
246:
247: public void setTileHeight(int height) {
248: if (DEBUG)
249: System.out.println("setting tile height to: " + height); // NOI18N
250: this .tileHeight = height;
251: this .createPartialPreview();
252: this .repaint();
253: }
254:
255: public void paintComponent(Graphics g) {
256: //int rounding = 20;
257: //g.setColor(Color.BLACK);
258: //g.drawRoundRect(0, 0, getWidth(), getHeight(), rounding, rounding);
259: if (this .originalImage != null) {
260: if (DEBUG)
261: System.out.println("about to draw image preview: "
262: + this .preview); // NOI18N
263: //this.createPreview();
264: int offX = (this .getWidth() - this .preview.getWidth(this )) / 2;
265: int offY = (this .getHeight() - this .preview.getHeight(this )) / 2;
266: g.drawImage(this .preview, offX, offY, this );
267: }
268: }
269:
270: public int getTileWidth() {
271: return this .tileWidth;
272: }
273:
274: public int getTileHeight() {
275: return this.tileHeight;
276: }
277:
278: }
|