Back to index

wims  3.65+svn20090927
PanelImage.java
Go to the documentation of this file.
00001 /*
00002 $Id: PanelImage.java,v 1.3 2003/02/18 11:48:46 sander Exp $
00003 */
00004 
00005 
00006 /*
00007 Copyright (C) 2001-2002 Mainline Project (I3S - ESSI - CNRS -UNSA)
00008 
00009 This library is free software; you can redistribute it and/or
00010 modify it under the terms of the GNU Lesser General Public
00011 License as published by the Free Software Foundation; either
00012 version 2.1 of the License, or (at your option) any later version.
00013 
00014 This library is distributed in the hope that it will be useful,
00015 but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 Lesser General Public License for more details.
00018 
00019 You should have received a copy of the GNU Lesser General Public
00020 License along with this library; if not, write to the Free Software
00021 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 
00023 For further information on the GNU Lesser General Public License,
00024 see: http://www.gnu.org/copyleft/lesser.html
00025 For further information on this library, contact: mainline@essi.fr
00026 */
00027 
00028 
00029 package fr.ove.errordialog;
00030 
00031 import java.awt.*;
00032 import java.io.*;
00033 import fr.ove.utils.ByteVector;
00034 
00045 /* The image is loaded fom the package of the instance fr.ove.utils.<BR>
00046 * So when we create a new instance, the path of the image to load must
00047 * must be relatif to fr.ove.utils.
00048 * For example, in the package fr.ove.errordialog we create a PanelImage with
00049 * the smiley.gif image (in the same directory/package), so the instanciation
00050 * must be new <CODE>PanelImage("../errordialog/smiley.gif");</CODE> to respect
00051 * the requirements aforementioned.
00052 */
00053 public class PanelImage extends Panel {
00054     private Image image;
00055     
00060     public PanelImage(String imageName) throws Exception {
00061               java.io.InputStream iStream = getClass().getResourceAsStream(imageName);
00062               
00063         try {
00064             /*
00065             int avaliable = iStream.available();
00066             byte imageBytes[] = new byte[avaliable];
00067             int bytesRead = iStream.read(imageBytes);
00068             bytesRead = iStream.read(imageBytes);
00069             image = (Toolkit.getDefaultToolkit()).createImage(imageBytes);
00070             iStream.close();
00071             */
00072             
00073             int read;
00074             ByteVector byteImage = new ByteVector();
00075                   while ((read = iStream.read()) != -1) {
00076                       byteImage.addElement((byte) read);
00077                       if (iStream.available() == 0)
00078                           break;
00079                   }
00080             
00081             image = (Toolkit.getDefaultToolkit()).createImage(byteImage.getBytes());
00082             iStream.close();
00083         }
00084         catch (IOException e){
00085             System.out.println("Impossible to read image : IO problems");
00086             e.printStackTrace();
00087         }
00088         catch (Exception e){
00089             System.out.println("Impossible to create image");
00090             e.printStackTrace();
00091         }
00092         
00093         if (image != null) {
00094               MediaTracker tracker = new MediaTracker(this);
00095             tracker.addImage(image, 0);
00096         
00097             // On attend que toutes les images soient chargées.
00098               try { 
00099                   tracker.waitForAll();
00100               }
00101               catch (InterruptedException e) {
00102                   System.out.print(e.toString());
00103               }
00104               
00105               if (tracker.isErrorAny())
00106                   throw new Exception("Impossible to load image");
00107         }
00108     }
00109     
00113     public Dimension getPreferredSize() {
00114         return new Dimension(image.getWidth(this), image.getHeight(this));
00115     }
00116     
00121     public void paint(Graphics g) {
00122         g.drawImage(image, 0, 0, this);
00123     }
00124 }