Back to index

wims  3.65+svn20090927
MyGraphicsSvg.java
Go to the documentation of this file.
00001 package rene.zirkel.graphics;
00002 
00003 import java.awt.*;
00004 import java.awt.image.*;
00005 import java.io.*;
00006 
00007 import rene.util.xml.*;
00008 import rene.zirkel.ZirkelFrame;
00009 import rene.zirkel.objects.*;
00010 import rene.gui.Global;
00011 
00012 class SvgFontMetrics extends FontMetrics
00013 {      public SvgFontMetrics ()
00014        {      super(new Font("Courier",10,Font.PLAIN)); // a dummy font.
00015        }
00016        
00017        public int stringWidth (String s)
00018        {      return s.length()*10; 
00019        }
00020        
00021        public int getHeight()
00022        {      return 12; 
00023        }
00024        
00025        public int getAscent()
00026        {      return 2; 
00027        }
00028        
00029 }
00030 
00035 public class MyGraphicsSvg extends MyGraphics
00036 {      int W,H;
00037 
00038        PrintWriter Out; 
00039        final static int STROKE_NORMAL=0,STROKE_THIN=1,STROKE_THICK=2; 
00040        final static int STYLE_NORMAL=0,STYLE_THIN=1,STYLE_THICK=0; 
00041        int Stroke=STROKE_NORMAL; 
00042        Color col,fillcol; 
00043        SvgFontMetrics MFM=new SvgFontMetrics(); 
00044        int yoffset=6000;
00045        int FontH=12; 
00046        
00047        SVGWriter svg;
00048        
00049        public MyGraphicsSvg (PrintWriter out, int w, int h)
00050        {      svg=new SVGWriter(out);
00051               svg.startSVG(w,h);
00052               W=w; H=h; 
00053        }
00054        
00055        public void close ()
00056        {      svg.endTagNewLine("svg");
00057        }
00058        
00059        public void setColor (Color c)
00060        {      col=c;
00061        }
00062        
00063        public void setFillcolor (Color c)
00064        {      fillcol=c;
00065        }
00066 
00067        public void setColor (ConstructionObject o)
00068        {
00069               if (o.isJobTarget()) setColor(ZirkelFrame.TargetColor); 
00070               else if (o.selected()) setColor(ZirkelFrame.SelectColor); 
00071               else
00072               {      if (o.getColorType()==ConstructionObject.THIN)
00073                      {      int i=o.getColorIndex(); 
00074                             if (o.isHidden()) setColor(ZirkelFrame.BrighterLightColors[i]); 
00075                             else setColor(ZirkelFrame.LightColors[i]); 
00076                      }
00077                      else
00078                      {      int i=o.getColorIndex(); 
00079                             if (o.isHidden()) setColor(ZirkelFrame.BrighterColors[i]); 
00080                             else setColor(ZirkelFrame.Colors[i]); 
00081                      }
00082                      if (o.getColorType()==ConstructionObject.THIN)
00083                      {
00084                             Stroke=STROKE_THIN; 
00085                      }
00086                      else if (o.getColorType()==ConstructionObject.THICK)
00087                      {
00088                             Stroke=STROKE_THICK; 
00089                      }
00090                      else
00091                      {
00092                             Stroke=STROKE_NORMAL; 
00093                      }
00094               }
00095        }
00096        
00097        public void setFillColor (ConstructionObject o)
00098        {      if (o.isJobTarget()) setFillcolor(ZirkelFrame.TargetColor); 
00099               else
00100               {      if (o.getColorType()!=ConstructionObject.THICK)
00101                      {      int i=o.getColorIndex(); 
00102                             if (o.isHidden()) setFillcolor(ZirkelFrame.BrighterLightColors[i]); 
00103                             else setFillcolor(ZirkelFrame.LightColors[i]); 
00104                      }
00105                      else
00106                      {      int i=o.getColorIndex(); 
00107                             if (o.isHidden()) setFillcolor(ZirkelFrame.BrighterColors[i]); 
00108                             else setFillcolor(ZirkelFrame.Colors[i]); 
00109                      }
00110               }
00111        }
00112        
00113        public void setLabelColor (ConstructionObject o)
00114        {      if (o.labelSelected()) setColor(ZirkelFrame.SelectColor);
00115               else if (o.isFilled())
00116               {      int type=o.getColorType();
00117                      o.setColorType(ConstructionObject.NORMAL);
00118                      setColor(o);
00119                      o.setColorType(type);
00120               }
00121               else setColor(o);
00122        }
00123 
00124        public void clearRect (int x, int y, int w, int h, Color c) {}
00125        
00126        public void drawRect (double x, double y, double w, double h)
00127        {      svg.startTagStart("path");
00128               w--; h--;
00129               svg.printArg("d","M "+x+" "+y+" H "+(x+w)+
00130                      " V "+(y+h)+" H "+x+" Z");
00131               svg.printArg("style","fill:none;stroke:"+rgb()+";stroke-width:1");
00132               svg.finishTagNewLine();     
00133        }
00134        
00135        public String rgb ()
00136        {      return "rgb("+col.getRed()+","+col.getGreen()+","+col.getBlue()+")";
00137        }
00138        
00139        public String frgb ()
00140        {      return "rgb("+fillcol.getRed()+","+fillcol.getGreen()+","+fillcol.getBlue()+")";
00141        }
00142        
00143        public void drawLine (double x, double y, double x1, double y1)
00144        {      svg.startTagStart("line");
00145               svg.printArg("x1",""+x);
00146               svg.printArg("y1",""+y);
00147               svg.printArg("x2",""+x1);
00148               svg.printArg("y2",""+y1);
00149               switch (Stroke)             
00150               {      case STROKE_NORMAL :
00151                             svg.printArg("style","stroke:"+rgb()+";stroke-width:1");
00152                             break;
00153                      case STROKE_THICK :
00154                             svg.printArg("style","stroke:"+rgb()+";stroke-width:3");
00155                             break;
00156                      case STROKE_THIN :
00157                             svg.printArg("style","stroke:"+rgb()+";stroke-width:1;fill:none;stroke-dasharray:5,5");
00158                             break;
00159               }
00160               svg.finishTagNewLine();
00161        }
00162        
00163        public void drawThickLine (double x, double y, double x1, double y1)
00164        {      svg.startTagStart("line");
00165               svg.printArg("x1",""+x);
00166               svg.printArg("y1",""+y);
00167               svg.printArg("x2",""+x1);
00168               svg.printArg("y2",""+y1);
00169               svg.printArg("style","stroke:"+rgb()+";stroke-width:3");
00170               svg.finishTagNewLine();
00171        }
00172        
00173        public void drawLine (double x, double y, double x1, double y1, ConstructionObject o)
00174        {      svg.startTagStart("line");
00175               svg.printArg("x1",""+x);
00176               svg.printArg("y1",""+y);
00177               svg.printArg("x2",""+x1);
00178               svg.printArg("y2",""+y1);
00179               svg.printArg("style","stroke:"+rgb()+";stroke-width:1");
00180               svg.finishTagNewLine();
00181        }
00182        
00183        public void drawArc (double x, double y, double w, double h, double a, double b)
00184        {      double x0=Math.round(x+w/2.0+w*Math.cos(a/180*Math.PI)/2);
00185               double y0=Math.round(y+h/2.0-h*Math.sin(a/180*Math.PI)/2);
00186               double x1=Math.round(x+w/2.0+w*Math.cos((a+b)/180*Math.PI)/2);
00187               double y1=Math.round(y+h/2.0-h*Math.sin((a+b)/180*Math.PI)/2);
00188               int f=0;
00189               if (b>180) f=1;
00190               svg.startTagStart("path");
00191               svg.printArg("d","M "+x0+" "+y0+" A "+(w/2)+" "+(h/2)+" 0 "+f+" 0 "+x1+" "+y1);
00192               svg.printArg("style","stroke:"+rgb()+";stroke-width:1;fill:none");
00193               svg.finishTagNewLine();
00194        }
00195        
00196        public void drawArc (double x, double y, double w, double h, double a, double b,
00197               ConstructionObject o)
00198        {      w=w/2; h=h/2; x+=w; y+=h;
00199               double x0=Math.round(x+w*Math.cos(a/180*Math.PI));
00200               double y0=Math.round(y-h*Math.sin(a/180*Math.PI));
00201               double x1=Math.round(x+w*Math.cos((a+b)/180*Math.PI));
00202               double y1=Math.round(y-h*Math.sin((a+b)/180*Math.PI));
00203               int f=0;
00204               if (b>180) f=1;
00205               svg.startTagStart("path");
00206               svg.printArg("d","M "+x0+" "+y0+" A "+w+" "+h+" 0 "+f+" 0 "+x1+" "+y1);
00207               switch (Stroke)             
00208               {      case STROKE_NORMAL :
00209                             svg.printArg("style","stroke:"+rgb()+";stroke-width:1;fill:none");
00210                             break;
00211                      case STROKE_THICK :
00212                             svg.printArg("style","stroke:"+rgb()+";stroke-width:3;fill:none");
00213                             break;
00214                      case STROKE_THIN :
00215                             svg.printArg("style","stroke:"+rgb()+";stroke-width:1;fill:none;stroke-dasharray:5,5");
00216                             break;
00217               }
00218               svg.finishTagNewLine();
00219        }
00220        
00221        public FontMetrics getFontMetrics ()
00222        {      return MFM; 
00223        }
00224        
00225        public void drawString (String s, double x, double y)
00226        {      svg.startTagStart("text");
00227               svg.printArg("x",""+x);
00228               svg.printArg("y",""+(y+10));
00229               svg.printArg("style","font-size:"+FontH+";fill:"+rgb()+";font-weight:"+
00230                      (Global.getParameter("font.bold",false)?"gold":"normal"));
00231               svg.startTagEnd();
00232               svg.print(s);
00233               svg.endTag("text");
00234        }
00235        
00236        public void drawOval (double x, double y, double w, double h)
00237        {      svg.startTagStart("ellipse");
00238               w=w/2; h=h/2; x+=w; y+=h;
00239               svg.printArg("cx",""+x);
00240               svg.printArg("cy",""+y);
00241               svg.printArg("rx",""+w);
00242               svg.printArg("ry",""+h);
00243               svg.printArg("style","stroke:"+rgb()+";stroke-width:1;fill:none");
00244               svg.finishTagNewLine();
00245        }
00246        
00247        public void drawOval (double x, double y, double w, double h,
00248               ConstructionObject o)
00249        {      svg.startTagStart("ellipse");
00250               w=w/2; h=h/2; x+=w; y+=h;
00251               svg.printArg("cx",""+x);
00252               svg.printArg("cy",""+y);
00253               svg.printArg("rx",""+w);
00254               svg.printArg("ry",""+h);
00255               switch (Stroke)             
00256               {      case STROKE_NORMAL :
00257                             svg.printArg("style","stroke:"+rgb()+";fill:none;stroke-width:1");
00258                             break;
00259                      case STROKE_THICK :
00260                             svg.printArg("style","stroke:"+rgb()+";fill:none;stroke-width:3");
00261                             break;
00262                      case STROKE_THIN :
00263                             svg.printArg("style","stroke:"+rgb()+";stroke-width:1;fill:none;stroke-dasharray:5,5");
00264                             break;
00265               }
00266               svg.finishTagNewLine();
00267        }
00268               
00269        public void fillRect (double x, double y, double w, double h,
00270               boolean outline, boolean transparent, ConstructionObject o)
00271        {      svg.startTagStart("path");
00272               w--; h--;
00273               svg.printArg("d","M "+x+" "+y+" H "+(x+w)+
00274                      " V "+(y+h)+" H "+x+" Z");
00275               svg.printArg("style","fill:"+rgb()+";stroke:"+rgb()+";stroke-width:1");
00276               svg.finishTagNewLine();     
00277        }
00278        
00279        public void fillArc (double x, double y, double w, double h, double a, double b,
00280               boolean outline, boolean transparent, boolean arc, ConstructionObject o)
00281        {      setFillColor(o);
00282               int x0=(int)(Math.round(x+w/2.0+w*Math.cos(a/180*Math.PI)/2));
00283               int y0=(int)(Math.round(y+h/2.0-h*Math.sin(a/180*Math.PI)/2));
00284               int x1=(int)(Math.round(x+w/2.0+w*Math.cos((a+b)/180*Math.PI)/2));
00285               int y1=(int)(Math.round(y+h/2.0-h*Math.sin((a+b)/180*Math.PI)/2));
00286               int f=0;
00287               if (b>180) f=1;
00288               svg.startTagStart("path");
00289               if (arc)
00290                      svg.printArg("d","M "+(x+w/2)+" "+(y+h/2)+
00291                             " L "+x0+" "+y0+
00292                             " A "+(w/2)+" "+(h/2)+" 0 "+f+" 0 "+x1+" "+y1+
00293                             " L "+(x+w/2)+" "+(y+h/2));
00294               else
00295                      svg.printArg("d",
00296                             "M "+x0+" "+y0+
00297                             " A "+(w/2)+" "+(h/2)+" 0 "+f+" 0 "+x1+" "+y1+
00298                             " L "+x0+" "+y0);                  
00299               switch (Stroke)             
00300               {      case STROKE_NORMAL :
00301                             svg.printArg("style","fill:"+frgb()+
00302                                    (transparent && !o.isSolid()?";fill-opacity:0.5":""));
00303                             break;
00304                      case STROKE_THICK :
00305                             svg.printArg("style","fill:"+frgb());
00306                             break;
00307                      case STROKE_THIN :
00308                             svg.printArg("style","fill:"+frgb()+
00309                                    (transparent && !o.isSolid()?";fill-opacity:0.5":""));
00310                             break;
00311               }
00312               svg.finishTagNewLine();
00313               if (outline)
00314               {      setColor(o);
00315                      drawArc(x,y,w,h,a,b);
00316               }
00317        }
00318  
00319        public void fillOval (double x, double y, double w, double h,
00320               boolean outline, boolean transparent, ConstructionObject o)
00321        {      setFillColor(o);
00322               svg.startTagStart("ellipse");
00323               svg.printArg("cx",""+(x+w/2));
00324               svg.printArg("cy",""+(y+h/2));
00325               svg.printArg("rx",""+(w/2));
00326               svg.printArg("ry",""+(h/2));
00327               switch (Stroke)             
00328               {      case STROKE_NORMAL :
00329                             svg.printArg("style","fill:"+frgb()+
00330                                    (transparent && !o.isSolid()?";fill-opacity:0.5":""));
00331                             break;
00332                      case STROKE_THICK :
00333                             svg.printArg("style","fill:"+frgb());
00334                             break;
00335                      case STROKE_THIN :
00336                             svg.printArg("style","fill:"+frgb()+
00337                                    (transparent && !o.isSolid()?";fill-opacity:0.5":""));
00338                             break;
00339               }
00340               svg.finishTagNewLine();
00341               if (outline)
00342               {      setColor(o);
00343                      drawOval(x,y,w,h);
00344               }
00345        }
00346        
00347        public void fillPolygon (double x[], double y[], int n,
00348               boolean outline, boolean transparent, ConstructionObject o)
00349        {      setFillColor(o);
00350               svg.startTagStart("path");
00351               String s="M "+x[0]+" "+y[0];
00352               for (int i=1; i<n; i++)
00353                      s=s+" L "+x[i]+" "+y[i];
00354               s=s+" L "+x[0]+" "+y[0];
00355               svg.printArg("d",s);
00356               switch (Stroke)             
00357               {      case STROKE_NORMAL :
00358                             svg.printArg("style","fill:"+frgb()+
00359                                    (transparent && !o.isSolid()?";fill-opacity:0.5":""));
00360                             break;
00361                      case STROKE_THICK :
00362                             svg.printArg("style","fill:"+frgb());
00363                             break;
00364                      case STROKE_THIN :
00365                             svg.printArg("style","fill:"+frgb()+
00366                                    (transparent && !o.isSolid()?";fill-opacity:0.5":""));
00367                             break;
00368               }
00369               svg.finishTagNewLine();
00370               if (outline)
00371               {      svg.startTagStart("path");
00372                      svg.printArg("d",s);
00373                      setColor(o);
00374                      svg.printArg("style","fill:none;stroke:"+rgb()+";stroke-width:1");
00375                      svg.finishTagNewLine();
00376               }
00377        }
00378        
00379        public void drawImage (Image i, int x, int y, ImageObserver o)
00380        {
00381        }
00382        public void drawImage (Image i, int x, int y, int w, int h, ImageObserver o)
00383        {
00384        }
00385 
00386        public void setFont (int h, boolean bold)
00387        {      FontH=h;
00388        }
00389 
00390        int fsize;
00391        boolean flarge,fbold;
00392        int ffactor=Global.getParameter("ffactor",130);
00393        
00394        public void setDefaultFont (int h, boolean large, boolean bold)
00395        {      fsize=h; flarge=large; fbold=bold;
00396               ffactor=Global.getParameter("ffactor",130);
00397               setFont(large,bold);
00398        }
00399        
00400        public void setFont (boolean large, boolean bold)
00401        {      int size=fsize;
00402               if (large) size=size*ffactor/100;
00403               if (flarge) size=size*ffactor/100;
00404               setFont(size,bold || fbold);
00405        }
00406 
00407        public void drawImage (Image i, double x, double y, double x1, double y1, 
00408                      double x2, double y2, ImageObserver o)
00409        {
00410        }
00411 
00412        public Graphics getGraphics() {
00413               return null;
00414        }
00415 
00416        public int stringWidth(String s) 
00417        {      return getFontMetrics().stringWidth(s);
00418        }
00419 
00420        public int stringHeight(String s) 
00421        {      return getFontMetrics().getHeight();
00422        }
00423 
00424        public int drawStringExtended(String s, double x, double y) 
00425        {      drawString(s,x,y+getFontMetrics().getAscent());
00426               return getFontMetrics().getHeight(); 
00427        }
00428 
00429        public int stringAscent(String s) 
00430        {      return getFontMetrics().getAscent();
00431        }
00432 
00433 }