1 29 30 package nextapp.echo2.testapp.interactive.testscreen; 31 32 import java.awt.Graphics2D ; 33 import java.awt.image.BufferedImage ; 34 import java.io.IOException ; 35 import java.io.InputStream ; 36 import java.io.OutputStream ; 37 import java.net.URL ; 38 39 import javax.swing.ImageIcon ; 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 53 public class ImageReferenceTest extends TestGrid { 54 55 private static final String RESOURCE_IMAGE_LOCATION = Styles.IMAGE_PATH + "Two.jpg"; 56 57 private static final int BUFFER_SIZE = 4096; 58 59 private static final AwtImageReference AWT_IMAGE_REFERENCE; 61 static { 62 URL resourceUrl = ImageReferenceTest.class.getResource(RESOURCE_IMAGE_LOCATION); 63 ImageIcon imageIcon = new ImageIcon (resourceUrl); 64 BufferedImage image = new BufferedImage (85, 100, BufferedImage.TYPE_INT_RGB); 65 Graphics2D graphics = (Graphics2D ) 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 id = ApplicationInstance.generateSystemId(); 75 76 79 public String getContentType() { 80 return "image/jpeg"; 81 } 82 83 86 public String getRenderId() { 87 return id; 88 } 89 90 93 public void render(OutputStream out) throws IOException { 94 InputStream 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 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 |