1 Reply Latest reply on Oct 1, 2008 2:32 PM by Sanjeev Koppal

    a4j:mediaOutput created by JAVA

    Ahmet temiz Novice

      hello

      I need an example about a4j:mediaOutput component
      created and implemented by JAVA code.

      if you provide I will appreciate.

      regards

        • 1. Re: a4j:mediaOutput created by JAVA
          Sanjeev Koppal Newbie

           

          <a4j:mediaOutput style="float:right;margin: 0px 15px 0px 15px;" element="img" cacheable="false" session="false"
           createContent="#{mediaBeanTest.paint}" value="#{mediaDataTest}" mimeType="image/jpg" />
          


          JAVA CODE MediaBean

          
          
          //In constructor
           File f = new File(getServletContext().getRealPath("/images/body_front.jpg"));
           BufferedImage bodyImgFront1 = loadImage(f);
          
           //PAINT called by mediaoutput
           public void paint(OutputStream out, Object data) throws IOException{
          
           /// HELPER BEAN CONTAINING DIMENSIONS OF THE IMAGE TO BE DRAWN
           if (data instanceof MediaData) {
           MediaData paintData = (MediaData) data;
          
           BufferedImage img = new BufferedImage(paintData.getWidth(),paintData.getHeight(),BufferedImage.TYPE_INT_RGB);
          
           Graphics2D graphics2D = img.createGraphics();
           GradientPaint bg = new GradientPaint(150/2,0,new Color(255,255,255),300/2, 150/2,new Color(255,255,255));
           graphics2D.setPaint(bg);
           graphics2D.fill (new Rectangle2D.Double(0, 0, 150, 300));
          
           // graphics2D.drawImage(bodyImgFront1, null, 0, 0);
           graphics2D.drawImage(bodyImgFront1, 2,2, 150,300,new ImageObserver(){
           @Override
           public boolean imageUpdate(Image img, int infoflags, int x, int y,
           int width, int height) {
           return true;
           }
           });
           resetImage(graphics2D,true);
           //needed if REQUIREMENT is to overlay something on background
           fillImageBodyPart(graphics2D, "neck", low);
          
           ImageIO.write(img,"jpeg",out);
           graphics2D.dispose();
          
           }
          
           //Helper method to paint on top of a background image
           public void fillImageBodyPart(Graphics2D graphics,String level,String bodyPart){
          
          
           //********** LOW *****************/
           if(riskLevel.equalsIgnoreCase("Low")){
           graphics.setColor(new Color(0x9BCA00));
           }
           //********** HIGH *****************/
           else if(riskLevel.startsWith("Mod")){
           graphics.setColor(new Color(0xFFC600));
           }
           //********** MODERATE *****************/
           else if(riskLevel.equalsIgnoreCase("High")){
           graphics.setColor(new Color(0xC60000));
           }
           else{
           graphics.setColor(new Color(0x2E77AA));
           }
          
           if(bodyPart.equalsIgnoreCase("neck")){
           //NECK ----------------------------------------------- NECK
           graphics.fillOval(75, 45, 10, 10);
           }
           }
          
           //Load the image
           private BufferedImage loadImage(File f) throws IOException {
          
           // read the raw bytes
           FileInputStream fis = new FileInputStream(f);
           byte[] data = new byte[(int) f.length()];
           fis.read(data);
           fis.close();
          
           // create the BufferedImage
           return ImageIO.read(f);
           }
          
          


          Hope it helps
          Njoy....

          -S