Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/adorphuye/othello/gui/GIFImageProvider.java


1   package com.adorphuye.othello.gui;
2   
3   import java.io.BufferedInputStream;
4   import java.net.URL;
5   import java.awt.Image;
6   import com.fmsware.GifDecoder;
7   
8   public class GIFImageProvider
9   {
10    public static final int NONE    =-1;
11    public static final int  APPLET    = 0;
12    public static final int APPLICATION  = 1;
13    
14    private static final GIFImageProvider singleton;
15    
16    private int source = NONE;
17    
18    static
19    {
20      singleton = new GIFImageProvider();
21    }
22    
23    private GIFImageProvider()
24    {
25    }
26    
27    /**
28     * @return  */  
29    public static GIFImageProvider getInstance()
30    {
31      return singleton;
32    }
33    
34    /**
35     * @param src  */  
36    public void setSource(int src)
37    {
38      source = src;
39    }
40    
41    /**
42     * @return  */  
43    public int getSource()
44    {
45      return source;
46    }
47    
48    /**
49     * @param url
50     * @return  */  
51    public Image getImage(URL url)
52    {
53      Image[] img = new Image[1];
54      img = getImage(url,img);
55      return img[0];
56    }
57    
58    /**
59     * @param url
60     * @param img
61     * @return  */  
62    public Image[] getImage(URL url, Image[] img)
63    {
64      switch(getSource())
65      {
66        case APPLET:
67          img = getAppletImage(url,img);
68          break;
69        case APPLICATION:
70          img = getApplicationImage(url,img);
71          break;
72        default:
73          throw new IllegalArgumentException("Invalid ImageProducer source: "+getSource());
74      }
75      return img;
76    }
77    
78    /**
79     * @param url
80     * @param img
81     * @return  */  
82    private Image[] getAppletImage(URL url, Image[] img)
83    {
84      return getApplicationImage(url,img);
85    }
86    
87    /**
88     * @param url
89     * @param img
90     * @return  */  
91    private Image[] getApplicationImage(URL url, Image[] img)
92    {
93      try
94      {
95        BufferedInputStream is = new BufferedInputStream(url.openStream());
96        GifDecoder gd = new GifDecoder();
97        gd.read(is);
98        int n = gd.getFrameCount();
99        for (int i = 0; i < n && i< img.length; i++)
100       {
101         img[i] = gd.getFrame(i);
102       }
103     }
104     catch(Exception e)
105     {
106       e.printStackTrace();
107     }
108     return img;
109   }
110 }