function writegif(GIF,fignum)

% WRITEGIF  - Create a GIF file from a figure
% 
% Use as: writegif(GIFfile,fignum)
% Inputs: GIFfile = Name of GIF image file to be created
%         fignum  = Figure Number to save as GIF (optional, 
%                   default = gcf)
%
% Example: surf(peaks);writegif('trashme.gif')
%
% Known issues: 
%     WRITEGIF will crash if the figure contains more than 256 colors.
% This is a limitation of the ACME java library used for GIF encoding.
%
% Note: 
%     WRITEGIF first creates a temporary PNG file in a temporary directory. 
% For most users, this will be the current working directory (try 'pwd' at the 
% matlab prompt to see the current directory). If you do not have write permission
% in this directory change to one that you do before using writegif, otherwise
% writegif will crash.
% 
%
% Installation:
%      WRITEGIF uses Matlab's java interface to call the static method
% 'convert' in the java class 'PngToGif'. It requires two freely
% available java packages and the class PngToGif to be on MATLAB's
% classpath:
% 
% The packages required are:
%   1. The java PNG library to decode PNG images. Distributed under the
%      GNU GPL. http://www.visualtek.com/PNG/index.html
%   2. ACME java library to encode the GIF images. Distributed free for 
%      any kind of use (Commercial, Educational, whatever).
%      http://www.acme.com/resources/classes/Acme.tar.Z
%   3. PngToGif.class
%
% The following is a quote from Matlab's solution seach:
%   For MATLAB to see your classes they must be in a directory on 
%   MATLAB's classpath (that's classpath, NOT MATLABPATH). MATLAB's 
%   classpath is kept in a file called classpath.txt in your 
%   toolbox/local directory (type which classpath.txt at the MATLAB 
%   prompt). Add the full path to the directory containing your .class 
%   files to this file. Classpath.txt is only read once when MATLAB 
%   starts, so changes to it or your .class files require you to restart
%   MATLAB before they will take effect.
%
% For Example...
% The *.jar and PngToGif.class files are in the same dir so I added
%   the following line to the classpath.txt file.
%   $matlabroot/toolbox/local/tools/png.jar
%   $matlabroot/toolbox/local/tools/acme.jar
%   $matlabroot/toolbox/local/tools/
%
% Also.....
% 	If you get errors saying "MATLAB is unable to start the virtual machine"
% 	then Java is most likely not installed on your machine.
% 	Installing MATLAB does not automatically install Java on your
% 	machine. If you do not already have Java installed, you can install it
% 	on a PC by  running "msjavx86.exe" from your Matlab bin directory.
% 	It will ask you if you want to install Java support for Internet
% 	Explorer. Press Yes, It will install Java, not Internet Explorer. On a
% 	Sun workstation, you'll have to download and install the "JRE" 
% 	(Java Runtime Environment) from Sun at www.javasoft.com.
%

% 11 Apr 1999; Brian Schlining - created
% 25 May 1999; Stephanie Flora - added error handling

if nargin == 0
  help writegif
  return
end

H = findobj('Type','figure');
if isempty(H)
  disp('  WRITEGIF Error (A figure must exist)')
  return
end

if nargin < 2
  fignum = gcf;
end

%java('on');                                      % Activate Java
%try
%    setJavaClasspath
%catch
%    warning('Failed to dynamically set the java classpath.');
%end

% Create PNG file name (a file format Matlab can create)
[File.dir File.filename File.ext] = fileparts(GIF);
if isempty(File.ext)
  File.ext = '.gif';
end
PNG = fullfile(tempdir, [File.filename '.png']);
if ~isempty(File.dir)
   GIF = [File.dir filesep File.filename File.ext];
else
   GIF = [File.filename File.ext];
end

% Print the figure as a PNG file into the work dir
print('-dpng',['-f' num2str(fignum)],PNG);

% Use Java to convert the PNG to a GIF
java_method('convert', 'PngToGif', PNG, GIF);

% Delete the PNG file from the work dir
delete(PNG);

