• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 225
  • Last Modified:

conversion of a Object(e,g Canvas or Panel) in to image of Gif or Jpg format

I want to convert a object(e,g Panel or Canvas) along with any thing which has drawn on it, into Gif/Jpg image.
0
nadirkamal
Asked:
nadirkamal
  • 6
  • 6
1 Solution
 
MurthyMvlnCommented:
Any component can be converted to GIF/jpg image. This we can convert
using GIFEncoder( third party tool ).
This GIFEncoder utilities can be downloaded from www.acme.com.
After downloading this, import those classes to your file and write down the following code.

FileOutputStream fos =
  new FileOutputStream(filePathName);

Image img = this.createImage(
                   this.getWidth(),
                   this.getHeight());
//Here this will be any component
GifEncoder gifEncoder = new GifEncoder(
                img,fos);              
gifEncoder.encode();
fos.flush();
fos.close();

If any problems mail me back.

Murthy Mvln
0
 
MurthyMvlnCommented:
I hope, i given the correct solution for your problem.
0
 
siayubiCommented:
Dear MurthyMvln

GifEncoder only has the following Constractor

GifEncoder(Image, OutputStream)
GifEncoder(Image, OutputStream,boolean)
GifEncoder(ImageProducer, OutputStream)
GifEncoder(ImageProducer, OutputStream, boolean)

Your "fos" is not a OutputStream, it is FileOutputStream so how it can initiallize

Waiting for response.....

Sohail Iqbal
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
siayubiCommented:
Sorry!

Please also told me that what is filePathName?
Is it is File or String?
0
 
siayubiCommented:
I have use your code, it has compile correctly but give NoClassDefFondError
Exception.

Sohail Iqbal
0
 
MurthyMvlnCommented:
1) Please also told me that what is filePathName?

Is it is File or String
Here the filePathName is the File Path where you want to store it.
eg. File filePathName = new File("../siayubi.gif");

2) I have use your code, it has compile correctly but give NoClassDefFondError
Exception.

check out where your class is created. when ever you are running your programme it is not able found the class.

eg. with out existing Temp.class, if we are trying to execute that( ie. java Temp ) then this Exception will come.
so check out for your class file.

0
 
MurthyMvlnCommented:
1) Please also told me that what is filePathName?

Is it is File or String
Here the filePathName is the File Path where you want to store it.
eg. File filePathName = new File("../siayubi.gif");

2) I have use your code, it has compile correctly but give NoClassDefFondError
Exception.

check out where your class is created. when ever you are running your programme it is not able found the class.

eg. with out existing Temp.class, if we are trying to execute that( ie. java Temp ) then this Exception will come.
so check out for your class file.

0
 
siayubiCommented:
Dear MurthyMvln

I think that the Exception is comming due to the following reason....

GifEncoder only has the following Constractor

GifEncoder(Image, OutputStream)
GifEncoder(Image, OutputStream,boolean)
GifEncoder(ImageProducer, OutputStream)
GifEncoder(ImageProducer, OutputStream, boolean)

Your "fos" is not a OutputStream, it is FileOutputStream so how it can initiallize

Waiting for response.....

Sohail Iqbal
0
 
MurthyMvlnCommented:
OutputStream is the super class for FileOutputStream. If you are passing the FileOutputStream, it will convert to OutputStream with out any problem.
so that is not the problem. please check what ever i mentioned before.

0
 
siayubiCommented:
Dear MurthyMvln

I am sending you all my code please compile it and see the Exception.

Please where the problem is....
and also send me you email address.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import Acme.JPM.Encoders.*;
import java.io.*;
import java.net.*;

public class Converter{
  TextCanvas canvas;
  public Converter(){
    File filePathName = new File("../siayubi.gif");
    canvas = new TextCanvas();
    canvas.createImage(filePathName);
  }
      public static void main(String args[]){
            System.out.println("Sohail Iqbal");
    new Converter();
      }
}

class TextCanvas extends Canvas{

      Graphics       g;

      TextCanvas(){
            g=getGraphics();
            setBackground(Color.blue);
            setFont(new Font("Arial",Font.BOLD,28));
      }

      public void createImage(File filePathName)
      {
             try{
               FileOutputStream fos =  new FileOutputStream(filePathName);
               Image img = this.createImage(this.getWidth(),this.getHeight());
               //Here this will be any component
               GifEncoder gifEncoder = new GifEncoder(img,fos);
               gifEncoder.encode();
               fos.flush();
               fos.close();
            } catch (IOException ignored){}
      }

      public void paint(Graphics g){
            g.drawString("Sohail Iqbal", 40,40);
      }
}


Best Regards
Sohail Iqbal
0
 
siayubiCommented:
Dear Murthy Mvln

Whenerver I run this code it has given the NullPointerException.
Please help me I will be very thankful to you.

Best Regards
Sohail Iqbal
0
 
MurthyMvlnCommented:
Hi Sohail!

Here TextCanvas extending Canvas. You are taking the instance of this TextCanvas. But you are not adding to any Panel or Frame. You should add the Canvas to Panel or Frame. Then only it will go to paint method.

please find the difference here.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import com.Printing.JPM.Encoders.*;
import java.io.*;
import java.net.*;

public class Converter extends Frame
{
  TextCanvas canvas;
  public Converter()
  {
     setVisible(true);
     setSize(100,100);
     canvas = new TextCanvas();
     add(canvas);
     canvas.createImage();
  }
  public static void main(String args[])
  {
     new Converter();
  }
}

class TextCanvas extends Canvas
{
   TextCanvas()
   {
        setSize(100,100);
        setVisible(true);
        setBackground(Color.blue);
        setFont(new Font("Arial",Font.BOLD,28));
   }

   public void createImage()
   {
     try
     {
           OutputStream fos =  new FileOutputStream("c:/Temp.gif");
           Image img = this.createImage(this.getWidth(),this.getHeight());
           GifEncoder gifEncoder = new GifEncoder(img,fos);
           gifEncoder.encode();
           fos.flush();
           fos.close();
      }
      catch(Exception e)
      {
         System.out.println("Error: "+e.toString());
      }
    }

    public void paint(Graphics g)
    {
        g.drawString("Welcome to GifEncoder", 40,40);
    }
}

This code is executing correctly.

still problems mail me to
murthy@claygency.com

MurthyMvln
0
 
nadirkamalAuthor Commented:
Thanks from Sohail Iqbal
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 6
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now