Back to index

wims  3.65+svn20090927
MyGraphics13.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.awt.geom.*;
00006 
00007 import atp.sHotEqn;
00008 
00009 import rene.gui.*;
00010 import rene.zirkel.ZirkelFrame;
00011 import rene.zirkel.objects.*;
00012 
00013 public class MyGraphics13 extends MyGraphics
00014 {      Graphics2D G;
00015        BasicStroke Thin,Normal,Thick,SuperThick;
00016        AlphaComposite C,CO;
00017        Component ZC=null;
00018        LatexOutput LOut;
00019        
00020        public MyGraphics13 (Graphics g, double factor, Component zc, LatexOutput lout)
00021        {      LOut=lout;
00022               G=(Graphics2D)g;
00023               if (Global.getParameter("quality",true))
00024               {      G.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
00025                             RenderingHints.VALUE_ANTIALIAS_ON);
00026                      G.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
00027                             RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
00028                      G.setRenderingHint(RenderingHints.KEY_RENDERING,
00029                             RenderingHints.VALUE_RENDER_QUALITY);
00030                      G.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
00031                             RenderingHints.VALUE_INTERPOLATION_BICUBIC);
00032               }
00033               float dash[]={(float)(factor*15.0),(float)(factor*15.0)};
00034               if (factor<Global.getParameter("minlinesize",1.0)) 
00035                      factor=Global.getParameter("minlinesize",1.0);
00036               Thin=new BasicStroke((float)(factor*2.0),BasicStroke.CAP_BUTT,
00037                      BasicStroke.JOIN_MITER,10.0f,dash,0.0f);
00038               Normal=new BasicStroke((float)(factor));
00039               Thick=new BasicStroke((float)(factor*4.0),BasicStroke.CAP_ROUND,
00040                             BasicStroke.JOIN_MITER);
00041               SuperThick=new BasicStroke((float)(20),BasicStroke.CAP_ROUND,
00042                             BasicStroke.JOIN_ROUND);
00043               C=AlphaComposite.getInstance(AlphaComposite.SRC_OVER,(float)0.5);
00044               CO=AlphaComposite.getInstance(AlphaComposite.SRC_OVER,(float)1.0);
00045               G.setComposite(CO);
00046               ZC=zc;
00047        }
00048        
00049        public MyGraphics13 (Graphics g)
00050        {      this(g,0.5,null,null);
00051        }
00052 
00053        public MyGraphics13 (Graphics g, Component zc)
00054        {      this(g,0.5,zc,null);
00055        }
00056 
00057        public MyGraphics13 (Graphics g, double factor)
00058        {      this(g,factor,null,null);
00059        }
00060 
00061        public void setColor (Color c)
00062        {      G.setColor(c);
00063        }
00064 
00065        public void setColor (ConstructionObject o)
00066        {      G.setStroke(Normal);
00067               if (o.isJobTarget()) setColor(ZirkelFrame.TargetColor);
00068               else if (o.indicated()) setColor(ZirkelFrame.IndicateColor);
00069               else if (o.selected()) setColor(ZirkelFrame.SelectColor);
00070               else
00071               {      if (o.getColorType()==ConstructionObject.THIN)
00072                      {      int i=o.getColorIndex();
00073                             if (o.isHidden()) setColor(ZirkelFrame.BrighterLightColors[i]);
00074                             else setColor(ZirkelFrame.LightColors[i]);                     
00075                      }             
00076                      else
00077                      {      int i=o.getColorIndex();
00078                             if (o.isHidden()) setColor(ZirkelFrame.BrighterColors[i]);
00079                             else setColor(ZirkelFrame.Colors[i]);                   
00080                      }
00081               }
00082               if (o.getColorType()==ConstructionObject.THIN)
00083               {      G.setStroke(Thin);
00084               }
00085               else if (o.getColorType()==ConstructionObject.THICK)
00086               {      G.setStroke(Thick);
00087               }
00088               else
00089               {      G.setStroke(Normal);
00090               }
00091        }
00092 
00093        public void clearRect (int x, int y, int w, int h, Color c)
00094        {      G.setColor(c);
00095               G.fillRect(x,y,w,h);
00096        }
00097 
00098        public void setFillColor (ConstructionObject o)
00099        {      G.setStroke(Normal);
00100               if (o.isJobTarget()) setColor(ZirkelFrame.TargetColor);
00101               else if ((o instanceof PointObject) && o.indicated()) setColor(ZirkelFrame.IndicateColor);
00102               else
00103               {      if (o.getColorType()!=ConstructionObject.THICK)
00104                      {      int i=o.getColorIndex();
00105                             if (o.isHidden()) setColor(ZirkelFrame.BrighterLightColors[i]);
00106                             else setColor(ZirkelFrame.LightColors[i]);                     
00107                      }             
00108                      else
00109                      {      int i=o.getColorIndex();
00110                             if (o.isHidden()) setColor(ZirkelFrame.BrighterColors[i]);
00111                             else setColor(ZirkelFrame.Colors[i]);                   
00112                      }
00113                      if (o.getColorType()==ConstructionObject.THIN)
00114                      {      G.setStroke(Thin);
00115                      }
00116                      else if (o.getColorType()==ConstructionObject.THICK)
00117                      {      G.setStroke(Thick);
00118                      }
00119                      else
00120                      {      G.setStroke(Normal);
00121                      }
00122               }
00123        }
00124 
00125        public void setLabelColor (ConstructionObject o)
00126        {      if (o.labelSelected()) setColor(ZirkelFrame.SelectColor);
00127               else if (o.isFilled())
00128               {      int type=o.getColorType();
00129                      o.setColorType(ConstructionObject.NORMAL);
00130                      setColor(o);
00131                      o.setColorType(type);
00132               }
00133               else setColor(o);
00134        }
00135 
00136        public void drawRect (double x, double y, double w, double h)
00137        {      if (test(x) || test(y) || test(x+w) || test(y+h)) return;
00138               G.setStroke(Normal);
00139               G.draw(new Rectangle2D.Double(x,y,w,h));
00140        }
00141 
00142        public void drawMarkerRect (double x, double y, double w, double h)
00143        {      if (test(x) || test(y) || test(x+w) || test(y+h)) return;
00144               G.setColor(ZirkelFrame.IndicateColor);
00145               G.setStroke(SuperThick);
00146               G.draw(new Rectangle2D.Double(x,y,w,h));
00147               G.setStroke(Normal);
00148        }
00149 
00150        public void drawLine (double x, double y, double x1, double y1)
00151        {      if (test(x) || test(y) || test(x1) || test(y1)) return;
00152               G.setStroke(Normal);
00153               G.draw(new Line2D.Double(x,y,x1,y1));
00154        }
00155 
00156        public void drawThickLine (double x, double y, double x1, double y1)
00157        {      if (test(x) || test(y) || test(x1) || test(y1)) return;
00158               G.setStroke(Thick);
00159               G.draw(new Line2D.Double(x,y,x1,y1));
00160               G.setStroke(Normal);
00161        }
00162 
00163        public void drawMarkerLine (double x, double y, double x1, double y1)
00164        {      if (test(x) || test(y) || test(x1) || test(y1)) return;
00165               G.setColor(ZirkelFrame.IndicateColor);
00166               G.setStroke(SuperThick);
00167               G.draw(new Line2D.Double(x,y,x1,y1));
00168               G.setStroke(Normal);
00169        }
00170 
00171        public void drawLine (double x, double y, double x1, double y1, ConstructionObject o)
00172        {      if (test(x) || test(y) || test(x1) || test(y1)) return;
00173               G.draw(new Line2D.Double(x,y,x1,y1));
00174        }
00175        
00176        public boolean test (double x)
00177        {      return Math.abs(x)>1e5;
00178        }
00179 
00180        public void drawArc (double x, double y, double w, double h, double a, double b)
00181        {      if (test(x) || test(y) || test(w) || test(h)) return;
00182               G.setStroke(Normal);
00183               G.draw(new Arc2D.Double(x,y,w,h,a,b,Arc2D.OPEN));
00184        }
00185 
00186        public void drawArc (double x, double y, double w, double h, double a, double b,
00187               ConstructionObject o)
00188        {      if (test(x) || test(y) || test(w) || test(h))
00189                      return;
00190               else
00191                      G.draw(new Arc2D.Double(x,y,w,h,a,b,Arc2D.OPEN));
00192        }
00193 
00194        public FontMetrics getFontMetrics ()
00195        {      return G.getFontMetrics();
00196        }
00197 
00198        public void drawString (String s, double x, double y)
00199        {      if (test(x) || test(y)) return;
00200               G.drawString(s,(float)x,(float)y);
00201        }
00202 
00203        public void drawOval (double x, double y, double w, double h)
00204        {      if (test(x) || test(y) || test(w) || test(h)) return;
00205               G.setStroke(Normal);
00206               G.draw(new Ellipse2D.Double(x,y,w,h));           
00207        }
00208 
00209        public void drawOval (double x, double y, double w, double h,
00210               ConstructionObject o)
00211        {      if (test(x) || test(y) || test(w) || test(h))
00212                      return;
00213               else
00214                      G.draw(new Ellipse2D.Double(x,y,w,h));           
00215        }
00216        
00217        public void drawCircle (double x, double y, double r,
00218               ConstructionObject o)
00219        {      if (r>10*(W+H))
00220                      drawLargeCircleArc(x,y,r,0,360);
00221               else
00222                      G.draw(new Ellipse2D.Double(x-r,y-r,2*r,2*r));          
00223        }
00224        
00225        public void drawMarkerArc (double x, double y, double r, double a, double b)
00226        {      if (test(x) || test(y) || test(r)) return;
00227               G.setColor(ZirkelFrame.IndicateColor);
00228               G.setStroke(SuperThick);
00229               G.draw(new Arc2D.Double(x-r,y-r,2*r,2*r,a,b,Arc2D.OPEN));
00230        }
00231 
00232        public void drawCircleArc (double x, double y, double r, double a, double b,
00233               ConstructionObject o)
00234        {      if (r>10*(W+H))
00235                      drawLargeCircleArc(x,y,r,a,b);
00236               else
00237                      G.draw(new Arc2D.Double(x-r,y-r,2*r,2*r,a,b,Arc2D.OPEN));
00238        }
00239 
00240        void drawLargeCircleArc (double x, double y, double r, double a, double b)
00241        {      double dw=Math.sqrt((W+H)/r/10);
00242               double w=a;
00243               double x0=x+r*Math.cos(w/180*Math.PI);
00244               double y0=y-r*Math.sin(w/180*Math.PI);
00245               w=w+dw;              
00246               while (w<a+b+dw)
00247               {      if (w>a+b) w=a+b;
00248                      double x1=x+r*Math.cos(w/180*Math.PI);
00249                      double y1=y-r*Math.sin(w/180*Math.PI);
00250                      double dx=(x0+x1)/2,dy=(y0+y1)/2;
00251                      if (Math.sqrt(dx*dx+dy*dy)<=10*(W+H)) 
00252                             G.draw(new Line2D.Double(x0,y0,x1,y1));
00253                      x0=x1; y0=y1;
00254                      w+=dw;
00255               }
00256        }
00257 
00258        public void fillRect (double x, double y, double w, double h,
00259               boolean outline, boolean transparent, ConstructionObject o)
00260        {      if (test(x) || test(y) || test(w) || test(h)) return;
00261               setFillColor(o);
00262               if (transparent && !o.isSolid()) G.setComposite(C);
00263               G.fill(new Rectangle2D.Double(x,y,w,h));
00264               if (transparent && !o.isSolid()) G.setComposite(CO);
00265               if (outline)
00266               {      setColor(o);
00267                      G.draw(new Rectangle2D.Double(x,y,w,h));
00268               }
00269        }
00270 
00271        public void fillOval (double x, double y, double w, double h,
00272               boolean outline, boolean transparent, ConstructionObject o)
00273        {      if (test(x) || test(y) || test(w) || test(h)) return;
00274               if (o.getColorType()!=ConstructionObject.INVISIBLE)
00275               {      setFillColor(o);
00276                      if (transparent && !o.isSolid()) G.setComposite(C);
00277                      try
00278                      {      G.fill(new Ellipse2D.Double(x,y,w,h));           
00279                      }
00280                      catch (Exception e) {}
00281                      if (transparent && !o.isSolid()) G.setComposite(CO);
00282               }
00283               if (outline)
00284               {      setColor(o);
00285                      drawOval(x,y,w,h);
00286               }
00287        }
00288 
00289        public void fillArc (double x, double y, double w, double h, double a, double b,
00290               boolean outline, boolean transparent, boolean arcb, ConstructionObject o)
00291        {      if (test(x) || test(y) || test(w) || test(h)) return;
00292               setFillColor(o);
00293               if (transparent && !o.isSolid()) G.setComposite(C);
00294               Arc2D arc=new Arc2D.Double(x,y,w,h,a,b,arcb?Arc2D.PIE:Arc2D.CHORD);
00295               G.fill(arc);
00296               if (transparent && !o.isSolid()) G.setComposite(CO);
00297               if (outline)
00298               {      setColor(o);
00299                      arc.setArcType(Arc2D.OPEN);
00300                      G.setStroke(Normal);
00301                      G.draw(arc);
00302               }
00303        }
00304 
00305        int xx[]=new int[64],yy[]=new int[64];
00306 
00307        public void fillPolygon (double x[], double y[], int n,
00308               boolean outline, boolean transparent, ConstructionObject o)
00309        {      if (o.getColorType()!=ConstructionObject.INVISIBLE)
00310               {      setFillColor(o);
00311                      if (transparent && !o.isSolid()) G.setComposite(C);
00312               }
00313               if (n>xx.length)
00314               {      xx=new int[n]; yy=new int[n];
00315               }
00316               for (int i=0; i<n; i++) 
00317               {      xx[i]=(int)(x[i]);
00318                      if (test(x[i])) return;
00319                      yy[i]=(int)(y[i]);
00320                      if (test(y[i])) return;
00321               }
00322               if (o.getColorType()!=ConstructionObject.INVISIBLE)
00323               {      G.fillPolygon(xx,yy,n);
00324                      if (transparent && !o.isSolid()) G.setComposite(CO);
00325               }
00326               if (outline)
00327               {      setColor(o);
00328                      G.setStroke(Normal);
00329                      for (int i=0; i<n-1; i++)
00330                             drawLine(xx[i],yy[i],xx[i+1],yy[i+1]);
00331                      drawLine(xx[n-1],yy[n-1],xx[0],yy[0]);                  
00332               }
00333        }
00334 
00335        public void drawImage (Image i, int x, int y, ImageObserver o)
00336        {      G.drawImage(i,x,y,o);
00337        }      
00338        
00339        public void drawImage (Image i, int x, int y, int w, int h,
00340               ImageObserver o)
00341        {      G.drawImage(i,x,y,w,h,o);
00342        }      
00343 
00344        FontStruct FS=new FontStruct();
00345 
00346        public void setFont (int h, boolean bold)
00347        {      Font f=FS.getFont(h,bold);
00348               if (f!=null) G.setFont(f);
00349               else
00350               {      f=new Font(
00351                             Global.getParameter("font.name","dialog"),
00352                             bold?Font.BOLD:Font.PLAIN,
00353                             h);
00354                      FS.storeFont(h,bold,f);
00355                      G.setFont(f);
00356               }
00357        }
00358 
00359        int fsize;
00360        boolean flarge,fbold;
00361        int ffactor=Global.getParameter("ffactor",130);
00362        
00363        public void setDefaultFont (int h, boolean large, boolean bold)
00364        {      ffactor=Global.getParameter("ffactor",130);
00365               fsize=h; flarge=large; fbold=bold;
00366               setFont(large,bold);
00367        }
00368        
00369        public void setFont (boolean large, boolean bold)
00370        {      int size=fsize;
00371               if (large) size=size*ffactor/100;
00372               if (flarge) size=size*ffactor/100;
00373               setFont(size,bold || fbold);
00374        }
00375 
00376        public void drawImage (Image i, double x, double y, double x1, double y1, 
00377                      double x2, double y2, ImageObserver o)
00378        {      try
00379               {      int w=i.getWidth(o),h=i.getHeight(o);
00380                      AffineTransform AT=new AffineTransform((x1-x)/w,(y1-y)/w,(x2-x)/h,(y2-y)/h,x,y);
00381                      G.drawImage(i,AT,o);
00382               }
00383               catch (Exception e) {}
00384        }
00385 
00386        public Graphics getGraphics() 
00387        {      return G;
00388        }
00389 
00390        int StrH=0,StrW=0,StrAsc=0;
00391        boolean StrTex=false;
00392        String Str=null;
00393        
00394        public void computeString (String s)
00395        {      if (s.equals(Str)) return;
00396               StrH=StrW=StrAsc=0;
00397               StrTex=false;
00398               Str=s;
00399               while (s!="") 
00400               {      int n=s.indexOf('$');
00401                      while (n>0 && s.charAt(n-1)=='\\') n=s.indexOf('$',n+1);
00402                      if (n<0)
00403                      {      StrH=Math.max(StrH,getFontMetrics().getHeight());
00404                             StrW+=getFontMetrics().stringWidth(s);
00405                             StrAsc=Math.max(StrAsc,getFontMetrics().getAscent());
00406                             break;
00407                      }
00408                      else
00409                      {      StrTex=true;
00410                             String ss=s.substring(0,n);
00411                             s=s.substring(n+1);
00412                             StrH=Math.max(StrH,getFontMetrics().getHeight());
00413                             StrW+=getFontMetrics().stringWidth(ss);
00414                             StrAsc=Math.max(StrAsc,getFontMetrics().getAscent());
00415                             n=s.indexOf('$');
00416                             while (n>0 && s.charAt(n-1)=='\\') n=s.indexOf('$',n+1);
00417                             ss=s;
00418                             if (n>=0)
00419                             {      ss=s.substring(0,n); s=s.substring(n+1);
00420                             }
00421                             else
00422                             {      s="";
00423                             }
00424                             if (HE==null || !HE.getEquation().equals(ss))
00425                                    setHotEqn(ss);
00426                             StrH=Math.max(StrH,heightHotEqn(G));
00427                             StrW+=widthHotEqn(G);
00428                             StrAsc=Math.max(StrAsc,ascentHotEqn(G));
00429                      }
00430               }             
00431        }
00432        
00433        public int stringWidth (String s) 
00434        {      computeString(s);
00435               return StrW;
00436        }
00437 
00438        public int stringHeight (String s) 
00439        {      computeString(s);
00440               return StrH;
00441        }
00442        
00443        public boolean isTex (String s)
00444        {      computeString(s);
00445               return StrTex;
00446        }
00447        
00448        public int drawStringExtended (String s, double x, double y) 
00449        {      if (s.startsWith("$$"))
00450               {      s=s.substring(2);
00451                      if (s.endsWith("$$")) s=s.substring(0,s.length()-2);
00452                      if (HE==null || !HE.getEquation().equals(s))
00453                             setHotEqn(s);
00454                      if (LOut==null || 
00455                                    !LOut.println("$$"+s+"$$",x,y+ascentHotEqn(G)))
00456                             return paintHotEqn((int)x,(int)y,G);
00457                      else
00458                             return heightHotEqn(G);
00459               }
00460               computeString(s);
00461               if (LOut!=null)
00462               {      if (StrTex && LOut.printDollar())
00463                      {      LOut.println(s,x,y+StrAsc,true);
00464                             return StrH;
00465                      }
00466                      else if (!StrTex)
00467                      {      if (LOut.println(s,x,y+StrAsc))
00468                                    return StrH;
00469                      }
00470               }
00471               int w=0;
00472               while (s!="") 
00473               {      int n=s.indexOf('$');
00474                      while (n>0 && s.charAt(n-1)=='\\') n=s.indexOf('$',n+1);
00475                      if (n<0)
00476                      {      drawString(translateDollar(s),x+w,y+StrAsc);
00477                             w+=getFontMetrics().stringWidth(s);
00478                             break;
00479                      }
00480                      else
00481                      {      String ss=s.substring(0,n);
00482                             s=s.substring(n+1);
00483                             drawString(translateDollar(ss),x+w,y+StrAsc);
00484                             w+=getFontMetrics().stringWidth(ss);
00485                             n=s.indexOf('$');
00486                             while (n>0 && s.charAt(n-1)=='\\') n=s.indexOf('$',n+1);
00487                             ss=s;
00488                             if (n>=0)
00489                             {      ss=s.substring(0,n); s=s.substring(n+1);
00490                             }
00491                             else
00492                             {      s="";
00493                             }
00494                             if (HE==null || !HE.getEquation().equals(ss))
00495                                    setHotEqn(ss);
00496                             paintHotEqn((int)x+w,(int)y+StrAsc-ascentHotEqn(G),G);
00497                             w+=widthHotEqn(G);
00498                      }
00499               }
00500               return StrH;
00501        }
00502        
00503        public String translateDollar (String s)
00504        {      int n;
00505               while ((n=s.indexOf("\\$"))>=0)
00506               {      s=s.substring(0,n)+"$"+s.substring(n+2);
00507               }
00508               return s;
00509        }
00510 
00511        public int stringAscent (String s) 
00512        {      return getFontMetrics().getAscent();
00513        }
00514 
00515        sHotEqn HE=null;
00516        
00517        public void setHotEqn (String s)
00518        {      if (ZC==null) return;
00519               if (HE==null)
00520               {      HE=new sHotEqn(ZC);
00521               }
00522               HE.setEquation(s);
00523        }
00524        
00525        public int paintHotEqn (int c, int r, Graphics g)
00526        {      if (HE==null) return 0;
00527               return HE.paint(c,r,g);
00528        }
00529        
00530        public int heightHotEqn (Graphics g)
00531        {      if (HE==null) return 0;
00532               return HE.getSizeof(HE.getEquation(),g).height;
00533        }
00534               
00535        public int ascentHotEqn (Graphics g)
00536        {      if (HE==null) return 0;
00537               return HE.getAscent(HE.getEquation(),g);
00538        }
00539        
00540        public int widthHotEqn (Graphics g)
00541        {      if (HE==null) return 0;
00542               return HE.getSizeof(HE.getEquation(),g).width;
00543        }
00544               
00545 }