import java.awt.*;
import java.util.*;
import java.awt.event.*;
import java.util.EventListener;
import javax.swing.*;
import java.awt.geom.*;
import java.awt.Image.*;
import java.awt.image.*;
import java.awt.image.BufferedImage;
import java.io.*;
class LC extends JPanel implements MouseListener, MouseMotionListener
 {
  int mce=0;
  Img imga=null;
  Enumeration enk;
  Hashtable hh=new Hashtable();
  Hashtable rh=new Hashtable();
  int rm=0;
  K ka=new K(0,0,0,0),kb=new K(0,0,0,0),kd=new K(0,0,0,0); 
  dP sc=new dP(1,1);
  dP xy=new dP(0,0);
  boolean sptush=true;
  double rbsnap=0;
  //boolean edcucu=false,cucuhit=false;
  boolean edcucu=true,cucuhit=true;// now
  boolean edqucu=false,qucuhit=false;
  boolean edbscu=false,bscuhit=false;
  // boolean edwu=false,wuhit=false;
  boolean edwu=false ,wuhit=false; // FOR WAVE EDITING *************
  boolean edwue=false ,wuehit=false; // FOR WAVE EDITING *************
  boolean edwuegraph=false ,wuegraphhit=false; // FOR WAVE EDITING *************
  Wav wav=null;
  boolean edfft=false,ffthit=false;
  boolean edaux=false,auxhit=false;
  Line2D line;
  boolean xor=false;
  int cw,ch;
  PixelGrabber pg;
  int pxa[],pixa[];
  Color ffa,ffb;
  int pixaw,ffai,ffbi;
  dP mp,mr,mc,mm,md,men,mex;
  double oxsc=1,oysc=1;
  FontMetrics ftm;
  Font ft;
  int ftsize,ftstyle;
  String ftname;
  Rec rbrec;
  Point rbbP,rbnP,rbeP;
  boolean rband=false;
  boolean wavrband=false;
  int rbanded=0;
  static final double PI=Math.PI;
  static final double PIPI=2*Math.PI;
  static final double DR=PI/180;
  static final double RD=180/PI;
  LScP lscp;
  BufferedImage offsim;
  Rec rec;
  Rectangle clip;
  int width, height;
  Graphics2D onsg,offsg;
  K pk=new K(Color.blue);
  // K fk=new K(Color.green);
  K fk=new K(100,200,200,0);
  double pw=3;
  boolean mouseButtonDown = false;
  boolean dons=true,doffs=true;
  int x,y;
  boolean ftt= true;
  M parent;
  String name;
  String host="";
  String pos="";
  Te te;
  Tu tu;
  Hashtable kidsh=new Hashtable();
  String[] kidsa=new String[1024];
  int nflayer=1;
  LSpP lspp;
  Rec[] recarr=new Rec[20];
  LC(M p,String name,LSpP zippy,String position,Rec rec,Rec lscprec)
   {
    super();
    this.setOpaque( false ) ;
    parent=p;
    te=parent.te;
    tu=parent.tu;
    this.name=name;
    lspp=zippy;
    lspp.lc=this;
    host=lspp.name;
    pos=position;
    this.rec=rec;
    x=rec.xi;
    y=rec.yi;
    width=rec.wi;
    height=rec.hi;
    setPreferredSize(new Dimension(rec.wi,rec.hi));
    setBackground(fk.c);
    lscp=new LScP(parent,name+"lscp",lscprec);
    lscp.setPreferredSize(new Dimension(lscprec.wi,lscprec.hi));
    lscp.getViewport().add(this);
    if(pos.equals("left")) lspp.setLeftComponent(lscp);
    else if(pos.equals("right")) lspp.setRightComponent(lscp);
    else if(pos.equals("top")) lspp.setTopComponent(lscp);
    else if(pos.equals("bottom")) lspp.setBottomComponent(lscp);
    addMouseListener(this);
    addMouseMotionListener(this);
    setLayout(null);
    Te.LCh.put(name,this);
  //  tta("LC constructor end");
   }
  void setHost(LSpP zippy)
   {
    lspp=zippy;
    lspp.lc=this;
    if(pos.equals("left")) lspp.setLeftComponent(lscp);
    else if(pos.equals("right")) lspp.setRightComponent(lscp);
    else if(pos.equals("top")) lspp.setTopComponent(lscp);
    else if(pos.equals("bottom")) lspp.setBottomComponent(lscp);
   }
  public void init()
   {
    tta("LC.init()");
    xoroff();
    rband=false;
    wavrband=false;
    dons=true;
    doffs=true;
    pk=new K(Color.blue);
    fk=new K(Color.white);
    pw=3;
    clearscreen();
   }
  public void paintSc()
   {
    onsg.drawImage(offsim, 0, 0, this);
   }
  public void scrollRecToVisible(Rec rec)
   {
    scrollRectToVisible(rec.rect);
   }
  public dP gvp() { return new dP(lscp.getViewport().getExtentSize()); }
  public dP gcvp() 
   {
    dP a=gvp();
    dP b=new dP(lscp.getViewport().getViewPosition());
    dP c=new dP((a.x/2+b.x-width/2)/sc.x,(-a.y/2-b.y+height/2)/sc.y);
    return c;
   }
  public void scvp(dP dpa) 
   {
    dP lscpvp=gvp();
    dP lcsize=new dP(getSize()); 
    dpa.x*=sc.x;
    dpa.y*=sc.y;
    dpa.y= -dpa.y;
    dpa.ae(lcsize.scale(.5));
    dpa.me(lscpvp.scale(.5));
    lscp.getViewport().setViewPosition(dpa.p);
   }
  public void update(Graphics g) 
   {
    if (offsim != null) g.drawImage(offsim,0,0,this); 
   }
  public void paint(Graphics g) { update(g); }
  //synchronized public void paintComponent() { if (offsim != null) getGraphics().drawImage(offsim,0,0,this); }
  //synchronized public void setBounds(int xx, int yy,int wwidth, int hheight) 
  public void setBounds(int x, int y,int width, int height) 
   {
    // tta("LC.setBounds beginning "+name);
    if(!ftt)
     {
      // tta("LC.setBounds !ftt before calling super.setBounds !ftt "+name);
      super.setBounds(x,y,width,height);
      // tta("LC.setBounds !ftt post calling super.setBounds !ftt "+name);
      return;
     }
    ftt=false;
    this.width = width;  
    this.height = height;  
    rec=new Rec(x,y,width,height);
    if (width > 0 && height > 0) 
     {
      BufferedImage newim = new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);
      //    BufferedImage newim = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
      offsg=newim.createGraphics(); 
      onsg=(Graphics2D)getGraphics();
      onsg.transform(new AffineTransform());
      offsg.transform(new AffineTransform());
      offsg.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
      tformxy(width/2,height/2);
      tformsc(1,-1);
      clearscreen();  
      if (offsim != null) offsg.drawImage(offsim,0,0,this);       
      offsim = newim;  
     }
    // tta("LC.setBounds before calling super.setBounds not the ftt "+name);
    super.setBounds(x,y,width,height);  
    // tta("LC.setBounds post calling super.setBounds not the ftt "+name);
    setclip(onsg,rec);
    myRepaint(); 
   }
  public void resettransform()
   {
    //   BufferedImage newim = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
    BufferedImage newim = new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);
    offsg=newim.createGraphics();
    onsg=(Graphics2D)getGraphics();
    onsg.transform(new AffineTransform());
    offsg.transform(new AffineTransform());
    offsg.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    tformxy(width/2,height/2);
    tformsc(1,-1);
    clearscreen();
    if (offsim != null) offsg.drawImage(offsim,0,0,this);
    offsim = newim;
   }
  public void aaon()
   {
    offsg.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
   }
  public void aaoff()
   {
    offsg.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
   }
  public void setw(double w) 
   {
    if(w != width) { setsize(new dP(w,height)); }
   }
  public void seth(double h) 
   {
    if(h != height) { setsize(new dP(width,h)); }
   }
  public void setrec(Rec r) 
   {
    if(rec.h != height || rec.w != width) { setsize(r.s); }
   }
  public void setsize(dP dp)
   {
    width=dp.xi;
    height=dp.yi;
    sc=new dP(1,1);
    if(tu != null) sc=tu.lcsc;
    rec=new Rec(x,y,dp.xi,dp.yi);
    setPreferredSize(new Dimension(width,height));
    if (width > 0 && height > 0)
     {
      //old not used BufferedImage newim = new BufferedImage(width,height,BufferedImage.TYPE_4BYTE_ABGR);
      //   BufferedImage newim = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
      BufferedImage newim = new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);
      offsg=newim.createGraphics();
      onsg=(Graphics2D)getGraphics();
      onsg.transform(new AffineTransform());
      offsg.transform(new AffineTransform());
      offsg.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
      tformxy(width/2,height/2);
      tformsc(sc.x,-sc.y);
      clearscreen();
      if (offsim != null) offsg.drawImage(offsim,0,0,this);
      offsim = newim;
     }
    super.setBounds(x,y,width,height);
    myRepaint();
   }
  public void drawImg(Img img,dP dp,double scale,double ang)   // for new
   {
    imga=null;
    if(scale==100) imga=img;
    else
     {
      //tta("scaling image");
      imga=Img.scalelib(img,scale,scale);
     }
    if (imga !=null && offsim != null)
     {
      AffineTransform orgTransform=offsg.getTransform();
      AffineTransform ct=AffineTransform.getScaleInstance(1,-1);
      offsg.transform(ct);
      onsg.transform(ct);
      //  tformsc(1,-1);
      AffineTransform dt=AffineTransform.getTranslateInstance(dp.x,-dp.y);
      offsg.transform(dt);
      onsg.transform(dt);
      //tformxy(dp.x,-dp.y);
      AffineTransform ft=AffineTransform.getRotateInstance(ang*DR);
      offsg.transform(ft);
      onsg.transform(ft);   
      //tformrot(ang);     
      AffineTransform et=AffineTransform.getTranslateInstance(-dp.x,dp.y);
      offsg.transform(et);
      onsg.transform(et);
      // tformxy(-dp.x,dp.y);
      if(doffs) 
       {
        //  offsg.drawImage(imga,dp.xi,-imga.rec.hi-dp.yi,this);
        int iw=imga.getWidth();
        int ih=imga.getHeight();
        int j=dp.xi-iw/2;
        int k=-imga.rec.hi-dp.yi+ih/2;
        offsg.drawImage(imga,j,k,this);
       } 
      offsg.setTransform(orgTransform);
     }
    imga=null;
    img=null;
    myRepaint();
   }
  public void tta(String s) { parent.tta(s); }
  public void tta1(String s) { parent.tta1(s); }
  public void tformsc(double x) { tformsc(x,x); }
  public void tformscx(double x) { tformsc(x,1); }
  public void tformscy(double y) { tformsc(1,y); }
  public void tformsc(dP dp) { tformsc(dp.x,dp.y); }
  public void tformsc(double x,double y)
   {
    if(x==0 || y==0)
     {
      tta("bad tformsc x:"+x+" y:"+y);
      return;
     }
    oxsc*=x;
    oysc*=y;
    AffineTransform ct=AffineTransform.getScaleInstance(x,y);
    offsg.transform(ct);
    onsg.transform(ct);
   }
  public void tformrsc(dP dp) { tformrsc(dp.x,dp.y); }
  public void tformrsc(double x,double y)
   {
    tformsc(x,y);
   }
  public void tformasc(dP dp) { tformasc(dp.x,dp.y); }
  public void tformasc(double x,double y)
   {
    tta("oxsc:"+oxsc);
    tformsc(x/oxsc,y/oysc);
   }
  public void tformrot(double transdeg)
   {
    AffineTransform ct=AffineTransform.getRotateInstance(transdeg*DR);
    offsg.transform(ct);
    onsg.transform(ct);
   }
  public void tformxy(double transx,double transy)
   {
    AffineTransform ct=AffineTransform.getTranslateInstance(transx,transy);
    offsg.transform(ct);
    onsg.transform(ct);
   }
  public void tformxy(dP aa)
   {
    xy=new dP(aa);
    AffineTransform ct=AffineTransform.getTranslateInstance(xy.x,xy.y);
    offsg.transform(ct);
    onsg.transform(ct);
   }
  public void clearscreen() { clearscreen(fk); }
  public void clearscreen(K k) 
   {
    offsg.setColor(k.c); 
    offsg.setBackground(k.c);
    // if(doffs) offsg.fillRect(-width/2,-height/2, width, height);     
    //  onsg.clearRect(-width/2,-height/2, width, height);     
    if(doffs) offsg.clearRect(-width/2,-height/2, width, height);     
    offsg.setColor(pk.c); 
    myRepaint();
   }
  public void clearoffscreen(K k) 
   {
    offsg.setColor(k.c); 
    offsg.fillRect(-width/2,-height/2, width, height);     
    // offsg.clearRect(-width/2,-height/2, width, height);    
    offsg.setColor(pk.c); 
   }
  public void clearoffscreen() { clearoffscreen(fk); }
  public void fillRec(double lx,double ly,double w,double h,K k) 
   {
    offsg.setColor(k.c);
    if(doffs) offsg.fillRect((int)lx,(int)ly,(int)w,(int)h);
    offsg.setColor(pk.c);
    myRepaint();
   }
  synchronized public void drawosLine(double xo,double yo,double x,double y,K k,double w)
   {
    spk(k);
    spw(w);
    drawosLine(xo,yo,x,y);
   }
  synchronized public void drawosLine(double xo,double yo,double x,double y)
   {
    Line2D l=new Line2D.Double(xo,yo,x,y);
    onsg=(Graphics2D)getGraphics();
    onsg.transform(new AffineTransform());
    onsg.transform(AffineTransform.getTranslateInstance(width/2,height/2));
    onsg.transform(AffineTransform.getScaleInstance(1,-1));
    onsg.draw(l);
    //validate();
   }
  synchronized public void drawLine(double xo,double yo,double x,double y,K k,double w)
   {
    spk(k);
    spw(w);
    drawLine(xo,yo,x,y);
   }
  synchronized public void redrawLine()
   {
    if(doffs && line != null) offsg.draw(line);
   }
  synchronized public void drawLine(double xo,double yo,double x,double y)
   {
    line=new Line2D.Double(xo,yo,x,y);
    if(doffs) offsg.draw(line);
    if(dons)
     {
      double sx=sc.x;
      if(sx<1) sx=1;
      double sy=sc.y;
      if(sy<1) sy=1;
      int x0 = (int)(sx*Math.min(xo,x));
      int y0 = (int)(sy*Math.max(yo,y));
      int dx = (int)(sx*Math.abs(x-xo)+1);
      int dy = (int)(sy*Math.abs(y-yo)+1);
      myRepaint((int)(width/2+x0-sx*pw),(int)(height/2-y0-sy*pw),(int)(dx+2*sx*pw),(int)(dy+2*sx*pw));  
     }
    //myRepaint(20+x0,20+y0,dx,dy);
    //myRepaint(width/2+x0,height/2+y0,dx,dy);  
    //myRepaint(new Rectangle(x0,y0,dx,dy));
    //RepaintManager myRepaintManager = RepaintManager.currentManager(this);
    //myRepaintManager.addDirtyRegion(this,x0,y0,dx,dy);
   }
  synchronized public void dS(String s,double x,double y,Font ft)
   {
    offsg.setFont(ft);
    tformsc(1,-1);
    if(doffs) offsg.drawString(s,(int)x,(int)-y);
    tformsc(1,-1);
    if(dons) repaint();
   }
  synchronized public void dS(String s,double x,double y,Font ft,double ang)
   {
    offsg.setFont(ft);
    AffineTransform orgTransform=offsg.getTransform();
    tformsc(1,-1);
    tformxy(x,-y);
    tformrot(ang);
    tformxy(-x,y);  
    if(doffs) offsg.drawString(s,(int)x,(int)-y);
    offsg.setTransform(orgTransform);
    if(dons) repaint();
   }
  synchronized public void rSh(Sh sh,boolean c,int df)
   {
    if(! doffs) return;
    AffineTransform orgTransform=offsg.getTransform();
    tformxy(sh.dp.x,sh.dp.y);
    tformrot(-sh.ang);
    tformsc(sh.sc,-sh.sc);
    if(c)tformxy(sh.dp2c.x,sh.dp2c.y);
    else tformxy(sh.dp2.x,sh.dp2.y);
    if(df==2 || df==3)
     {
      spk(sh.fk);
      offsg.fill(sh.gp);
     }
    if(df==1 || df==3)
     {
      spk(sh.pk);
      offsg.draw(sh.gp);
     }
    offsg.setTransform(orgTransform);
    myRepaint();
   }
  synchronized public void rdaPoly(double[] da,double scale,K k,dP dp,double ang,Graphics2D g,boolean f)
   {
    int z=da.length;
    int i=1;
    int j=z/2;
    int[] dpx=new int[j];
    int[] dpy=new int[j];
    int m=0;
    for(i=0;i<j;i++)
     {
      dpx[i]=(int)Math.rint(da[m++]);
      dpy[i]=(int)Math.rint(da[m++]);
     }
    rPoly(new Polygon(dpx,dpy,j),dp,ang,scale,g,f,k);
   }
  synchronized public void rPoly(Polygon poly,dP dp,double ang,double scale,Graphics2D g,boolean f,K k)
   {
    g.setColor(k.c);
    AffineTransform orgTransform=offsg.getTransform();
    tformsc(scale,scale);
    tformxy(dp.x,dp.y);
    tformrot(-ang);
    //tformxy(-dp.x,-dp.y);
    if(f) g.fillPolygon(poly);
    else  g.drawPolygon(poly);
    offsg.setTransform(orgTransform);
    g.setColor(pk.c);
    myRepaint();
   }
  synchronized public void rdaPoly(double[] da,dP dp,double scale,Graphics2D g,boolean f,K k)
   {
    int z=da.length;
    int i=1;
    int j=z/2;
    int[] dpx=new int[j];
    int[] dpy=new int[j];
    int m=0;
    for(i=0;i<j;i++)
     {
      dpx[i]=(int)Math.rint(dp.x+scale*da[m++]);
      dpy[i]=(int)Math.rint(dp.y+scale*da[m++]);
     }
    rPoly(new Polygon(dpx,dpy,j),g,f,k);
   }
  synchronized public void rPoly(Polygon poly,Graphics2D g,boolean f,K k) 
   {
    g.setColor(k.c);
    if(f) g.fillPolygon(poly);
    else  g.drawPolygon(poly);
    g.setColor(pk.c);
    myRepaint();
   }
  synchronized public void rRecoffs(Rec r,boolean f)
   {
    if(f) offsg.fill(new Rectangle2D.Double(r.x,r.y,r.w,r.h));
    else  offsg.draw(new Rectangle2D.Double(r.x,r.y,r.w,r.h));
   }
  synchronized public void csRec(Rec r,Graphics2D g,boolean f,K k)
   {
    if(g==null) return;
    //  offsg.setColor(k.c);
    offsg.setBackground(k.c);
    // onsg.clearRect(r.xi,r.yi,r.wi,r.hi);
    offsg.clearRect(r.xi,r.yi,r.wi,r.hi);
    //onsg.fillRect(r.xi,r.yi,r.wi,r.hi);
    //offsg.fillRect(r.xi,r.yi,r.wi,r.hi);
    g.setColor(pk.c);
    myRepaint(r,true);
   } 
  //   rRec(r,offsg,false,r.fk);
  synchronized public void rRec(Rec r,Graphics2D g,boolean f,boolean p)
   {
    if(g==null) return; 
    if(p) rRec(r,g,f,r.pk);
    else rRec(r,g,f,r.fk);
    tta("LC.rRec no myRepaint()");
   }
  synchronized public void rRec(Rec r,Graphics2D g,boolean f,K k)
   {
    tta("LC.rRec(Rec,G2D,boolean,K");
    if(g==null) return;
    g.setColor(k.c);
    if(doffs && f) g.fill(new Rectangle2D.Double(r.x,r.y,r.w,r.h));
    else if(doffs) g.draw(new Rectangle2D.Double(r.x,r.y,r.w,r.h));
    g.setColor(pk.c);
    myRepaint(r,true);
   }
  synchronized public void rRec(Rec r,Graphics2D g,dP dp1,K k1,dP dp2,K k2)
   {
    if(g==null) return;
    GradientPaint gp=new GradientPaint(dp1.xi,dp1.yi,k1.c,dp2.xi,dp2.yi,k2.c);
    g.setPaint(gp);
    if(doffs) g.fill(new Rectangle2D.Double(r.x,r.y,r.w,r.h));
    g.setColor(pk.c);
    myRepaint(r,true);
   }
  synchronized public void rEll(double x,double y,double d,Graphics2D g,boolean f,K k)
   {
    if(g==null || !doffs) return;
    g.setColor(k.c);
    if(f) g.fill(new Ellipse2D.Double(x-d/2,y-d/2,d,d));
    else g.draw(new Ellipse2D.Double(x-d/2,y-d/2,d,d));
    g.setColor(pk.c);
    myRepaint(new Rec(x,y,d,d),true);
   }
  //synchronized public void rEll(double x,double y,double d,Graphics2D g,boolean f,K k)
  synchronized public void rEll(Ci a,dP dp,Graphics2D g,int f)
   {
    dP dpo=a.cp;
    a.cp=dp;
    a.com();
    rEll(a,g,f);
    a.cp=dpo;
    a.com();
   }
  synchronized public void rEll(Ci a,Graphics2D g,int f)
   {
    if(g==null || !doffs) return;
    if(f==2 || f==3)
     {
      g.setColor(a.fk.c);
      g.fill(a);
     }
    if(f==1 || f==3)    
     {
      g.setColor(a.pk.c);
      g.draw(a);
     }
    g.setColor(pk.c);
    myRepaint(new Rec(a.loc.x-2,a.loc.y-2,a.rx+4,a.ry+4),true);
   }
  synchronized public void myRepaint() 
   {
    //if(dons) repaint(); 
    //if(dons) myRepaint(-width/2,-height/2, width, height);     
    myRepaint(0,0, width, height);     
   }
  synchronized public void myRepaint(Rec r) { if(dons) myRepaint(r.xi,r.hi-r.yi,r.wi,r.hi); }
  synchronized public void myRepaint(Rec r,boolean b) 
   {
    if(!dons) return;
    if(b) 
     {
      //repaint(r.xi,r.yi,r.wi,r.hi);
      myRepaint();
     }
    else  myRepaint(r.xi,r.hi-r.yi,r.wi,r.hi);
   }
  synchronized public void myRepaint(int x,int y,int w,int h) 
   {
    if(rm==1 && Te.Meth.containsKey("render"))
     {
      rm=0;
      parent.te.teS("render");
      rm=1;
     }
    if(dons) repaint(x,y,w,h);
   }
  synchronized public void render()
   {
    myRepaint(0,0, width, height);     
   }
  synchronized public void m2Repaint(double x,double y,double w,double h) 
   {
    if(dons) myRepaint((int)(rec.wi/2 + x),(int)(rec.hi/2-(y+h)),(int)w,(int)h);
   }
  synchronized public void ds() { repaint(); }
  synchronized public void fRecfk(Rec r) { rRec(r,offsg,true,r.fk); }
  synchronized public void fRecpk(Rec r) { rRec(r,offsg,true,r.pk); }
  synchronized public void fRec(Rec r,K k) { rRec(r,offsg,true,k); }
  synchronized public void dRecfk(Rec r) 
   { 
    //tta("LC.dRecfk rband:"+rband+" Rec r:"+Te.Svo(r));
    rRec(r,offsg,false,r.fk);
   }
  synchronized public void dRecpk(Rec r) { rRec(r,offsg,false,r.pk); }
  synchronized public void dRec(Rec r,K k) { rRec(r,offsg,false,k); }
  public void mouseEntered(MouseEvent e) 
   {
    men=MEtodP(e);
    //tta("LC.mouseEntered");
    te.runLCTrigteS(name+"men",men);
   }
  public void mouseMoved(MouseEvent e) 
   {
    mm=MEtodP(e);
    // tta("LC.mouseMoved");
    te.mmdphh(this,mm); 
    te.runLCTrigteS(name+"mm",mm);
   }
  public void mousePressed(MouseEvent e)   // mp      note: md is mouseDragged not mouse down which is mouse pressed
   {	   
    mp=MEtodP(e);
    /* if(wavrband)
     { 
      rbbP=mp.p; // rb beginning point
      rbeP=null;  
     }
    */
    if(rband) 
     {
      //   tta("LC.mousePressed rband true");
      rbbP=mp.p; // rb beginning point
      rbeP=null;  
      //onsg.drawImage(offsim, 0, 0, this);
      line=null;
      rbeP=null; 
      rbrec=null; 
     }
    te.mpdphh(this,mp);
    te.runLCTrigteS(name+"mp",mp);
   }
  public void mouseDragged(MouseEvent e)
   {
    md=MEtodP(e);
    if(wavrband)
     {
      offsg.setXORMode(Color.black);
      if(rbrec !=null)
       {
        offsg.draw(rbrec);
        offsg.setPaintMode();      
        rbanded=0;
        //tta("LC.mouseDragged wav not null  oh oh");
        for(int j=0;j<wav.reca.length;j++) 
         { 
          if(rbrec.contains(wav.reca[j]))  
           {
            ++rbanded;
            wav.reca[j].estate=1;
            offsg.setColor(Color.green);
            offsg.fill(wav.reca[j].rect);  
            offsg.setColor(Color.BLACK);
            offsg.draw(wav.reca[j].rect);
            offsg.setColor(pk.c);
           }
          else if(wav.reca[j].estate==1)
           {
            --rbanded;
            wav.reca[j].estate=0;
            offsg.setColor(Color.blue);
            offsg.fill(wav.reca[j].rect);
            offsg.setColor(Color.BLACK);
            offsg.draw(wav.reca[j].rect);
            offsg.setColor(pk.c);
           }
         } 
        repaint(x,y,width,height);
       }
      rbeP=md.p;  // rband end point
      calcrbRec();
      offsg.setXORMode(Color.black);
      offsg.draw(rbrec); 
     } 
    else if(rband)
     { 
      offsg.setXORMode(getBackground());
      if(rbrec !=null)
       {
        //  tta("LC.mouseDragged rband true rbrec is not null rbrec:"+te.Svo(rbrec));
        offsg.draw(rbrec);
        onsg.draw(rbrec);
       }
      rbeP=md.p;
      // tta("LC.mouseDragged rband true pre calcRec");
      calcrbRec();
      // offsg.setXORMode(Color.black);
      offsg.draw(rbrec); 
      //onsg.draw(rbrec);
      repaint(x,y,width,height);
     }
    te.mddphh(this,md);   //mouse dragged
    te.runLCTrigteS(name+"md",md);
   }
  public void mouseReleased(MouseEvent e)
   {
    mr=MEtodP(e);
    // tta("LC.mouseReleased"); 
    if(rband)
     {
      if( rbrec != null)
       {
        offsg.draw(rbrec);
        //rbrec=null;
        repaint(x,y,width,height);
       }
      // if(! mr.eq(md)) tta("LC.mouseReleased rband check this"); 
      offsg.setPaintMode(); 
      rbStop(); 
     }
    te.mrdphh(this,mr);
    te.runLCTrigteS(name+"mr",mr);
   }
  public void mouseClicked(MouseEvent e)  
   {
    mc=MEtodP(e);
    //tta("LC.mouseClicked");
    if(edbscu) te.bscu.mcdp(mc);
    else if(edwu) te.wu.mcdp(mc);
    //else if(edwue) te.wue.mcdp(mc);
    else if(edwuegraph) te.wuegraph.mcdp(mc);
    else if(edfft) te.fft.mcdp(this,mc);
    te.runLCTrigteS(name+"mc",mc);
   }
  public void mouseExited(MouseEvent e)
   {
    mex=MEtodP(e);
    te.runLCTrigteS(name+"mex",mex);
   }
  public void rbandExpA()
   {
    K k=new K(getBackground());
    //  tta("LC.rbandExpA rband true  background:"+te.Svo(k));
    clearscreen(new K(Color.cyan));
    //clearscreen(new K(100,100,200,110));
    //onsg.drawImage(offsim, 0, 0, this);
    if(true)
     {
      offsg.setPaintMode();
      offsg.setColor(Color.green);
      line=new Line2D.Double(0,-200,0,200);
      offsg.draw(line);
      line=new Line2D.Double(-200,0,200,0);
      offsg.draw(line);
      repaint(x,y,width,height);
      recarr=new Rec[10];
      offsg.setStroke(new BasicStroke((float)2));
      for(int j=0;j<10;j++) 
       {
        int x=(int) (200*Math.random());
        int y=(int) (200*Math.random());
        recarr[j]=new Rec(x,y,8,8);
        offsg.setColor(Color.blue);
        offsg.fill(recarr[j].rect);  
        offsg.setColor(Color.BLACK);
        offsg.draw(recarr[j].rect);
        offsg.setColor(pk.c);
        repaint(x,y,width,height);
       } 
      offsg.setStroke(new BasicStroke((float)pw));
     }
   }
  public void rbandExpB()
   {
    for(int j=0;j<10;j++) 
     { 
      if(rbrec.contains(recarr[j]))  
       {
        recarr[j].estate=1;
        offsg.setColor(Color.green);
        offsg.fill(recarr[j].rect);  
        offsg.setColor(Color.BLACK);
        offsg.draw(recarr[j].rect);
        offsg.setColor(pk.c);
       }
      else if(recarr[j].estate==1)
       {
        recarr[j].estate=0;
        offsg.setColor(Color.blue);
        offsg.fill(recarr[j].rect);  
        offsg.setColor(Color.BLACK);
        offsg.draw(recarr[j].rect);
        offsg.setColor(pk.c);
       }
     } 
   }
  public dP MEtodP(MouseEvent e) 
   {
    if(tu != null) sc=tu.lcsc;
    return new dP((e.getPoint().x-width/2)/sc.x,(height/2-e.getPoint().y)/sc.y);
   }
  public Point MEtoPoint(MouseEvent e) 
   {
    if(tu != null) sc=tu.lcsc;
    return new Point((int)((e.getPoint().x-width/2)/sc.x),(int)((height/2-e.getPoint().y)/sc.y)); 
   }
  synchronized public void spk(K k)
   {
    //tta("LC.spk k:"+te.Svo(k));
    pk=k;
    if(offsg != null) offsg.setColor(pk.c);
   }
  synchronized public void sfk(K k) { fk=k; }
  synchronized public void spw(double d)
   {
    pw=d;
    if(offsg != null) offsg.setStroke(new BasicStroke((float)pw));
   }
  public void odofin()
   {
    tta("LC.odofin");
    try { finalize(); }
    catch (Throwable e)
     {
      tta("NoGo on finalize");
     }
    System.gc();
    tta("Post");
   }
  public void  dofin()
   { 
    tta("LC.dofin");
    lspp.remove(this);
    lspp.doLCnull();
    lspp.lc=null;
    // lspp.doLayout();
    // lspp.repaint();
    //lspp.dofin();
    odofin();
   }
  public void xoron() 
   {
    xor=true;
    // offsg.setXORMode(getBackground());
    // orig  offsg.setXORMode(Color.black);
    //tta("LC.xoron()");
   }
  public void xoroff() 
   {
    xor=false;
    offsg.setPaintMode(); 
    //  tta("LC.xoroff()");
   }
  public void rbStart()
   {
    // tta("LC.rbStart skipp xoron");
    xoron(); 
    rbrec=null;
    rband=true;
    tta("LC.rbStart() rbrec set to null");
   }
  public void rbStart(Point p)
   {
    //myth=new Thread();
    //myth.start();
   }
  public void rbStop()
   {
    rband=false;
    xoroff();
   }
  public void calcrbRec()
   {
    Point bl=new Point();
    int w=0;
    int h=0;
    if(rbbP.x<rbeP.x)
     {
      bl.x=rbbP.x;
      w=rbeP.x-rbbP.x;
     }
    else
     {
      bl.x=rbeP.x;
      w=rbbP.x-rbeP.x;
     }
    if(rbbP.y<rbeP.y)
     {
      bl.y=rbbP.y;
      h=rbeP.y-rbbP.y;
     }
    else
     {
      bl.y=rbeP.y;
      h=rbbP.y-rbeP.y;
     }
    //  rbrec=new Rec(bl.x,bl.y,w,h);
    rbrec=new Rec(bl.x/sc.x,bl.y/sc.y,w/sc.x,h/sc.y);
    // tta("calcrbRec new Rec"+Te.Svo(rbrec)+" sc:"+te.Svo(sc));
    // rbrec.scale(1/sc.x, 1/sc.y);
    // wrong way rbrec=new Rec(sc.x* bl.x,sc.y* bl.y,sc.x*w,sc.y*h);
    if(rbsnap >0) rbrec.snap(rbsnap);
   }
  Rec rbRec()
   {
    // tta("LC.rbRec() begin");
    rbsnap=0;
    double pwo=pw;
    spw(1);
    rbStart();
    //tts("LC.rbRec() post rbStart() rband:"+rband);
    while(rband) te.schleep(50);
    rbStop();
    //tts("LC.rbRec() post rbStop() rband:"+rband);
    spw(pwo);
    if(rbrec ==null)
     {
      //tts("LC.rbRec is null");
     }
    //tts("LC.rbRec");
    return rbrec;
   }
  Rec rbRec(dP a) { return rbRec(a,0); }
  Rec rbRec(dP a,double d)
   {
    rbsnap=d;
    rbbP=a.p;
    double pwo=pw;
    spw(1);
    rbStart();
    while(rband) te.schleep(50);
    rbStop();
    spw(pwo);
    return rbrec;
   }
  static int ivoftstyle(String s)
   {
    if(s.equals("bold")) return Font.BOLD;
    else if(s.equals("plain")) return Font.PLAIN;
    return -1;
   }
  static String Svoftstyle(Font f)
   {
    return Svoftstyle(f.getStyle());
   }
  static String Svoftstyle(int i)
   {
    if(i==Font.BOLD) return "bold";
    else if(i==Font.PLAIN) return "plain";
    return "unknown font style";
   }
  public void setftname(String ftname)
   {
    this.ftname=ftname;
    setft(false);
   }
  public void setftstyle(String s)
   {
    ftstyle=ivoftstyle(s);
    setft(false);
   }
  public void setftsize(double d)
   {
    this.ftsize=(int)d;
    setft(false);
   }
  public void setft() { setFont(ft); }
  public void setft(boolean sf)
   {
    ft=new Font(ftname,ftstyle,ftsize);
    if(sf) setFont(ft);
   }
  public void saveLCpng(String bfn)
   {
    //String fn="C:/webroot/ajlogo/html/ajlogo/version3beta/images/"+bfn;
    String fn="/webroot/ajlogo/html/ajlogo/version3beta/images/"+bfn;
    tta("LC.saveLCpng fn:"+fn);
    Img.pngEncode(pixelGrab(),fn);
   }
  public void saveLCjpg(String fn) { Img.jpgEncode(pixelGrab(),fn,1.0); }
  public void saveLCjpg(String fn,double qf) { Img.jpgEncode(pixelGrab(),fn,qf); }
  public void saveLCgif(String fn,int transindex) { Img.gifEncode(pixelGrab(),fn,transindex,false); }
  public void saveLCgif(String fn) { Img.gifEncode(pixelGrab(),fn,-1,false); }
  Img pgRec(Rec rec)
   {
    return Img.mkImg(pixelGrab(offsim,rec.xi,rec.yi,rec.wi,rec.hi));
   }
  Image pixelGrab() 
   {
    int w=width;
    int h=height;
    //  int w=getSize().width;
    // int h=getSize().height;
    tta("new LC.pixelGrab   width:"+w+" h:"+h+" from getSize()");
    return pixelGrab(offsim,-w/2,-h/2,w,h);
   }
  Image pixelGrab(Image im,Rec rec) { return pixelGrab(im,rec.xi,rec.yi,rec.wi,rec.hi); }
  Image pixelGrab(Image im,dP dp)
   {
    return pixelGrab(im,dp.xi,dp.yi,1,1);
   }
  Image pixelGrab(Image im,int xx,int yy,int w,int h)
   {
    int xl=xx+getSize().width/2;
    int yl=getSize().height/2-(yy+h);
    Image imb=createImage(1,1);
    pixa=new int[w*h];
    try
     {
      tta("LC.pixelGrab  xl:"+xl+" yl:"+yl+" w:"+w+"h:"+h);
      PixelGrabber pgl=new PixelGrabber(im,xl,yl,w,h,pixa,0,w);
      tta("PixelGrabber.getColorModel"+pgl.getColorModel());
      if(pgl.grabPixels() && pgl.status()!=0 )
       {
        tta("new MemoryImageSource  w:"+w+" h:"+h);
        imb=createImage(new MemoryImageSource(w,h,pixa,0,w));
        MediaTracker tracker = new MediaTracker(this);
        try
         {
          tracker.addImage(imb,0);
          tracker.waitForID(0);
         }
        catch(InterruptedException e) { e.printStackTrace(); }
       }
     }
    catch (InterruptedException e)
     {
      e.printStackTrace();
     }
    return imb;
   }
  Image pixelGrab(Img im,Rec rec)
   {
    int w=rec.wi;
    int h=rec.hi;
    int xl=im.rec.wi/2 + rec.xi;
    int yl=(im.rec.hi/2)-(h+rec.yi);
    Image imb=createImage(1,1);
    int[] pxaa=new int[w*h];
    try
     {
      PixelGrabber pgl=new PixelGrabber(im,xl,yl,w,h,pxaa,0,w);
      if(pgl.grabPixels() && pgl.status()!=0 )
       {
        imb=createImage(new MemoryImageSource(w,h,pxaa,0,w));
        MediaTracker tracker = new MediaTracker(this);
        try
         {
          tracker.addImage(imb,0);
          tracker.waitForID(0);
         }
        catch(InterruptedException e) { e.printStackTrace(); }
       }
     }
    catch (InterruptedException e)
     {
      e.printStackTrace();
     }
    return imb;
   }
  Img rbImg()
   {
    Rec lrec=rbRec();
    return Img.mkImg(pixelGrab(offsim,lrec.xi,lrec.yi,lrec.wi,lrec.hi));
   }
  public boolean pff()
   {
    cw=getSize().width;
    ch=getSize().height;
    pxa=new int[cw*ch];
    try
     {
      PixelGrabber pgl=new PixelGrabber(offsim.getSource(),0,0,cw,ch,pxa,0,cw);
      if(pgl.grabPixels() && pgl.status()!=0 )
       {
        MediaTracker tracker = new MediaTracker(this);
        try { tracker.waitForID(0); }
        //catch(InterruptedException e) { e.printStackTrace(); }
        catch(InterruptedException e) { return false ; }
       }
     }
    catch (InterruptedException e) { return false; }
    return true;
   }
  void mff(dP dp,K ka) { mff(dp,ka,new K(0,0,0),1); }
  void mff(dP dp,K ka,K kb) { mff(dp,ka,kb,1); }
  void mff(dP dp,K ka,K kb,int m) { mff(dp,ka,kb,m,0,0,0,0); }
  void mff(dP dp,K ka,K kb,int m,double dh,double ds,double dv,double da){mff(dp,ka,kb,m,new K(dh,ds,dv,da));}
  void mff(dP dp,K ka,K kb,int m,K kd)
   {
    int mode=m;
    ffai=ka.i;
    ffbi=kb.i;
    this.ka=ka;
    this.kb=kb;
    this.kd=kd;
    // mode 1 stop at border (target) color, flood all others.
    // 2 flood only the single target color with the flood color.
    // 3 like mode 1, flood any color but the border and the flood color itself, with flood color.
    // 4 flood any color except color range.
    // 5 like 1, stop at border (target) color, mix range around target and use flood on all else.
    // 6 like 2, flood target color, mix target and range with the flood color.
    if(! pff()) // grab canvas each time
     {
      tta("Could not pff()");
      return;
     }
    int xl=(int)(dp.xi+getSize().width/2);
    int yl=(int)(getSize().height/2-dp.yi);
    mff(xl,yl,mode);
    MediaTracker tracker = new MediaTracker(null);
    Image im=createImage(new MemoryImageSource(cw,ch,pxa,0,cw));
    try
     {
      tracker.waitForID(0);
      tracker.addImage(im,0);
     }
    catch(InterruptedException e) { e.printStackTrace(); }
    if(doffs)
     {
      tformsc(1,-1);
      offsg.drawImage(im,-getSize().width/2,-getSize().height/2,null);
      tformsc(1,-1);
     }
    myRepaint();
   }
  int getPixel(int x,int y) { return pxa[y*cw+x]; }
  public void setPixel(int x,int y) { pxa[y*cw+x]=ffai; }
  void mff(int x,int y,int mode)
   {
    int ai=y*cw+x;
    if (mode==1 && pxa[ai]==ffbi) return;
    else if(mode==2 && pxa[ai] !=ffbi) return;
    else if (mode==3 && pxa[ai]==ffbi) return;
    else if(mode==5 && pxa[ai]==ffbi) return;
    else if(mode==6)
     {
      if(pxa[ai]==ffbi) ;
      else if(! K.eq(new K(pxa[ai]),kb,kd)) return;
     }
    int pxqa[]=new int[cw*ch];//create a que possibly as big as the image
    int pxqs=0;
    pxqa[++pxqs]=(y<<16)+x;//convert the point to a value and place it in que
    //setPixel(x,y);// fill 
    if(mode==1 || mode==2 || mode==3) pxa[ai]=ffai;
    else if(mode==5) 
     {
      //tta("LC mode 5:"+pxa[ai]);
      if(K.eq(new K(pxa[ai]),kb,kd)) pxa[ai]=K.mixi5(ka,kb,pxa[ai]);
      else pxa[ai]=ffai;
      //tta("LC mode 5 post:"+pxa[ai]);
     }
    else if(mode==6)
     {
      if(pxa[ai] == ffbi) pxa[ai]=ffai;
      else  pxa[ai]=K.mixi6(ka,kb,pxa[ai]);
     }
    int spt=1;// number of flooded points 
    while(pxqs>0)
     {
      pxqa[0]=pxqa[pxqs--];//pull point from que
      x=pxqa[0] & 0xffff;// convert point to x,y
      y=(pxqa[0]>>16) & 0xffff;
      ai=y*cw+x-1;
      if(x>0)
       {
        if(mode ==1 && pxa[ai] != ffbi && pxa[ai] != ffai) //test about x,y
         {
          pxa[ai]=ffai;
          spt++;// inc flooded points
          pxqa[++pxqs]=(y<<16)+x-1;// encode x,y to point and put in que
         }
        else if(mode ==2 && pxa[ai] == ffbi) //test about x,y
         {
          pxa[ai]=ffai;
          spt++;// inc flooded points
          pxqa[++pxqs]=(y<<16)+x-1;// encode x,y to point and put in que
         }
        else if(mode ==3 && pxa[ai] != ffbi) //test about x,y
         {
          pxa[ai]=ffai;
          spt++;// inc flooded points
          pxqa[++pxqs]=(y<<16)+x-1;// encode x,y to point and put in que
         }
        else if(mode==5 && pxa[ai] != ffbi && pxa[y*cw+x-1] != ffai)
         {
          if(K.eq(new K(pxa[ai]),kb,kd)) pxa[ai]=K.mixi5(ka,kb,pxa[ai]);
          else  pxa[ai]=ffai;
          spt++;// inc flooded points
          pxqa[++pxqs]=(y<<16)+x-1;// encode x,y to point and put in que
         }
        else if(mode==6)
         {
          if(pxa[ai]==ffbi)
           {
            pxa[ai]=ffai;
            spt++;// inc flooded points
            pxqa[++pxqs]=(y<<16)+x-1;// encode x,y to point and put in que
           }
          else if( K.eq(new K(pxa[ai]),kb,kd))
           {
            pxa[ai]=K.mixi6(ka,kb,pxa[ai]);
            spt++;// inc flooded points
            pxqa[++pxqs]=(y<<16)+x-1;// encode x,y to point and put in que
           }
         }
       }
      if(y>0)
       {
        ai=(y-1)*cw+x;
        if(mode ==1 && pxa[ai] != ffbi && pxa[ai] != ffai)
         {
          pxa[ai]=ffai;
          spt++;
          pxqa[++pxqs]=((y-1)<<16)+x;
         }
        else if(mode ==2 && pxa[ai] == ffbi)
         {
          pxa[ai]=ffai;
          spt++;
          pxqa[++pxqs]=((y-1)<<16)+x;
         }
        else if(mode ==3 &&  pxa[ai] != ffbi) 
         {
          pxa[ai]=ffai;
          spt++;
          pxqa[++pxqs]=((y-1)<<16)+x;
         }
        else if(mode==5 && pxa[ai] != ffbi && pxa[ai]!=ffai)
         {
          if(K.eq(new K(pxa[ai]),kb,kd)) pxa[ai]=K.mixi5(ka,kb,pxa[ai]);
          else  pxa[ai]=ffai;
          spt++;
          pxqa[++pxqs]=((y-1)<<16)+x;
         }
        else if(mode==6)
         {
          if(pxa[ai] == ffbi)
           {
            pxa[ai]=ffai;
            spt++;
            pxqa[++pxqs]=((y-1)<<16)+x;
           }
          else if(K.eq(new K(pxa[ai]),kb,kd))
           {
            pxa[ai]=K.mixi6(ka,kb,pxa[ai]);
            spt++;
            pxqa[++pxqs]=((y-1)<<16)+x;
           }
         }
       }
      if(x<cw-1) //test for inside width
       {
        ai=y*cw+x+1;
        if(mode==1 && pxa[ai] != ffbi && pxa[ai] != ffai)
         {
          pxa[ai]=ffai;
          spt++;
          pxqa[++pxqs]=(y<<16)+x+1;
         }
        else if(mode==2 && pxa[ai]==ffbi)
         {
          pxa[ai]=ffai;
          spt++;
          pxqa[++pxqs]=(y<<16)+x+1;
         }
        else if(mode==3 &&  pxa[ai] != ffbi)
         {
          pxa[ai]=ffai;
          spt++;
          pxqa[++pxqs]=(y<<16)+x+1;
         }
        else if(mode==5 && pxa[ai] != ffbi && pxa[ai] != ffai)
         {
          if(K.eq(new K(pxa[ai]),kb,kd)) pxa[ai]=K.mixi5(ka,kb,pxa[ai]);
          else  pxa[ai]=ffai;
          spt++;
          pxqa[++pxqs]=(y<<16)+x+1;
         }
        else if(mode==6)
         {
          if(pxa[ai]==ffbi)
           {
            pxa[ai]=ffai;
            spt++;
            pxqa[++pxqs]=(y<<16)+x+1;
           }
          else if(K.eq(new K(pxa[ai]),kb,kd))
           {
            pxa[ai]=K.mixi6(ka,kb,pxa[ai]);
            spt++;
            pxqa[++pxqs]=(y<<16)+x+1;
           }
         }
       }
      if(y<ch-1)// test for inside height
       {
        ai=(1+y)*cw+x;
        if(mode==1 && pxa[ai] != ffbi && pxa[ai] != ffai)
         {
          pxa[ai]=ffai;
          spt++;
          pxqa[++pxqs]=((y+1)<<16)+x;
         }
        else if(mode==2 && pxa[ai]== ffbi)
         {
          pxa[ai]=ffai;
          spt++;
          pxqa[++pxqs]=((y+1)<<16)+x;
         }
        else if(mode==3 && pxa[ai] != ffbi)
         {
          pxa[ai]=ffai;
          spt++;
          pxqa[++pxqs]=((y+1)<<16)+x;
         }
        else if(mode==5 && pxa[ai] != ffbi && pxa[ai] != ffai)
         {
          if(K.eq(new K(pxa[ai]),kb,kd)) pxa[ai]=K.mixi5(ka,kb,pxa[ai]);
          else pxa[ai]=ffai;
          //tta("mode 5 and stuff:"+ pxa[ai]);
          spt++;
          pxqa[++pxqs]=((y+1)<<16)+x;
         }
        else if(mode==6)
         {
          if(pxa[ai]== ffbi)
           {
            pxa[ai]=ffai;
            spt++;
            pxqa[++pxqs]=((y+1)<<16)+x;
           }
          else if(K.eq(new K(pxa[ai]),kb,kd))
           {
            pxa[ai]=K.mixi6(ka,kb,pxa[ai]);
            spt++;
            pxqa[++pxqs]=((y+1)<<16)+x;
           }
         }
       }
     }
   }
  Color pgdP(dP dp) { return pgsp(dp.x,dp.y); }
  Color pgP(Point p) { return pgsp(p.x,p.y); }
  int[] pgia(int xx,int yy,int w,int h)
   {
    int xl=xx+getSize().width/2;
    int yl=getSize().height/2-(yy+h);
    pixa=new int[w*h];
    try
     {
      pg=new PixelGrabber(offsim,xl,yl,w,h,pixa,0,w);
      pg.grabPixels();
     }
    catch (InterruptedException e) { e.printStackTrace(); }
    return pixa;
   }
  Color pgsp(int xx,int yy) 
   {
    return pgsp((double)xx,(double)yy);
   }
  Color pgsp(double xx,double yy) 
   {
    int xl=(int)(xx+getSize().width/2);
    int yl=(int)(getSize().height/2-yy);
    pixa=new int[1];
    try
     {
      pg=new PixelGrabber(offsim,xl,yl,1,1,pixa,0,1);
      pg.grabPixels();
     }
    catch (InterruptedException e) { e.printStackTrace(); }
    return new Color(pixa[0]);
   }
  void pssp(double xx,double yy,K k)
   {
    int xl=(int)(xx+getSize().width/2);
    int yl=(int)(getSize().height/2-yy);
    //cw=getSize().width;
    //ch=getSize().height;
    //pxa=new int[cw*ch];
    pixa=new int[1];
    try
     {
      pg=new PixelGrabber(offsim,xl,yl,1,1,pixa,0,1);
      pg.grabPixels();
     }
    catch (InterruptedException e) { e.printStackTrace(); }
    pixa[0]=k.i;
    tta("LC.pssp xl:"+xl+" yl:"+yl+" pixa[0]:"+pixa[0]);
    //Image im=createImage(new MemoryImageSource(cw,ch,pxa,0,cw));
    Image im=createImage(new MemoryImageSource(1,1,pixa,0,1));
    MediaTracker tracker = new MediaTracker(this);
    try
     {
      tracker.waitForID(0);
      tracker.addImage(im,0);
     }
    catch(InterruptedException e) { e.printStackTrace(); }
    if(doffs)
     {
      tformsc(1,-1);
      offsg.drawImage(im,-getSize().width/2,-getSize().height/2,null);
      tformsc(1,-1);
     }
    myRepaint();
   }
  public void attach(LP lp) { add(lp); }
  public void detach(LP lp) { remove(lp); }
  public void setlayer(LP lp,int l) 
   {
    if(l==nflayer) add(lp);
   }
  public void setlayer(LP lp) { setlayer(lp,nflayer++); }
  int getlayer(LP lp) { return 42; }
  public void offsgsetColor(Color c) {  offsg.setColor(c); }
  public void offsgfill(Rectangle r) {  offsg.fill(r); }
  public void offsgdraw(CubicCurve2D c) {  offsg.draw(c); }
  public void offsgdraw(QuadCurve2D c) {  offsg.draw(c); }
  public void offsgdraw(Line2D l) {  offsg.draw(l); }
  public void onsgsetColor(Color c) {  onsg.setColor(c); }
  public void onsgfill(Rectangle r) {  onsg.fill(r); }
  synchronized void setclip(int x,int y,int w,int h) 
   {
    clip=new Rectangle(x,y,w,h);
    onsg.setClip(clip); 
    //offsg.setClip(clip); 
    offsg.setClip(clip); 
    clip=onsg.getClipBounds();
   }
  synchronized void setclip(Graphics2D g2,int x,int y,int w,int h) 
   {
    clip=new Rectangle(x,y,w,h);
    g2.setClip(clip); 
    clip=g2.getClipBounds();
   }
  synchronized void setclip(Rec r) 
   {
    clip=r.rect; 
    onsg.setClip(clip);
    //offsg.setClip(clip); 
    offsg.setClip(clip); 
    clip=onsg.getClipBounds();
   }
  synchronized void setclip(Graphics2D g2,Rec r) 
   {
    clip=r.rect;
    g2.setClip(clip);
    clip=g2.getClipBounds();
   }
  synchronized void getclip()
   {
    clip=onsg.getClipBounds();
   }
  void ed()
   {
    tta1(name+".rec "+Te.Svo(rec));
    tta1(name+".s "+Te.Svo(rec.s));
    tta1(name+".w "+Te.Svo(rec.w));
    tta1(name+".h "+Te.Svo(rec.h));
    tta1(name+".sc "+Te.Svo(sc));
    tta1(name+".xy "+Te.Svo(xy));
    tta1(name+".vp "+Te.Svo(gvp()));
    tta1(name+".clip "+Te.Svo(clip));
    tta1(name+".host "+host);
    tta1(name+".pos "+pos);
    tta1(name+".rm "+rm);
   }
  synchronized void mpdphh(dP dp)
   {
   }
 }
/*
* int rgb = rgbColor.getRGB(); //always returns TYPE_INT_ARGB
int alpha = (rgb >> 24) & 0xFF;
int red =   (rgb >> 16) & 0xFF;
int green = (rgb >>  8) & 0xFF;
int blue =  (rgb      ) & 0xFF;
*/
