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-2007 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 apichanges;
042:
043: import framework.VisualTestCase;
044: import org.netbeans.api.visual.border.BorderFactory;
045: import org.netbeans.api.visual.widget.LabelWidget;
046: import org.netbeans.api.visual.widget.Scene;
047: import org.netbeans.api.visual.widget.LayerWidget;
048:
049: import java.awt.*;
050: import java.awt.image.BufferedImage;
051:
052: /**
053: * @author David Kaspar
054: */
055: public class OffscreenRenderingTest extends VisualTestCase {
056:
057: public OffscreenRenderingTest(String testName) {
058: super (testName);
059: }
060:
061: public void testOffscreenRendering() {
062: Scene scene = new Scene();
063:
064: LayerWidget layer = new LayerWidget(scene);
065: layer.setPreferredBounds(new Rectangle(0, 0, 80, 80));
066: scene.addChild(layer);
067:
068: LabelWidget widget = new LabelWidget(scene, "Hi");
069: widget
070: .setVerticalAlignment(LabelWidget.VerticalAlignment.CENTER);
071: widget.setAlignment(LabelWidget.Alignment.CENTER);
072: widget.setBorder(BorderFactory.createLineBorder());
073: widget.setPreferredLocation(new Point(20, 20));
074: widget.setPreferredBounds(new Rectangle(0, 0, 40, 40));
075: layer.addChild(widget);
076:
077: BufferedImage image = dumpSceneOffscreenRendering(scene);
078: assertCleaness(testCleaness(image, Color.WHITE, Color.BLACK),
079: image, null);
080:
081: assertScene(scene, Color.WHITE, new Rectangle(19, 19, 42, 42));
082: }
083:
084: private BufferedImage dumpSceneOffscreenRendering(Scene scene) {
085: // validate the scene with a off-screen graphics
086: BufferedImage emptyImage = new BufferedImage(1, 1,
087: BufferedImage.TYPE_4BYTE_ABGR);
088: Graphics2D emptyGraphics = emptyImage.createGraphics();
089: scene.validate(emptyGraphics);
090: emptyGraphics.dispose();
091:
092: // now the scene is calculated using the emptyGraphics, all widgets should be layout and scene has its size resolved
093: // paint the scene with a off-screen graphics
094: Rectangle viewBounds = scene.convertSceneToView(scene
095: .getBounds());
096: BufferedImage image = new BufferedImage(viewBounds.width,
097: viewBounds.height, BufferedImage.TYPE_4BYTE_ABGR);
098: Graphics2D graphics = image.createGraphics();
099: double zoomFactor = scene.getZoomFactor();
100: graphics.scale(zoomFactor, zoomFactor);
101: scene.paint(graphics);
102: graphics.dispose();
103:
104: return image;
105: }
106:
107: }
|