我正在考虑这个展示:http://www.primefaces.org/showcase/ui/dynamicImage.jsf http://www.primefaces.org/showcase/ui/dynamicImage.jsf特别是“动态图形文本”子种姓。
我的问题是通过添加一个 .当按下按钮时,我需要图像动态变化。
在 DynamicImageController 类中,我重写了与 GraphicImage 关联的 getter:
public StreamedContent getGraphicText(){
double random = Math.random();// a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.
if(random>0.5){
BufferedImage bufferedImg = new BufferedImage(100, 25, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = bufferedImg.createGraphics();
g2.drawString("This is a text", 0, 10);
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
ImageIO.write(bufferedImg, "png", os);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
graphicText = new DefaultStreamedContent(new ByteArrayInputStream(os.toByteArray()), "image/png");
} else {
BufferedImage bufferedImg = new BufferedImage(100, 25, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = bufferedImg.createGraphics();
g2.drawString("This is another text", 0, 10);
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
ImageIO.write(bufferedImg, "png", os);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
graphicText = new DefaultStreamedContent(new ByteArrayInputStream(os.toByteArray()), "image/png");
}
return graphicText;
}
我有这个按钮:
<p:commandButton id="refreshImageButton" value="Refresh image random">
<p:ajax update=":idForm" />
</p:commandButton>
和图像:
<p:graphicImage value="#{dynamicImageController.graphicText}" />
idForm 是包含我的graphicImage 和我的commandButton 的表单的表单id
我的问题是:
为什么如果我按键盘上的 F5 按钮,图像会随机变化,与 getGraphicText 方法中所需的行为一致?
为什么当我按下按钮时图像没有改变?
Thanks.
附:我真正的问题是 jcaptcha 在 primefaces 中的集成,我的集成几乎终止,我只错过了验证码图像的刷新按钮