Added
Link Here
|
1 |
/* |
2 |
* The contents of this file are subject to the terms of the Common Development |
3 |
* and Distribution License (the License). You may not use this file except in |
4 |
* compliance with the License. |
5 |
* |
6 |
* You can obtain a copy of the License at http://www.netbeans.org/cddl.html |
7 |
* or http://www.netbeans.org/cddl.txt. |
8 |
* |
9 |
* When distributing Covered Code, include this CDDL Header Notice in each file |
10 |
* and include the License file at http://www.netbeans.org/cddl.txt. |
11 |
* If applicable, add the following below the CDDL Header, with the fields |
12 |
* enclosed by brackets [] replaced by your own identifying information: |
13 |
* "Portions Copyrighted [year] [name of copyright owner]" |
14 |
* |
15 |
* The Original Software is NetBeans. The Initial Developer of the Original |
16 |
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun |
17 |
* Microsystems, Inc. All Rights Reserved. |
18 |
*/ |
19 |
|
20 |
package apichanges; |
21 |
|
22 |
import framework.VisualTestCase; |
23 |
import org.netbeans.api.visual.border.BorderFactory; |
24 |
import org.netbeans.api.visual.widget.LabelWidget; |
25 |
import org.netbeans.api.visual.widget.Scene; |
26 |
import org.netbeans.api.visual.widget.LayerWidget; |
27 |
|
28 |
import java.awt.*; |
29 |
import java.awt.image.BufferedImage; |
30 |
|
31 |
/** |
32 |
* @author David Kaspar |
33 |
*/ |
34 |
public class OffscreenRenderingTest extends VisualTestCase { |
35 |
|
36 |
public OffscreenRenderingTest (String testName) { |
37 |
super (testName); |
38 |
} |
39 |
|
40 |
public void testOffscreenRendering () { |
41 |
Scene scene = new Scene (); |
42 |
|
43 |
LayerWidget layer = new LayerWidget (scene); |
44 |
layer.setPreferredBounds (new Rectangle (0, 0, 80, 80)); |
45 |
scene.addChild (layer); |
46 |
|
47 |
LabelWidget widget = new LabelWidget (scene, "Hi"); |
48 |
widget.setVerticalAlignment (LabelWidget.VerticalAlignment.CENTER); |
49 |
widget.setAlignment (LabelWidget.Alignment.CENTER); |
50 |
widget.setBorder (BorderFactory.createLineBorder ()); |
51 |
widget.setPreferredLocation (new Point (20, 20)); |
52 |
widget.setPreferredBounds (new Rectangle (0, 0, 40, 40)); |
53 |
layer.addChild (widget); |
54 |
|
55 |
BufferedImage image = dumpSceneOffscreenRendering (scene); |
56 |
assertCleaness (testCleaness (image, Color.WHITE, Color.BLACK), image, null); |
57 |
|
58 |
assertScene (scene, Color.WHITE, new Rectangle (19, 19, 42, 42)); |
59 |
} |
60 |
|
61 |
private BufferedImage dumpSceneOffscreenRendering (Scene scene) { |
62 |
// validate the scene with a off-screen graphics |
63 |
BufferedImage emptyImage = new BufferedImage (1, 1, BufferedImage.TYPE_4BYTE_ABGR); |
64 |
Graphics2D emptyGraphics = emptyImage.createGraphics (); |
65 |
scene.validate (emptyGraphics); |
66 |
emptyGraphics.dispose (); |
67 |
|
68 |
// now the scene is calculated using the emptyGraphics, all widgets should be layout and scene has its size resolved |
69 |
// paint the scene with a off-screen graphics |
70 |
Rectangle viewBounds = scene.convertSceneToView (scene.getBounds ()); |
71 |
BufferedImage image = new BufferedImage (viewBounds.width, viewBounds.height, BufferedImage.TYPE_4BYTE_ABGR); |
72 |
Graphics2D graphics = image.createGraphics (); |
73 |
double zoomFactor = scene.getZoomFactor (); |
74 |
graphics.scale (zoomFactor, zoomFactor); |
75 |
scene.paint (graphics); |
76 |
graphics.dispose (); |
77 |
|
78 |
return image; |
79 |
} |
80 |
|
81 |
} |