7

I'm considering this showcase: http://www.primefaces.org/showcase/ui/dynamicImage.jsf in particular the sub-case "GraphicText on-the-fly".

My problem is implementing an extended version of this sub case with the addition of a . When the button is pressed i need that the image change dinamically.

In the DynamicImageController class I re-writed the getter associeted with the graphicImage:

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;
 }

I have this button:

<p:commandButton id="refreshImageButton" value="Refresh image random">
                <p:ajax update=":idForm" />
             </p:commandButton>

and the image:

<p:graphicImage value="#{dynamicImageController.graphicText}" />

idForm is the form id of the form containing my graphicImage and my commandButton

My question is:

Why if I press the F5 button on the keyboard the image change random consistent with the desired behavior in the getGraphicText method ? And why when I press the button the image doesn't change?

Thanks.

ps. my real problem is the integration of jcaptcha in primefaces, my integration is almost termineted, i miss only the refresh button for the captcha image

iljkr
  • 161
  • 1
  • 1
  • 8

2 Answers2

12

By default the graphicImage caches the image.
Set the cache attribute to false on your p:graphicImage
Something like this:
<p:graphicImage value="#{dynamicImageController.graphicText}" cache="false" />

S1LENT WARRIOR
  • 11,704
  • 4
  • 46
  • 60
  • @user3147345! you're right about the firefox. i just tested this on firefox and turning cache off doesn't have any effect on it! It appears to be a bug in primefaces implementation. B.t.w which primefaces version are you using? – S1LENT WARRIOR Mar 03 '14 at 09:58
  • Mine is the 4.0 community edition as well! I think we must post this bug to PrimeFaces Bugs list! – S1LENT WARRIOR Mar 06 '14 at 02:28
  • 1
    it's working for me, my version is 5.1, not tested on ie, but working on ff and chrome. – Vineeth NG Mar 20 '15 at 09:16
2

Ok update: the problem is a bug of primefaces I discussed here the problems:

http://forum.primefaces.org/viewtopic.php?f=3&t=35637&p=113830#p113830

iljkr
  • 161
  • 1
  • 1
  • 8