Back to index

wims  3.65+svn20090927
ImageSymbol.java
Go to the documentation of this file.
00001 /*
00002 $Id: ImageSymbol.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.openmath.jome.ctrlview.bidim;
00030 
00031 import java.awt.*;
00032 import java.awt.image.ImageObserver;
00033 import fr.ove.openmath.jome.ctrlview.bidim.*;
00034 import fr.ove.openmath.jome.ctrlview.bidim.images.ImageLoader;
00035 
00042 public class ImageSymbol extends DisplayableImpl {
00046     private String imgId;
00047     
00056     private Image symbolInit;
00057     private Image symbol;
00058         
00062     private ImageObserver observer;
00063     
00064     private Insets insets = new Insets(0, 0, 0, 0);
00065         
00071     public ImageSymbol(String imgId, ImageObserver observer) {
00072         symbolInit = ImageLoader.getImage(imgId);
00073         this.observer = observer;
00074         this.imgId = imgId;
00075     }
00076     
00085     public void setGraphicContext(GraphicContext graphicContext) {
00086         super.setGraphicContext(graphicContext);
00087         FontMetrics fm = Toolkit.getDefaultToolkit().getFontMetrics(graphicContext.getFont());
00088         
00089         float heightFactor = ImageLoader.getHeightFactor(imgId);
00090         int fontHeight = fm.getHeight();
00091         
00092         insets.top = Math.round(ImageLoader.getTopInset(imgId)*fontHeight);
00093         insets.bottom = Math.round(ImageLoader.getBottomInset(imgId)*fontHeight);
00094         insets.left = Math.round(ImageLoader.getLeftInset(imgId)*fontHeight);
00095         insets.right = Math.round(ImageLoader.getRightInset(imgId)*fontHeight);
00096         
00097         setHeight((int) Math.round(fontHeight*heightFactor + insets.top + insets.bottom));
00098         
00099         float baseline = ImageLoader.getBaseline(imgId);
00100         int ascent;
00101         if (baseline == -1) {
00102             // C'est la baseline de la police courante.
00103             // Par contre, dans le cas d'un éventuel facteur de taille, l'ascent s'en trouve aussi
00104             // modifié
00105             ascent = (int) Math.round(fm.getAscent()*heightFactor);
00106         }
00107         else
00108             ascent = (int) Math.round(fontHeight*heightFactor*baseline);
00109             
00110         setAscent(ascent + insets.top);
00111         setDescent(getHeight() - ascent);
00112     }
00113 
00118     public void paint(Graphics g) {
00119         g.drawImage(symbol, insets.left, insets.top, observer);
00120     }
00121     
00125     public Dimension getPreferredSize() {
00126               MediaTracker tracker = new MediaTracker((Component) observer);
00127         // Le -1 signifie que la largeur sera proportionnelle à la hauteur.
00128         symbol = symbolInit.getScaledInstance(-1, getHeight()- insets.top - insets.bottom, Image.SCALE_SMOOTH);
00129         tracker.addImage(symbol, 0);
00130         // On attend que toutes les images soient chargées.
00131               try {
00132                   tracker.waitForAll();
00133               }
00134               catch (InterruptedException e) {
00135                   System.out.print(e.toString());
00136               }
00137                       
00138         tracker.removeImage(symbol);
00139         return new Dimension(symbol.getWidth(observer) + insets.left + insets.right, getHeight());
00140     }
00141     
00145     public Image getSymbol() {
00146         return symbol;
00147     }
00148 }