0

I am working on a project in which I need to create graphics (bufferedimage) on another image.

I am using ZK framework for Front end and java for back end.

Here is my sample code and output.

public void createImage() throws Exception{ 
        ladleList = fD.getListLocation();

        baseImage = ImageIO.read(new File(BASE_IMG));

        Graphics2D baseGraphics = baseImage.createGraphics();

        for(LtsLadleLocation ladLoc : ladleList){

            setLadlePostion(ladLoc);
            setLadleImage(ladle);           

            Graphics2D imageGraphics = ladleImage.createGraphics();     
            imageGraphics.setFont(font);

            String ladleNo  = ladle.getLadleId().intValue() <=9 ? " "+ladle.getLadleId().toString() : ladle.getLadleId().toString();

            imageGraphics.drawString(ladleNo, 12, 45);
            baseGraphics.drawImage(ladleImage, position.getxVal(), position.getyVal(), 62,62,null,null);

            img.setContent(baseImage);          

            BindUtils.postNotifyChange(null, null, this, "img");

            ladle=null;
            position = null;
            ladleImage = null;          
        }
    }   


    /**
     * @Desc : selects the ladle and position values from the Position List.
     *          posList is having x and y values for particular locations.
     * @param : ladLoc and position
     */
    public void setLadlePostion(LtsLadleLocation ladLoc) {
        for(Position pos:posList){
            if(ladLoc.getLocationdescription().equalsIgnoreCase(pos.getLocName())){
                ladle = ladLoc;
                position = pos;
                break;          
            }
        }
    }

    /**
     * @Desc : This method is used to get the relevent image according to the location
     *          and according to the status.
     * @param ladle2
     */
    public void setLadleImage(LtsLadleLocation ladleImageObj) throws Exception {    
        if("LF_2".equalsIgnoreCase(ladleImageObj.getLocationdescription())){
            setLFLadle(ladleImageObj);
        }else{
            setNormalLadle(ladleImageObj);
        }
    }

    /**
     * @Desc : This method is used to set the normal ladle images (Circle Shaped images)
     * @param ladleImageObj
     */
    public void setNormalLadle(LtsLadleLocation ladleImageObj) throws Exception {
            if("OUT".equalsIgnoreCase(ladleImageObj.getStatus())){
                ladleImage = ImageIO.read(new File(HEATER_OUT));
            }else if("IN".equalsIgnoreCase(ladleImageObj.getStatus())){
                if("F".equalsIgnoreCase(ladleImageObj.getLadleStatusFlag())){
                    ladleImage = ImageIO.read(new File(HEATER_FILLED));
                }else{
                    ladleImage = ImageIO.read(new File(HEATER_EMPTY));
                }
            }       
    }

    /**
     * @Desc : For Setting LF images
     * @param ladleImageObj
     */
    public void setLFLadle(LtsLadleLocation ladleImageObj) throws Exception {
            if("OUT".equalsIgnoreCase(ladleImageObj.getStatus())){
                ladleImage = ImageIO.read(new File(LADLE_OUT));
            }else if("IN".equalsIgnoreCase(ladleImageObj.getStatus())){
                if("F".equalsIgnoreCase(ladleImageObj.getLadleStatusFlag())){
                    ladleImage = ImageIO.read(new File(LADLE_FILLED));
                }else{
                    ladleImage = ImageIO.read(new File(LADLE_EMPTY));
                }
            }       
    }

Where img is an element id of ZK element Image.

and my ZK code is

<window title="Hello World!!" border="normal" apply="org.zkoss.bind.BindComposer"
viewModel = "@id('vm') @init('com.practice.image.ImageViewModel')">
    <image id="img" >
        <custom-attributes org.zkoss.zul.image.preload="true" />
    </image>
    <timer id="refresh" repeats="true" onTimer="@command('createImage')" delay="10000"/>    
</window>

I need to have GIF image(blinking image) on ladle 7. but its not animating please help me through this.

The output is as followsenter image description here

Vijay Karchi
  • 13
  • 1
  • 5
  • I don't know ZK, sorry. But I do know that `BufferedImage`s don't animate. They can only represent a single frame of an animation. Using `ImageIO.read(...)` will only read the first frame of a GIF animation. You may want to look into [this great answer](http://stackoverflow.com/questions/22188940/gif-image-doesnt-moves-on-adding-it-to-the-jtabbed-pane/22190844#22190844) though. – Harald K Apr 18 '17 at 15:48
  • @HaraldK - Thanks for the reply. I went through the answer you have suggested. I have a question. Is imageicon used as image for web front end not for swing/desktop? – Vijay Karchi Apr 19 '17 at 04:21
  • `ImageIcon` is a Swing class, yes. For a web front end, I think you already have a native container capable of rendering an animated GIF (`img` HTML element), but as I said, I don't know ZK. – Harald K Apr 19 '17 at 07:41
  • As I am going to put a **"GIF"** file on **PNG** file is it going to render or not? – Vijay Karchi Apr 19 '17 at 08:12

0 Answers0