Reshma
(Reshma N)
February 14, 2014, 4:49am
1
Hi,
I have a problem similar to this :https://vaadin.com/old-forum/#!/thread/2789663/2789662
I am trying to check out the sample code for StreamResource from the Book of Vaadin. However in my case the getStream() method is not invoked. Am I missing something ?
Reshma
(Reshma N)
February 14, 2014, 4:58am
2
Code snippet are as follows.
The UI Class:
public class ImagesourceprojectUI extends UI {
@WebServlet(value = "/*", asyncSupported = true)
@VaadinServletConfiguration(productionMode = false, ui = ImagesourceprojectUI.class)
public static class Servlet extends VaadinServlet {
}
@Override
protected void init(VaadinRequest request) {
final VerticalLayout layout = new VerticalLayout();
layout.setMargin(true);
setContent(layout);
Button button = new Button("Click Me");
button.addClickListener(new Button.ClickListener() {
public void buttonClick(ClickEvent event) {
layout.addComponent(new Label("Thank you for clicking"));
}
});
layout.addComponent(button);
StreamResource.StreamSource imagesource = new ImageSource();
StreamResource resource =
new StreamResource(imagesource, "myimage.png");
layout.addComponent(new Image("Image title", resource));
}
}
The Class implementing StreamSource
public class ImageSource implements StreamSource {
ByteArrayOutputStream imagebuffer = null;
int reloads = 0;
@Override
public InputStream getStream() {
System.out.println("In getStream");
// TODO Auto-generated method stub
BufferedImage image = new BufferedImage (200, 200,
BufferedImage.TYPE_INT_RGB);
Graphics drawable = image.getGraphics();
drawable.setColor(Color.lightGray);
drawable.fillRect(0,0,200,200);
drawable.setColor(Color.yellow);
drawable.fillOval(25,25,150,150);
drawable.setColor(Color.blue);
drawable.drawRect(0,0,199,199);
drawable.setColor(Color.black);
drawable.drawString("Reloads="+reloads, 75, 100);
reloads++;
try {
/* Write the image to a buffer. */
imagebuffer = new ByteArrayOutputStream();
ImageIO.write(image, "png", imagebuffer);
/* Return a stream from the buffer. */
return new ByteArrayInputStream(
imagebuffer.toByteArray());
} catch (IOException e) {
return null;
}
}
}