View Javadoc

1   package fr.ove.errordialog;
2   
3   import java.awt.*;
4   import java.io.*;
5   import fr.ove.utils.ByteVector;
6   
7   /***
8   * A simple panel that contains an image to display.<BR>
9   * The image to display must be in the same package of the instance.
10  * (possibly the subpackages with the correct path specified in the 
11  * constructo)
12  *
13  * @author © 1998 DIRAT Laurent
14  * @version 1.0  13/04/99
15  */
16  
17  /* The image is loaded fom the package of the instance fr.ove.utils.<BR>
18  * So when we create a new instance, the path of the image to load must
19  * must be relatif to fr.ove.utils.
20  * For example, in the package fr.ove.errordialog we create a PanelImage with
21  * the smiley.gif image (in the same directory/package), so the instanciation
22  * must be new <CODE>PanelImage("../errordialog/smiley.gif");</CODE> to respect
23  * the requirements aforementioned.
24  */
25  public class PanelImage extends Panel {
26      private Image image;
27      
28      /***
29      * The constructor.
30      * @param imageName the name of the image the instance displays.
31      */
32      public PanelImage(String imageName) throws Exception {
33  		java.io.InputStream iStream = getClass().getResourceAsStream(imageName);
34  		
35          try {
36              /*
37              int avaliable = iStream.available();
38              byte imageBytes[] = new byte[avaliable];
39              int bytesRead = iStream.read(imageBytes);
40              bytesRead = iStream.read(imageBytes);
41              image = (Toolkit.getDefaultToolkit()).createImage(imageBytes);
42              iStream.close();
43              */
44              
45              int read;
46              ByteVector byteImage = new ByteVector();
47  		    while ((read = iStream.read()) != -1) {
48  		        byteImage.addElement((byte) read);
49  		        if (iStream.available() == 0)
50  		            break;
51  		    }
52              
53              image = (Toolkit.getDefaultToolkit()).createImage(byteImage.getBytes());
54              iStream.close();
55          }
56          catch (IOException e){
57              System.out.println("Impossible to read image : IO problems");
58              e.printStackTrace();
59          }
60          catch (Exception e){
61              System.out.println("Impossible to create image");
62              e.printStackTrace();
63          }
64          
65          if (image != null) {
66      		MediaTracker tracker = new MediaTracker(this);
67              tracker.addImage(image, 0);
68          
69              // On attend que toutes les images soient chargÈes.
70      		try { 
71      		    tracker.waitForAll();
72      		}
73      		catch (InterruptedException e) {
74      		    System.out.print(e.toString());
75      		}
76      		
77      		if (tracker.isErrorAny())
78      		    throw new Exception("Impossible to load image");
79          }
80      }
81      
82      /***
83      * Returns the preferred size of the instance.
84      */
85      public Dimension getPreferredSize() {
86          return new Dimension(image.getWidth(this), image.getHeight(this));
87      }
88      
89      /***
90      * Paints the instance.
91      * @param g the graphics where to paint.
92      */
93      public void paint(Graphics g) {
94          g.drawImage(image, 0, 0, this);
95      }
96  }