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.scene;
041:
042: import java.awt.Color;
043: import java.awt.Graphics;
044: import java.awt.Graphics2D;
045: import java.awt.Point;
046: import java.awt.Rectangle;
047: import java.awt.RenderingHints;
048: import java.awt.event.ComponentAdapter;
049: import java.awt.event.ComponentEvent;
050: import javax.swing.JComponent;
051: import org.netbeans.modules.vmd.game.model.Layer;
052: import org.netbeans.modules.vmd.game.model.Scene;
053: import org.netbeans.modules.vmd.game.model.Scene.LayerInfo;
054: import org.netbeans.modules.vmd.game.model.SceneListener;
055:
056: /**
057: *
058: * @author kaja
059: */
060: public class ScenePreviewPanel extends JComponent implements
061: SceneListener {
062:
063: private Scene scene;
064: private ScenePanel scenePanel;
065:
066: public ScenePreviewPanel(Scene scene) {
067: this .scene = scene;
068: this .scenePanel = new ScenePanel(scene);
069: this .addComponentListener(new ComponentAdapter() {
070: public void componentResized(ComponentEvent e) {
071: //System.out.println("ScenePreviewPanel resized - updating preview");
072: ScenePreviewPanel.this .repaint();
073: }
074: });
075: this .scene.addSceneListener(this );
076: }
077:
078: @Override
079: protected void paintComponent(Graphics graphics) {
080: Graphics2D g = (Graphics2D) graphics;
081: g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
082: RenderingHints.VALUE_ANTIALIAS_ON);
083: g.setColor(Color.WHITE);
084: g.fillRect(0, 0, this .getWidth(), this .getHeight());
085:
086: Rectangle bounds = scene.getAllLayersBounds();
087: double ratioW = (double) this .getWidth() / bounds.getWidth();
088: double ratioH = (double) this .getHeight() / bounds.getHeight();
089: double ratio = Math.min(ratioW, ratioH);
090:
091: //center
092: double x = 0;
093: double y = 0;
094: if (ratio == ratioW) {
095: double newHeight = bounds.getHeight() * ratio;
096: y = (this .getHeight() - newHeight) / 2;
097: } else {
098: double newWidth = bounds.getWidth() * ratio;
099: x = (this .getWidth() - newWidth) / 2;
100: }
101: g.translate(x, y);
102: g.scale(ratio, ratio);
103: scenePanel.drawLayers(g);
104: }
105:
106: public void layerAdded(Scene sourceScene, Layer layer, int index) {
107: this .sceneChangedVisualy();
108: }
109:
110: public void layerRemoved(Scene sourceScene, Layer layer,
111: LayerInfo info, int index) {
112: this .sceneChangedVisualy();
113: }
114:
115: public void layerMoved(Scene sourceScene, Layer layer,
116: int indexOld, int indexNew) {
117: this .sceneChangedVisualy();
118: }
119:
120: public void layerPositionChanged(Scene sourceScene, Layer layer,
121: Point oldPosition, Point newPosition, boolean inTransition) {
122: if (inTransition) {
123: return;
124: }
125: this .sceneChangedVisualy();
126: }
127:
128: public void layerLockChanged(Scene sourceScene, Layer layer,
129: boolean locked) {
130: this .sceneChangedVisualy();
131: }
132:
133: public void layerVisibilityChanged(Scene sourceScene, Layer layer,
134: boolean visible) {
135: this .sceneChangedVisualy();
136: }
137:
138: private void sceneChangedVisualy() {
139: this.repaint();
140: }
141:
142: }
|