KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nextapp > echo2 > testapp > interactive > testscreen > ImageReferenceTest


1 /*
2  * This file is part of the Echo Web Application Framework (hereinafter "Echo").
3  * Copyright (C) 2002-2005 NextApp, Inc.
4  *
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * Alternatively, the contents of this file may be used under the terms of
18  * either the GNU General Public License Version 2 or later (the "GPL"), or
19  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
20  * in which case the provisions of the GPL or the LGPL are applicable instead
21  * of those above. If you wish to allow use of your version of this file only
22  * under the terms of either the GPL or the LGPL, and not to allow others to
23  * use your version of this file under the terms of the MPL, indicate your
24  * decision by deleting the provisions above and replace them with the notice
25  * and other provisions required by the GPL or the LGPL. If you do not delete
26  * the provisions above, a recipient may use your version of this file under
27  * the terms of any one of the MPL, the GPL or the LGPL.
28  */

29
30 package nextapp.echo2.testapp.interactive.testscreen;
31
32 import java.awt.Graphics2D JavaDoc;
33 import java.awt.image.BufferedImage JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.io.InputStream JavaDoc;
36 import java.io.OutputStream JavaDoc;
37 import java.net.URL JavaDoc;
38
39 import javax.swing.ImageIcon JavaDoc;
40
41 import nextapp.echo2.app.ApplicationInstance;
42 import nextapp.echo2.app.AwtImageReference;
43 import nextapp.echo2.app.HttpImageReference;
44 import nextapp.echo2.app.Label;
45 import nextapp.echo2.app.ResourceImageReference;
46 import nextapp.echo2.app.StreamImageReference;
47 import nextapp.echo2.testapp.interactive.Styles;
48 import nextapp.echo2.testapp.interactive.TestGrid;
49
50 /**
51  * An interactive test for <code>ImageReference</code>s.
52  */

53 public class ImageReferenceTest extends TestGrid {
54     
55     private static final String JavaDoc RESOURCE_IMAGE_LOCATION = Styles.IMAGE_PATH + "Two.jpg";
56     
57     private static final int BUFFER_SIZE = 4096;
58     
59     // Static for memory conservation w/ live demos.
60
private static final AwtImageReference AWT_IMAGE_REFERENCE;
61     static {
62         URL JavaDoc resourceUrl = ImageReferenceTest.class.getResource(RESOURCE_IMAGE_LOCATION);
63         ImageIcon JavaDoc imageIcon = new ImageIcon JavaDoc(resourceUrl);
64         BufferedImage JavaDoc image = new BufferedImage JavaDoc(85, 100, BufferedImage.TYPE_INT_RGB);
65         Graphics2D JavaDoc graphics = (Graphics2D JavaDoc) image.getGraphics();
66         graphics.drawImage(imageIcon.getImage(), 0, 0, null);
67         graphics.setColor(java.awt.Color.BLUE);
68         graphics.drawString("Java2D", 5, 40);
69         AWT_IMAGE_REFERENCE = new AwtImageReference(image);
70     }
71     
72     private StreamImageReference streamImageReference = new StreamImageReference() {
73
74         private String JavaDoc id = ApplicationInstance.generateSystemId();
75         
76         /**
77          * @see nextapp.echo2.app.StreamImageReference#getContentType()
78          */

79         public String JavaDoc getContentType() {
80             return "image/jpeg";
81         }
82         
83         /**
84          * @see nextapp.echo2.app.RenderIdSupport#getRenderId()
85          */

86         public String JavaDoc getRenderId() {
87             return id;
88         }
89
90         /**
91          * @see nextapp.echo2.app.StreamImageReference#render(java.io.OutputStream)
92          */

93         public void render(OutputStream JavaDoc out) throws IOException JavaDoc {
94             InputStream JavaDoc in = null;
95             byte[] buffer = new byte[BUFFER_SIZE];
96             int bytesRead = 0;
97             
98             try {
99                 in = ImageReferenceTest.class.getResourceAsStream(RESOURCE_IMAGE_LOCATION);
100                 do {
101                     bytesRead = in.read(buffer);
102                     if (bytesRead > 0) {
103                         out.write(buffer, 0, bytesRead);
104                     }
105                 } while (bytesRead > 0);
106             } finally {
107                 if (in != null) { try { in.close(); } catch (IOException JavaDoc ex) { } }
108             }
109         }
110     };
111     
112     public ImageReferenceTest() {
113         addHeaderRow("ImageReference Types");
114         HttpImageReference httpImageReference = new HttpImageReference("images/two.jpg");
115         ResourceImageReference resourceImageReference
116                 = new ResourceImageReference(RESOURCE_IMAGE_LOCATION);
117         addTestRow("AwtImageReference", new Label(AWT_IMAGE_REFERENCE));
118         addTestRow("HttpImageReference", new Label(httpImageReference));
119         addTestRow("ResourceImageReference", new Label(resourceImageReference));
120         addTestRow("StreamImageReference", new Label(streamImageReference));
121     }
122 }
123
Popular Tags