1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
package com.vaadin.tests.components.embedded;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import javax.imageio.ImageIO;
import com.vaadin.terminal.StreamResource;
import com.vaadin.tests.components.TestBase;
import com.vaadin.ui.Button;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.Button.ClickEvent;
public class EmbeddedImageRefresh extends TestBase {
@Override
protected String getDescription() {
return "Tests if requestRepaint() makes the browser reload a dynamic resource.";
}
@Override
protected Integer getTicketNumber() {
return 2470;
}
@Override
protected void setup() {
// Create the embedded.
final Embedded embedded = new Embedded();
embedded.setDescription("Click on the grid cells to switch them.");
addComponent(embedded);
// Attach it to a resource.
final MyImageSource imageSource = new MyImageSource();
final StreamResource imageResource = new StreamResource(imageSource,
"testimage.png", this);
imageResource.setCacheTime(0);
embedded.setSource(imageResource);
// The button requests repainting the embedded.
Button button = new Button("refr");
button.addListener(new Button.ClickListener() {
public void buttonClick(ClickEvent event) {
embedded.requestRepaint();
}
});
addComponent(button);
button = new Button("refr name");
button.addListener(new Button.ClickListener() {
public void buttonClick(ClickEvent event) {
((StreamResource) embedded.getSource()).setFilename(new Date()
.getTime()
+ ".png");
embedded.requestRepaint();
}
});
addComponent(button);
button = new Button("200x200");
button.addListener(new Button.ClickListener() {
public void buttonClick(ClickEvent event) {
embedded.setWidth("200px");
embedded.setHeight("200px");
}
});
addComponent(button);
button = new Button("undef");
button.addListener(new Button.ClickListener() {
public void buttonClick(ClickEvent event) {
embedded.setSizeUndefined();
}
});
addComponent(button);
}
public class MyImageSource implements StreamResource.StreamSource {
public MyImageSource() {
}
int intervalPos(int pos, int resolution, int cells) {
return (int) Math.round(pos * resolution / (cells * 1.0));
}
public InputStream getStream() {
// Create an image and draw some background on it.
BufferedImage image = new BufferedImage(640, 480,
BufferedImage.TYPE_INT_RGB);
Graphics drawable = image.getGraphics();
// Background
drawable.setColor(Color.white);
drawable.fillRect(0, 0, 640, 480);
final int rows = 10;
final int cols = 10;
// Grid
for (int row = 0; row < rows; row++) {
int gridy = intervalPos(row, 480, rows);
int gridynext = intervalPos(row + 1, 480, rows);
// Horizontal grid line
if (row > 0) {
drawable.setColor(Color.lightGray);
drawable.drawLine(0, gridy, 640 - 1, gridy);
}
for (int col = 0; col < cols; col++) {
int gridx = intervalPos(col, 640, cols);
int gridxnext = intervalPos(col + 1, 640, cols);
// Vertical grid line
if (row == 0 && col > 0) {
drawable.setColor(Color.lightGray);
drawable.drawLine(gridx, 0, gridx, 480 - 1);
}
// Cell
if (Math.random() < 0.5f) {
drawable.setColor(Color.white);
} else {
drawable.setColor(Color.black);
}
drawable.fillRect(gridx + 1, gridy + 1, gridxnext - gridx
- 1, gridynext - gridy - 1);
}
}
try {
// Write the image to a buffer.
ByteArrayOutputStream imagebuffer = new ByteArrayOutputStream();
ImageIO.write(image, "png", imagebuffer);
// Return a stream from the buffer.
ByteArrayInputStream istream = new ByteArrayInputStream(
imagebuffer.toByteArray());
return istream; // new DownloadStream (istream,null,null);
} catch (IOException e) {
return null;
}
}
}
}
|