Save This Page
Home » openjdk-7 » net.claribole.zvtm » glyphs » [javadoc | source]
    1   /*   FILE: GlyphIcon.java
    2    *   DATE OF CREATION:   Thu Oct 17 16:17:13 2002
    3    *   AUTHOR :            Emmanuel Pietriga (emmanuel@w3.org)
    4    *   MODIF:              Fri Aug 01 13:02:13 2003 by Emmanuel Pietriga (emmanuel@w3.org, emmanuel@claribole.net)
    5    *   Copyright (c) Emmanuel Pietriga, 2002. All Rights Reserved
    6    *   Licensed under the GNU LGPL. For full terms see the file COPYING.
    7    */
    8    
    9   package net.claribole.zvtm.glyphs;
   10   
   11   import java.awt;
   12   import javax.swing;
   13   import com.xerox.VTM.glyphs;
   14   
   15     /**
   16      * Icon representing a Glyph (can be used to represent a Glyph in any Java/Swing component including non-ZVTM components)
   17      * @author Emmanuel Pietriga
   18      */
   19   
   20   public abstract class GlyphIcon {
   21   
   22       int width;
   23       int height;
   24       int cWidth;   //half component width
   25       int cHeight;  //half component height
   26   
   27       /**Use this method to get a GlyphIcon (you shoud not use a Ic* constructor directly nor GlyphIcon()) ; all objects returned by this method implement javax.swing.Icon
   28        *@param g glyph to be represented
   29        *@param w icon width (should be greater than 0)
   30        *@param h icon height (should be greater than 0)
   31        */
   32       public static javax.swing.Icon getGlyphIcon(Glyph g,int w,int h){
   33   	if (g instanceof VShape){
   34   	    return new IcShape((VShape)g,w,h);
   35   	}
   36   	else if (g instanceof VRectangle){
   37   	    return new IcRectangle((VRectangle)g,w,h);
   38   	}
   39   	else if (g instanceof VCircle){
   40   	    return new IcCircle((VCircle)g,w,h);
   41   	}
   42   	else if (g instanceof VEllipse){
   43   	    return new IcEllipse((VEllipse)g,w,h);
   44   	}
   45   	else if (g instanceof VTriangle){
   46   	    return new IcTriangle((VTriangle)g,w,h);
   47   	}
   48   	else if (g instanceof VDiamond){
   49   	    return new IcDiamond((VDiamond)g,w,h);
   50   	}
   51   	else if (g instanceof VOctagon){
   52   	    return new IcOctagon((VOctagon)g,w,h);
   53   	}
   54   	else if (g instanceof VRoundRect){
   55   	    return new IcRoundRect((VRoundRect)g,w,h);
   56   	}
   57   	else {
   58   	    return null;
   59   	}
   60       }
   61   
   62       /**set the glyph that the icon should be representing
   63        *@param g glyph to be represented (warning: the glyph type cannot be changed, only its attributes ; this means that if the Icon was instantiated as a VShape, only a VShape can be provided as parameter, etc...)
   64        */
   65       public abstract void setGlyph(Glyph g);
   66   
   67       /**get the glyph that the icon is representing
   68        */
   69       public abstract Glyph getGlyph();
   70   
   71       /** set the icon's width and height
   72        *@param w width (must be greater than 0)
   73        *@param h height (must be greater than 0)
   74        */
   75       public void setIconWidthHeight(int w,int h){
   76   	if (w>0){width=w;}
   77   	if (h>0){height=h;}
   78       }
   79       
   80   }
   81   
   82   class IcShape extends GlyphIcon implements Icon {
   83   
   84       VShape glyph;
   85       int trS;
   86   
   87       Polygon p;
   88       int[] xcoords;
   89       int[] ycoords;
   90       float vertexAngle;
   91       float[] vertices;
   92       
   93       IcShape(VShape g,int w,int h){
   94   	this.glyph=g;
   95   	this.width=w;
   96   	this.height=h;
   97       }
   98   
   99       /**set the glyph that the icon should be representing
  100        *@param g glyph to be represented  (should be a VShape (or subclass))
  101        */
  102       public void setGlyph(Glyph g){
  103   	glyph=(VShape)g;
  104       }
  105   
  106       /**get the glyph that the icon is representing
  107        */
  108       public Glyph getGlyph(){return glyph;}
  109   
  110       /**
  111        *get the icon's width (Icon interface)
  112        */
  113       public int getIconHeight(){return height;}
  114   
  115       /**
  116        *get the icon's height (Icon interface)
  117        */
  118       public int getIconWidth(){return width;}
  119   
  120       /**
  121        *Icon interface
  122        */
  123       public void paintIcon(Component c,Graphics g,int x,int y){
  124   	cWidth=c.getWidth()/2;
  125   	cHeight=c.getHeight()/2;
  126   	computePolygon();
  127   	if (glyph.getFillStatus()){
  128   	    g.setColor(glyph.getColor());
  129   	    g.fillPolygon(p);
  130   	}
  131   	g.setColor(glyph.getColorb());
  132   	g.drawPolygon(p);
  133       }
  134   
  135       protected void computePolygon(){
  136   	trS=Math.min(width,height)/2-2;
  137   	vertexAngle=glyph.getOrient();
  138   	vertices=glyph.getVertices();
  139   	xcoords=new int[vertices.length];
  140   	ycoords=new int[vertices.length];
  141   	for (int j=0;j<vertices.length-1;j++){
  142   	    xcoords[j]=(int)Math.round(cWidth+trS*Math.cos(vertexAngle)*vertices[j]);
  143   	    ycoords[j]=(int)Math.round(cHeight-trS*Math.sin(vertexAngle)*vertices[j]);
  144   	    vertexAngle+=2*Math.PI/vertices.length;
  145   	}//last iteration outside to loop to avoid one vertexAngle computation too many
  146   	xcoords[vertices.length-1]=(int)Math.round(cWidth+trS*Math.cos(vertexAngle)*vertices[vertices.length-1]);
  147   	ycoords[vertices.length-1]=(int)Math.round(cHeight-trS*Math.sin(vertexAngle)*vertices[vertices.length-1]);
  148   	p=new Polygon(xcoords,ycoords,vertices.length);
  149       }
  150       
  151   }
  152   
  153   class IcRectangle extends GlyphIcon implements Icon {
  154   
  155       VRectangle glyph;
  156       double rW,rH;  //rectangle half width and height
  157       int trW,trH;
  158       double factor;  //projection factor
  159       
  160       IcRectangle(VRectangle g,int w,int h){
  161   	this.glyph=g;
  162   	this.width=w;
  163   	this.height=h;
  164       }
  165   
  166       /**set the glyph that the icon should be representing
  167        *@param g glyph to be represented  (should be a VRectangle (or subclass))
  168        */
  169       public void setGlyph(Glyph g){
  170   	glyph=(VRectangle)g;
  171       }
  172   
  173       /**get the glyph that the icon is representing
  174        */
  175       public Glyph getGlyph(){return glyph;}
  176   
  177       /**
  178        *get the icon's width (Icon interface)
  179        */
  180       public int getIconHeight(){return height;}
  181   
  182       /**
  183        *get the icon's height (Icon interface)
  184        */
  185       public int getIconWidth(){return width;}
  186   
  187       /**
  188        *Icon interface
  189        */
  190       public void paintIcon(Component c,Graphics g,int x,int y){
  191   	cWidth=c.getWidth()/2;
  192   	cHeight=c.getHeight()/2;
  193   	rW=glyph.getWidth();
  194   	rH=glyph.getHeight();
  195   	factor=Math.max(rW/(double)width,rH/(double)height);
  196   	trW=(int)Math.round(rW/(factor*2))-2;  //-2 so that it leaves a 1 pixel border blank
  197   	trH=(int)Math.round(rH/(factor*2))-2;  //around it and the component's border
  198   	if (glyph.getFillStatus()){
  199   	    g.setColor(glyph.getColor());
  200   	    g.fillRect(cWidth-trW,cHeight-trH,2*trW,2*trH);
  201   	}
  202   	g.setColor(glyph.getColorb());
  203   	g.drawRect(cWidth-trW,cHeight-trH,2*trW,2*trH);
  204       }
  205       
  206   }
  207   
  208   class IcRoundRect extends GlyphIcon implements Icon {
  209   
  210       VRoundRect glyph;
  211       double rW,rH;  //rectangle half width and height
  212       int trW,trH;
  213       double factor;  //projection factor
  214       
  215       IcRoundRect(VRoundRect g,int w,int h){
  216   	this.glyph=g;
  217   	this.width=w;
  218   	this.height=h;
  219       }
  220   
  221       /**set the glyph that the icon should be representing
  222        *@param g glyph to be represented  (should be a VRectangle (or subclass))
  223        */
  224       public void setGlyph(Glyph g){
  225   	glyph=(VRoundRect)g;
  226       }
  227   
  228       /**get the glyph that the icon is representing
  229        */
  230       public Glyph getGlyph(){return glyph;}
  231   
  232       /**
  233        *get the icon's width (Icon interface)
  234        */
  235       public int getIconHeight(){return height;}
  236   
  237       /**
  238        *get the icon's height (Icon interface)
  239        */
  240       public int getIconWidth(){return width;}
  241   
  242       /**
  243        *Icon interface
  244        */
  245       public void paintIcon(Component c,Graphics g,int x,int y){
  246   	cWidth=c.getWidth()/2;
  247   	cHeight=c.getHeight()/2;
  248   	rW=glyph.getWidth();
  249   	rH=glyph.getHeight();
  250   	factor=Math.max(rW/(double)width,rH/(double)height);
  251   	trW=(int)Math.round(rW/(factor*2))-2;  //-2 so that it leaves a 1 pixel border blank
  252   	trH=(int)Math.round(rH/(factor*2))-2;  //around it and the component's border
  253   	if (glyph.getFillStatus()){
  254   	    g.setColor(glyph.getColor());
  255   	    g.fillRoundRect(cWidth-trW,cHeight-trH,2*trW,2*trH,trW,trH);
  256   	}
  257   	g.setColor(glyph.getColorb());
  258   	g.drawRoundRect(cWidth-trW,cHeight-trH,2*trW,2*trH,trW,trH);
  259       }
  260       
  261   }
  262   
  263   class IcCircle extends GlyphIcon implements Icon {
  264   
  265       VCircle glyph;
  266       int trS;
  267       
  268       IcCircle(VCircle g,int w,int h){
  269   	this.glyph=g;
  270   	this.width=w;
  271   	this.height=h;
  272       }
  273   
  274       /**set the glyph that the icon should be representing
  275        *@param g glyph to be represented  (should be a VCircle (or subclass))
  276        */
  277       public void setGlyph(Glyph g){
  278   	glyph=(VCircle)g;
  279       }
  280   
  281       /**get the glyph that the icon is representing
  282        */
  283       public Glyph getGlyph(){return glyph;}
  284   
  285       /**
  286        *get the icon's width (Icon interface)
  287        */
  288       public int getIconHeight(){return height;}
  289   
  290       /**
  291        *get the icon's height (Icon interface)
  292        */
  293       public int getIconWidth(){return width;}
  294   
  295       /**
  296        *Icon interface
  297        */
  298       public void paintIcon(Component c,Graphics g,int x,int y){
  299   	cWidth=c.getWidth()/2;
  300   	cHeight=c.getHeight()/2;
  301   	trS=Math.min(width,height)/2-2;
  302   	if (glyph.getFillStatus()){
  303   	    g.setColor(glyph.getColor());
  304   	    g.fillOval(cWidth-trS,cHeight-trS,2*trS,2*trS);
  305   	}
  306   	g.setColor(glyph.getColorb());
  307   	g.drawOval(cWidth-trS,cHeight-trS,2*trS,2*trS);
  308       }
  309       
  310   }
  311   
  312   
  313   class IcEllipse extends GlyphIcon implements Icon {
  314   
  315       VEllipse glyph;
  316       double rW,rH;  //rectangle half width and height
  317       int trW,trH;
  318       double factor;  //projection factor
  319       
  320       IcEllipse(VEllipse g,int w,int h){
  321   	this.glyph=g;
  322   	this.width=w;
  323   	this.height=h;
  324       }
  325   
  326       /**set the glyph that the icon should be representing
  327        *@param g glyph to be represented  (should be a VEllipse (or subclass))
  328        */
  329       public void setGlyph(Glyph g){
  330   	glyph=(VEllipse)g;
  331       }
  332   
  333       /**get the glyph that the icon is representing
  334        */
  335       public Glyph getGlyph(){return glyph;}
  336   
  337       /**
  338        *get the icon's width (Icon interface)
  339        */
  340       public int getIconHeight(){return height;}
  341   
  342       /**
  343        *get the icon's height (Icon interface)
  344        */
  345       public int getIconWidth(){return width;}
  346   
  347       /**
  348        *Icon interface
  349        */
  350       public void paintIcon(Component c,Graphics g,int x,int y){
  351   	cWidth=c.getWidth()/2;
  352   	cHeight=c.getHeight()/2;
  353   	rW=glyph.getWidth();
  354   	rH=glyph.getHeight();
  355   	factor=Math.max(rW/(double)width,rH/(double)height);
  356   	trW=(int)Math.round(rW/(factor*2))-2;  //-2 so that it leaves a 1 pixel border blank
  357   	trH=(int)Math.round(rH/(factor*2))-2;  //around it and the component's border
  358   	if (glyph.getFillStatus()){
  359   	    g.setColor(glyph.getColor());
  360   	    g.fillOval(cWidth-trW,cHeight-trH,2*trW,2*trH);
  361   	}
  362   	g.setColor(glyph.getColorb());
  363   	g.drawOval(cWidth-trW,cHeight-trH,2*trW,2*trH);
  364       }
  365       
  366   }
  367   
  368   class IcTriangle extends GlyphIcon implements Icon {
  369   
  370       VTriangle glyph;
  371       int trS;
  372       Polygon p;
  373       int halfEdge,thirdHeight;
  374       int[] xcoords=new int[3];
  375       int[] ycoords=new int[3];
  376       float orient;
  377       
  378       IcTriangle(VTriangle g,int w,int h){
  379   	this.glyph=g;
  380   	this.width=w;
  381   	this.height=h;
  382       }
  383   
  384       /**set the glyph that the icon should be representing
  385        *@param g glyph to be represented  (should be a VTriangle (or subclass))
  386        */
  387       public void setGlyph(Glyph g){
  388   	glyph=(VTriangle)g;
  389       }
  390   
  391       /**get the glyph that the icon is representing
  392        */
  393       public Glyph getGlyph(){return glyph;}
  394   
  395       /**
  396        *get the icon's width (Icon interface)
  397        */
  398       public int getIconHeight(){return height;}
  399   
  400       /**
  401        *get the icon's height (Icon interface)
  402        */
  403       public int getIconWidth(){return width;}
  404   
  405       /**
  406        *Icon interface
  407        */
  408       public void paintIcon(Component c,Graphics g,int x,int y){
  409   	cWidth=c.getWidth()/2;
  410   	cHeight=c.getHeight()/2;
  411   	computePolygon();
  412   	if (glyph.getFillStatus()){
  413   	    g.setColor(glyph.getColor());
  414   	    g.fillPolygon(p);
  415   	}
  416   	g.setColor(glyph.getColorb());
  417   	g.drawPolygon(p);
  418       }
  419   
  420       protected void computePolygon(){
  421   	trS=Math.min(width,height)/2-2;
  422   	orient=glyph.getOrient();
  423   	halfEdge=Math.round(0.866f*trS);
  424   	thirdHeight=Math.round(0.5f*trS);
  425   	xcoords[0]=(int)Math.round(cWidth-trS*Math.sin(orient));
  426   	xcoords[1]=(int)Math.round(cWidth-halfEdge*Math.cos(orient)+thirdHeight*Math.sin(orient));
  427   	xcoords[2]=(int)Math.round(cWidth+halfEdge*Math.cos(orient)+thirdHeight*Math.sin(orient));
  428   	ycoords[0]=(int)Math.round(cHeight-trS*Math.cos(orient));
  429   	ycoords[1]=(int)Math.round(cHeight+thirdHeight*Math.cos(orient)+halfEdge*Math.sin(orient));
  430   	ycoords[2]=(int)Math.round(cHeight+thirdHeight*Math.cos(orient)-halfEdge*Math.sin(orient));
  431   	p=new Polygon(xcoords,ycoords,3);
  432       }
  433       
  434   }
  435   
  436   class IcDiamond extends GlyphIcon implements Icon {
  437   
  438       VDiamond glyph;
  439       int trS;
  440       Polygon p;
  441       int[] xcoords=new int[4];
  442       int[] ycoords=new int[4];
  443       float orient;
  444       
  445       IcDiamond(VDiamond g,int w,int h){
  446   	this.glyph=g;
  447   	this.width=w;
  448   	this.height=h;
  449       }
  450   
  451       /**set the glyph that the icon should be representing
  452        *@param g glyph to be represented  (should be a VDiamond (or subclass))
  453        */
  454       public void setGlyph(Glyph g){
  455   	glyph=(VDiamond)g;
  456       }
  457   
  458       /**get the glyph that the icon is representing
  459        */
  460       public Glyph getGlyph(){return glyph;}
  461   
  462       /**
  463        *get the icon's width (Icon interface)
  464        */
  465       public int getIconHeight(){return height;}
  466   
  467       /**
  468        *get the icon's height (Icon interface)
  469        */
  470       public int getIconWidth(){return width;}
  471   
  472       /**
  473        *Icon interface
  474        */
  475       public void paintIcon(Component c,Graphics g,int x,int y){
  476   	cWidth=c.getWidth()/2;
  477   	cHeight=c.getHeight()/2;
  478   	computePolygon();
  479   	if (glyph.getFillStatus()){
  480   	    g.setColor(glyph.getColor());
  481   	    g.fillPolygon(p);
  482   	}
  483   	g.setColor(glyph.getColorb());
  484   	g.drawPolygon(p);
  485       }
  486   
  487       protected void computePolygon(){
  488   	trS=Math.min(width,height)/2-2;
  489   	orient=glyph.getOrient();
  490   	xcoords[0]=(int)Math.round(cWidth+trS*Math.cos(orient));
  491   	xcoords[1]=(int)Math.round(cWidth-trS*Math.sin(orient));
  492   	xcoords[2]=(int)Math.round(cWidth-trS*Math.cos(orient));
  493   	xcoords[3]=(int)Math.round(cWidth+trS*Math.sin(orient));
  494   	ycoords[0]=(int)Math.round(cHeight-trS*Math.sin(orient));
  495   	ycoords[1]=(int)Math.round(cHeight-trS*Math.cos(orient));
  496   	ycoords[2]=(int)Math.round(cHeight+trS*Math.sin(orient));
  497   	ycoords[3]=(int)Math.round(cHeight+trS*Math.cos(orient));
  498   	p=new Polygon(xcoords,ycoords,4);
  499       }
  500       
  501   }
  502   
  503   class IcOctagon extends GlyphIcon implements Icon {
  504   
  505       VOctagon glyph;
  506       int trS;
  507       Polygon p;
  508       int halfS;
  509       int[] xcoords=new int[8];
  510       int[] ycoords=new int[8];
  511       float orient;
  512       
  513       IcOctagon(VOctagon g,int w,int h){
  514   	this.glyph=g;
  515   	this.width=w;
  516   	this.height=h;
  517       }
  518   
  519       /**set the glyph that the icon should be representing
  520        *@param g glyph to be represented  (should be a VOctagon (or subclass))
  521        */
  522       public void setGlyph(Glyph g){
  523   	glyph=(VOctagon)g;
  524       }
  525   
  526       /**get the glyph that the icon is representing
  527        */
  528       public Glyph getGlyph(){return glyph;}
  529   
  530       /**
  531        *get the icon's width (Icon interface)
  532        */
  533       public int getIconHeight(){return height;}
  534   
  535       /**
  536        *get the icon's height (Icon interface)
  537        */
  538       public int getIconWidth(){return width;}
  539   
  540       /**
  541        *Icon interface
  542        */
  543       public void paintIcon(Component c,Graphics g,int x,int y){
  544   	cWidth=c.getWidth()/2;
  545   	cHeight=c.getHeight()/2;
  546   	computePolygon();
  547   	if (glyph.getFillStatus()){
  548   	    g.setColor(glyph.getColor());
  549   	    g.fillPolygon(p);
  550   	}
  551   	g.setColor(glyph.getColorb());
  552   	g.drawPolygon(p);
  553       }
  554   
  555       protected void computePolygon(){
  556   	trS=Math.min(width,height)/2-2;
  557   	orient=glyph.getOrient();
  558   	halfS=trS/2;
  559   	xcoords[0]=(int)Math.round((trS*Math.cos(orient)-halfS*Math.sin(orient))+cWidth);
  560   	xcoords[1]=(int)Math.round((halfS*Math.cos(orient)-trS*Math.sin(orient))+cWidth);
  561   	xcoords[2]=(int)Math.round((-halfS*Math.cos(orient)-trS*Math.sin(orient))+cWidth);
  562   	xcoords[3]=(int)Math.round((-trS*Math.cos(orient)-halfS*Math.sin(orient))+cWidth);
  563   	xcoords[4]=(int)Math.round((-trS*Math.cos(orient)+halfS*Math.sin(orient))+cWidth);
  564   	xcoords[5]=(int)Math.round((-halfS*Math.cos(orient)+trS*Math.sin(orient))+cWidth);
  565   	xcoords[6]=(int)Math.round((halfS*Math.cos(orient)+trS*Math.sin(orient))+cWidth);
  566   	xcoords[7]=(int)Math.round((trS*Math.cos(orient)+halfS*Math.sin(orient))+cWidth);
  567   	ycoords[0]=(int)Math.round((-halfS*Math.cos(orient)-trS*Math.sin(orient))+cHeight);
  568   	ycoords[1]=(int)Math.round((-trS*Math.cos(orient)-halfS*Math.sin(orient))+cHeight);
  569   	ycoords[2]=(int)Math.round((-trS*Math.cos(orient)+halfS*Math.sin(orient))+cHeight);
  570   	ycoords[3]=(int)Math.round((-halfS*Math.cos(orient)+trS*Math.sin(orient))+cHeight);
  571   	ycoords[4]=(int)Math.round((halfS*Math.cos(orient)+trS*Math.sin(orient))+cHeight);
  572   	ycoords[5]=(int)Math.round((trS*Math.cos(orient)+halfS*Math.sin(orient))+cHeight);
  573   	ycoords[6]=(int)Math.round((trS*Math.cos(orient)-halfS*Math.sin(orient))+cHeight);
  574   	ycoords[7]=(int)Math.round((halfS*Math.cos(orient)-trS*Math.sin(orient))+cHeight);
  575   	p=new Polygon(xcoords,ycoords,8);
  576       }
  577       
  578   }

Save This Page
Home » openjdk-7 » net.claribole.zvtm » glyphs » [javadoc | source]