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.Point;
049: import java.awt.Rectangle;
050: import java.awt.image.BufferedImage;
051: import java.net.MalformedURLException;
052: import java.net.URL;
053: import org.netbeans.modules.vmd.game.model.ImageUtils;
054: import org.netbeans.modules.vmd.game.model.Position;
055:
056: /**
057: *
058: * @author kherink
059: */
060: public class FullImageGridPreview extends AbstractImagePreviewComponent {
061:
062: private static final boolean DEBUG = false;
063:
064: private URL imageURL;
065: private BufferedImage originalImage;
066:
067: private int cellHeight;
068: private int cellWidth;
069:
070: private int gridWidth = 3;
071:
072: public FullImageGridPreview() {
073: }
074:
075: public void setTileWidth(int width) {
076: if (DEBUG)
077: System.out.println("setting tile width to: " + width); // NOI18N
078: this .cellWidth = width;
079: this .repaint();
080: }
081:
082: public void setTileHeight(int height) {
083: if (DEBUG)
084: System.out.println("setting tile height to: " + height); // NOI18N
085: this .cellHeight = height;
086: this .repaint();
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 .cellWidth = originalImage.getWidth(this );
104: this .cellHeight = originalImage.getHeight(this );
105:
106: this .repaint();
107: }
108:
109: public URL getImageURL() {
110: return this .imageURL;
111: }
112:
113: public Image getImage() {
114: return this .originalImage;
115: }
116:
117: public Dimension getPreferredSize() {
118: if (imageURL == null) {
119: return super .getPreferredSize();
120: }
121: int cols = this .originalImage.getWidth() / this .cellWidth;
122: int rows = this .originalImage.getHeight() / this .cellHeight;
123: int width = this .gridWidth + (this .cellWidth + this .gridWidth)
124: * cols;
125: int height = this .gridWidth
126: + (this .cellHeight + this .gridWidth) * rows;
127: return new Dimension(width, height);
128: }
129:
130: public void paintComponent(Graphics g) {
131: //tiled layer editor component - paintCells() etc.
132: Graphics2D g2d = (Graphics2D) g;
133: if (this .originalImage == null) {
134: return;
135: }
136: this .paintGridLines(g2d);
137: this .paintCells(g2d);
138: }
139:
140: void paintCells(Graphics2D g) {
141: Rectangle rect = g.getClipBounds();
142: if (DEBUG)
143: System.out.println("Paint cell: " + rect); // NOI18N
144: Position topLeft = this .getCellAtPoint(rect.getLocation());
145: Position bottomRight = this .getCellAtCoordinates(rect
146: .getLocation().x
147: + rect.width, rect.getLocation().y + rect.height);
148: if (DEBUG)
149: System.out.println("topLeft: " + topLeft
150: + ", bottomRight: " + bottomRight); // NOI18N
151: //rows
152: for (int row = topLeft.getRow(); row <= bottomRight.getRow(); row++) {
153: //cols
154: for (int col = topLeft.getCol(); col <= bottomRight
155: .getCol(); col++) {
156: Position cell = new Position(row, col);
157: //if (DEBUG) System.out.println("Looking at: " + cell + " compared to " + this.cellHiLited);
158: this .paintCellContents(g, cell);
159: }
160: }
161: }
162:
163: private void paintCellContents(Graphics2D g, Position cell) {
164: Rectangle rect = this .getCellArea(cell);
165: int dx = rect.x;
166: int dy = rect.y;
167: int sx = cell.getCol() * cellWidth;
168: int sy = cell.getRow() * cellHeight;
169: g.drawImage(originalImage, dx, dy, dx + cellWidth, dy
170: + cellHeight, sx, sy, sx + cellWidth, sy + cellHeight,
171: null);
172: }
173:
174: private Position getCellAtPoint(Point p) {
175: return this .getCellAtCoordinates(p.x, p.y);
176: }
177:
178: private Position getCellAtCoordinates(int x, int y) {
179: int row = (y - this .gridWidth)
180: / (this .cellHeight + this .gridWidth);
181: int col = (x - this .gridWidth)
182: / (this .cellWidth + this .gridWidth);
183: if (x < 0) {
184: col--;
185: }
186: if (y < 0) {
187: row--;
188: }
189: //if (DEBUG) System.out.println("row = " + row + " col = " + col);
190: return new Position(row, col);
191: }
192:
193: private Rectangle getCellArea(Position cell) {
194: return this .getCellArea(cell.getRow(), cell.getCol());
195: }
196:
197: private Rectangle getCellArea(int row, int col) {
198: Rectangle cellArea = new Rectangle(
199: ((this .cellWidth + this .gridWidth) * col)
200: + this .gridWidth,
201: ((this .cellHeight + this .gridWidth) * row)
202: + this .gridWidth, this .cellWidth,
203: this .cellHeight);
204: return cellArea;
205: }
206:
207: private void paintGridLines(Graphics2D g) {
208: for (int horizontal = 0; horizontal < this .getPreferredSize().height; horizontal += (this .cellHeight + this .gridWidth)) {
209: g.setColor(Color.WHITE);
210: g.fillRect(0, horizontal, this .getPreferredSize().width,
211: this .gridWidth);
212: }
213: for (int vertical = 0; vertical < this .getPreferredSize().width; vertical += (cellWidth + this .gridWidth)) {
214: g.setColor(Color.WHITE);
215: g.fillRect(vertical, 0, this .gridWidth, this
216: .getPreferredSize().height);
217: }
218: for (int horizontal = 0; horizontal < this .getPreferredSize().height; horizontal += (this .cellHeight + this .gridWidth)) {
219: g.setColor(Color.BLACK);
220: g.drawLine(1, horizontal + 1,
221: this .getPreferredSize().width - 2, horizontal + 1);
222: }
223: for (int vertical = 0; vertical < this .getPreferredSize().width; vertical += (cellWidth + this .gridWidth)) {
224: g.setColor(Color.BLACK);
225: g.drawLine(vertical + 1, 1, vertical + 1, this
226: .getPreferredSize().height - 2);
227: }
228: }
229:
230: public int getTileWidth() {
231: return this .cellWidth;
232: }
233:
234: public int getTileHeight() {
235: return this.cellHeight;
236: }
237:
238: }
|