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: */package org.netbeans.modules.vmd.game.editor.tiledlayer;
041:
042: import java.awt.Color;
043: import java.awt.Graphics;
044: import java.awt.Graphics2D;
045: import java.awt.RenderingHints;
046: import java.awt.event.ComponentAdapter;
047: import java.awt.event.ComponentEvent;
048: import java.util.Set;
049: import javax.swing.JComponent;
050: import org.netbeans.modules.vmd.game.model.Tile;
051: import org.netbeans.modules.vmd.game.model.TiledLayer;
052: import org.netbeans.modules.vmd.game.model.TiledLayerListener;
053:
054: /**
055: *
056: * @author kaja
057: */
058: public class TiledLayerPreviewPanel extends JComponent implements
059: TiledLayerListener {
060:
061: private TiledLayer tiledLayer;
062: private boolean autoUpdate;
063:
064: public TiledLayerPreviewPanel(TiledLayer tl, boolean autoUpdate) {
065: this .tiledLayer = tl;
066: this .addComponentListener(new ComponentAdapter() {
067: public void componentResized(ComponentEvent e) {
068: TiledLayerPreviewPanel.this .repaint();
069: }
070: });
071: this .setAutoUpdate(autoUpdate);
072: }
073:
074: public void setAutoUpdate(boolean autoUpdate) {
075: if (autoUpdate == this .autoUpdate) {
076: return;
077: }
078: if (autoUpdate) {
079: this .tiledLayer.addTiledLayerListener(this );
080: } else {
081: this .tiledLayer.removeTiledLayerListener(this );
082: }
083: this .autoUpdate = autoUpdate;
084: }
085:
086: public boolean isAutoUpdate() {
087: return this .autoUpdate;
088: }
089:
090: public void refresh() {
091: this .repaint();
092: }
093:
094: protected void paintComponent(Graphics graphics) {
095: Graphics2D g = (Graphics2D) graphics;
096: g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
097: RenderingHints.VALUE_ANTIALIAS_ON);
098: g.setColor(Color.WHITE);
099: g.fillRect(0, 0, this .getWidth(), this .getHeight());
100:
101: double ratioW = (double) this .getWidth()
102: / (double) this .tiledLayer.getWidth();
103: double ratioH = (double) this .getHeight()
104: / (double) this .tiledLayer.getHeight();
105: double ratio = Math.min(ratioW, ratioH);
106:
107: //center
108: double x = 0;
109: double y = 0;
110: if (ratio == ratioW) {
111: double newHeight = this .tiledLayer.getHeight() * ratio;
112: y = (this .getHeight() - newHeight) / 2;
113: //System.out.println("new height: " + newHeight + ", offY: " + y);
114: } else {
115: double newWidth = this .tiledLayer.getWidth() * ratio;
116: x = (this .getWidth() - newWidth) / 2;
117: //System.out.println("new width: " + newWidth + ", offX: " + x);
118: }
119: g.translate(x, y);
120: g.scale(ratio, ratio);
121: this .paintCellContents(g);
122: }
123:
124: private void paintCellContents(Graphics2D g) {
125: for (int r = 0; r < this .tiledLayer.getRowCount(); r++) {
126: for (int c = 0; c < this .tiledLayer.getColumnCount(); c++) {
127: Tile tile = this .tiledLayer.getTileAt(r, c);
128: tile
129: .paint(g, c * tile.getWidth(), r
130: * tile.getHeight());
131: }
132: }
133: }
134:
135: public void tileChanged(TiledLayer source, int row, int col) {
136: this .tiledLayerChangedVisualy();
137: }
138:
139: public void tilesChanged(TiledLayer source, Set positions) {
140: this .tiledLayerChangedVisualy();
141: }
142:
143: public void columnsInserted(TiledLayer source, int index, int count) {
144: this .tiledLayerChangedVisualy();
145: }
146:
147: public void columnsRemoved(TiledLayer source, int index, int count) {
148: this .tiledLayerChangedVisualy();
149: }
150:
151: public void rowsInserted(TiledLayer source, int index, int count) {
152: this .tiledLayerChangedVisualy();
153: }
154:
155: public void rowsRemoved(TiledLayer source, int index, int count) {
156: this .tiledLayerChangedVisualy();
157: }
158:
159: public void tilesStructureChanged(TiledLayer source) {
160: this .tiledLayerChangedVisualy();
161: }
162:
163: private void tiledLayerChangedVisualy() {
164: this.repaint();
165: }
166:
167: }
|