package Current.popups.AnimatedWord; import Current.*; import Current.basic.*; import Current.manage.*; import java.awt.*; import java.awt.geom.*; import java.awt.event.*; import java.io.Serializable; public class AnimatedWordCanvas extends Canvas implements MouseListener { Manager M; AnimatedWordContainer acont; AnimatedWordControlCanvas AC; public ColorSettings colors; VertexPair CV; String word; Complex[] Z=new Complex[5000]; Complex[] B=new Complex[2]; Complex[] v=new Complex[4]; int location=0; ListenSquare[] L=new ListenSquare[20]; Animator A; int speed=1; int cut1,cut2; ListenSquare[] W=new ListenSquare[5000]; Color[] COL=new Color[10]; int start,stop; String special; Image offscreen=null; int width=0; int height=0; double size; int shape=1; public AnimatedWordCanvas(Manager M,AnimatedWordContainer acont) { this.M=M; this.acont=acont; M.addListener(this); colors=(ColorSettings)(M.settings.getDefault(ColorSettings.class)); word=M.getWord(); setSize(300,300); addMouseListener(this); A=new Animator(); try {A.start();} catch(Exception e){} start=0;stop=0; newWordReceived(); } public void addAnimatedWordControlCanvas(AnimatedWordControlCanvas AC){ this.AC=AC; } public static class ColorSettings implements Serializable { public String getTitle() { return "word window colors"; } public Color bgColor=new Color(0,0,0); public static final String bgColor_name="background color"; public Color hexColor=new Color(0,0,110); public static final String hexColor_name="hexagon color"; public Color gridColor=new Color(0,50,50); public static final String gridColor_name="grid color"; public Color pathColor=new Color(200,0,200); public static final String pathColor_name="path color"; public Color onColor=new Color(255,255,0); public static final String onColor_name="path highlight color"; public Color borderColor=new Color(80,80,80); public static final String borderColor_name="border color"; public Color digit1Color=new Color(0,0,255); public static final String digit1Color_name="digit 1 color"; public Color digit2Color=new Color(65,110,255); public static final String digit2Color_name="digit 2 color"; public Color digit3Color=new Color(120,160,255); public static final String digit3Color_name="digit 3 color"; public Color traceColor=Color.white; public static final String traceColor_name="trace color"; public Color textColor=Color.white; public static final String textColor_name="text color"; public Color leftColor=new Color(0,200,0); public static final String leftColor_name="left marker color"; public Color rightColor=new Color(240,0,0); public static final String rightColor_name="right marker color"; } public void newWordReceived() { v[1]=new Complex(); v[2]=new Complex(); v[3]=new Complex(); if(shape==0) setVectorsRegular(); if(shape==1) setVectorsIntegral(); Z=WordIcon.rawPath(word,v); B=getLocation(word.length()); if(Math.min(start,stop)>word.length()) {start=0;stop=0;} try {createBackground();} catch(Exception e){} repaint(); } public Complex spineCompute(int i,int sp) { Complex[] V=new Complex[4]; for(int ii=0;ii<=3;++ii) V[ii]=Complex.times(v[ii],new Complex(.5,0)); if(i%2==1) return(Complex.plus(Z[i],V[sp])); return(Complex.minus(Z[i],V[sp])); } public void setVectorsIntegral() { v[0]=new Complex(0,0); v[1]=new Complex(1,-2); v[2]=new Complex(-2,1); v[3]=new Complex(1,1); } public void setVectorsRegular() { double pi=Math.PI; double arg1=pi/6+pi/2; double arg2=arg1+2*pi/3; double arg3=arg2+2*pi/3; v[0]=new Complex(0,0); v[1]=new Complex(Math.cos(arg1),Math.sin(arg1)); v[2]=new Complex(Math.cos(arg2),Math.sin(arg2)); v[3]=new Complex(Math.cos(arg3),Math.sin(arg3)); } public void createBackground() { COL[0]=colors.digit1Color; COL[1]=colors.digit2Color; COL[2]=colors.digit3Color; if (offscreen!=null) offscreen.flush(); width=getWidth(); height=getHeight(); offscreen = createImage(width, height); Graphics gfx = offscreen.getGraphics(); Graphics2D g=(Graphics2D) gfx; g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON); g.setColor(colors.bgColor); g.fillRect(0,0,width,height); drawBgGrid(g); for(int i=0;i<=3;++i) { if(AC.CHOICE[2][i].on==1) drawWordMain(g,i); } for(int i=0;i<=3;++i) { if(AC.CHOICE[0][i].on==1) subwordDraw(g,i); } drawOrigin(g); makeBorder(g); L[6]=new ListenSquare(width-15,3,12,12,colors.textColor); L[6].infoRender(g); wordLengthRender(g); makeCutsMain(g); g.dispose(); gfx.dispose(); } public void paint(Graphics gfx) { Graphics2D g=(Graphics2D) gfx; g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); if ((offscreen==null)||(width!=getWidth())||(height!=getHeight())) createBackground(); gfx.drawImage(offscreen, 0, 0, this); for(int i=0;i<=3;++i) { if(AC.CHOICE[1][i].on==1) wordDrawExtra(g,i); } if(AC.CHOICE[1][0].on+AC.CHOICE[1][1].on+AC.CHOICE[1][2].on+AC.CHOICE[1][3].on>0) makeCutsExtra(g); g.setColor(Color.white); } public void wordLengthRender(Graphics2D g) { g.setColor(colors.textColor); g.setFont(new Font("Helvetica",Font.PLAIN,10)); Integer N=new Integer(word.length()); g.drawString(N.toString(),4,12); } public void makeCutsMain(Graphics2D g) { COL[0]=colors.digit1Color; COL[1]=colors.digit2Color; COL[2]=colors.digit3Color; Color[] COL2=new Color[2]; COL2[0]=colors.bgColor; COL2[1]=colors.onColor; int h=getHeight(); int w=getWidth(); int n=word.length(); n=n/2; cut1=(h*n)/(w+h); if(cut1==0) cut1=1; if(cut1==n) cut1=n-1; cut2=n-cut1; double w1=(w-56.0)/cut2; double h1=(h-56.0)/cut1; double tab1,tab2; tab1=16;tab2=12; int highlight=0; for(int i=0;i<2*cut1+2*cut2;++i) { highlight=0; if(i=start)&&(i<=stop)) {highlight=1;} if((start>stop+1)&&(i>=start)) {highlight=1;} if((start>stop+1)&&(i<=stop)) {highlight=1;} int digit=Converter.getDigit(word,i+1); W[i]=new ListenSquare(28+w1*i,tab1,w1,tab2,COL[digit]); W[i].renderYLayer(g,COL[digit],COL2[highlight]); Integer DIGIT=new Integer(digit+1); g.setColor(colors.textColor); g.setFont(new Font("Helvetica",Font.PLAIN,8)); g.drawString(DIGIT.toString(),(float)(W[i].x+.5*W[i].w-2),(float)(W[i].y-2)); } if((i>=cut2)&&(i=start)&&(i<=stop)) {highlight=1;} if((start>stop+1)&&(i>=start)) {highlight=1;} if((start>stop+1)&&(i<=stop)) {highlight=1;} int digit=Converter.getDigit(word,i+1); W[i]=new ListenSquare(w-28,28+h1*ii,tab2,h1,COL[digit]); W[i].renderXLayer(g,COL2[highlight],COL[digit]); Integer DIGIT=new Integer(digit+1); g.setColor(colors.textColor); g.setFont(new Font("Helvetica",Font.PLAIN,8)); g.drawString(DIGIT.toString(),(float)(W[i].x+15),(float)(W[i].y+.5*W[i].h+4)); } if((i>=cut1+cut2)&&(i=start)&&(i2<=stop)) {highlight=1;} if((start>stop+1)&&(i2>=start)) {highlight=1;} if((start>stop+1)&&(i2<=stop)) {highlight=1;} int digit=Converter.getDigit(word,i2+1); W[i2]=new ListenSquare(28+w1*ii,h-28,w1,tab2,COL[digit]); W[i2].renderYLayer(g,COL2[highlight],COL[digit]); Integer DIGIT=new Integer(digit+1); g.setColor(colors.textColor); g.setFont(new Font("Helvetica",Font.PLAIN,8)); g.drawString(DIGIT.toString(),(float)(W[i2].x+.5*W[i2].w-2),(float)(W[i2].y+22)); } if(i>=cut1+2*cut2) { int ii=i-cut1-2*cut2; int i2=3*cut1+4*cut2-i-1; if((start<=stop)&&(i2>=start)&&(i2<=stop)) {highlight=1;} if((start>stop+1)&&(i2>=start)) {highlight=1;} if((start>stop+1)&&(i2<=stop)) {highlight=1;} int digit=Converter.getDigit(word,i2+1); W[i2]=new ListenSquare(tab1,28+h1*ii,tab2,h1,COL[digit]); W[i2].renderXLayer(g,COL[digit],COL2[highlight]); Integer DIGIT=new Integer(digit+1); g.setColor(colors.textColor); g.setFont(new Font("Helvetica",Font.PLAIN,8)); g.drawString(DIGIT.toString(),(float)(W[i2].x-8),(float)(W[i2].y+.5*W[i2].h+4)); } } if(Math.abs(start-stop)>1) { int i=start; int digit=Converter.getDigit(word,i+1); if(i=cut2)&&(i=cut1+cut2)&&(i=cut1+2*cut2) W[i].renderXLayer(g,COL[digit],colors.leftColor); i=stop; digit=Converter.getDigit(word,i+1); if(i=cut2)&&(i=cut1+cut2)&&(i=cut1+2*cut2) W[i].renderXLayer(g,COL[digit],colors.rightColor); } } public void makeCutsExtra(Graphics2D g) { g.setStroke(new BasicStroke((float)(1.0))); W[location].mainRenderSmooth(g,colors.traceColor,Color.black); AC.I[0].val=location; } public void makeBorder(Graphics2D g) { g.setStroke(new BasicStroke((float)(1.0))); g.setColor(colors.borderColor); g.fillRect(0,0,width,28); g.fillRect(0,0,28,height); g.fillRect(width-28,0,28,height); g.fillRect(0,height-28,width,28); g.setColor(Color.blue); g.drawRect(28,28,width-56,height-56); } public void computeSize() { int w=this.getWidth()-60; int h=this.getHeight()-60; int n=word.length(); int ww=(int)(w*1.0/B[0].x); int hh=(int)(h*1.0/B[0].y); size=ww; if(size>hh) size=hh; } /**this routine finds the bounding box of the raw path, and also computes the center of the b.box. The box is wide enough to accomodate the spines.*/ public Complex[] getLocation(int n) { double mx,my,Mx,My; mx=1000000; my=1000000; Mx=-1000000; My=-1000000; for(int i=0;iV.x) mx=V.x; if(MxV.y) my=V.y; if(My=start)&&(i<=stop)) on=1; if((start>stop+1)&&(i>=start)) on=1; if((start>stop+1)&&(i<=stop)) on=1; g.setStroke(new BasicStroke((float)(2.0))); path.moveTo(x1,y1); path.lineTo(x2,y2); g.setColor(COL[choice]); g.draw(path); } g.translate(-30,-30); } public void subwordDraw(Graphics2D g,int choice) { COL[0]=colors.pathColor; COL[1]=colors.digit1Color; COL[2]=colors.digit2Color; COL[3]=colors.digit3Color; int w=this.getWidth()-60; int h=this.getHeight()-60; int n=word.length(); computeSize(); double xx=B[1].x; double yy=B[1].y; g.setColor(Color.white); float x1,y1,x2,y2; g.translate(30,30); for(int i=0;i=start)&&(i<=stop)) on=1; if((start>stop+1)&&(i>=start)) on=1; if((start>stop+1)&&(i<=stop)) on=1; g.setStroke(new BasicStroke((float)(2.0))); path.moveTo(x1,y1); path.lineTo(x2,y2); if(on==1) { g.setColor(colors.onColor); g.draw(path); } //special case if((start==stop+1)&&(i==start)) { path.reset(); System.out.println("here I am " +stop); path.moveTo(x1+1,y1+1); path.lineTo(x1-1,y1+1); path.lineTo(x1-1,y1-1); path.lineTo(x1+1,y1-1); path.closePath(); g.setColor(colors.onColor); g.draw(path); } } if(Math.abs(start-stop)>1) { GeneralPath path=new GeneralPath(); int i=start; Complex Z1=spineCompute(i,choice); Complex Z2=spineCompute(i+1,choice); x1=(float)((Z1.x-xx)*size+w/2); y1=(float)((Z1.y-yy)*size+h/2); x2=(float)((Z2.x-xx)*size+w/2); y2=(float)((Z2.y-yy)*size+h/2); g.setStroke(new BasicStroke((float)(2.0))); path.moveTo(x1,y1); path.lineTo(x2,y2); g.setColor(colors.leftColor); g.draw(path); path=new GeneralPath(); i=stop; Z1=spineCompute(i,choice); Z2=spineCompute(i+1,choice); x1=(float)((Z1.x-xx)*size+w/2); y1=(float)((Z1.y-yy)*size+h/2); x2=(float)((Z2.x-xx)*size+w/2); y2=(float)((Z2.y-yy)*size+h/2); g.setStroke(new BasicStroke((float)(2.0))); path.moveTo(x1,y1); path.lineTo(x2,y2); g.setColor(colors.rightColor); g.draw(path); } g.translate(-30,-30); g.setStroke(new BasicStroke((float)(1.0))); } public double distanceToPath(Point X) { int h=this.getHeight()-60; int w=this.getWidth()-60; double xx=B[1].x; double yy=B[1].y; double dist=100000000; Complex XX=new Complex(X.x-30,X.y-30); for(int i=0;itest) dist=test; } return(dist); } public void markerSwap() { int temp=stop; stop=start; start=temp; } public void wordDrawExtra(Graphics2D g,int ch) { int w=this.getWidth()-60; int h=this.getHeight()-60; int n=word.length(); double xx=B[1].x; double yy=B[1].y; float x1,y1,x2,y2; g.translate(30,30); for(int ii=location;iistop+1) { special=word.substring(start+1,word.length())+word.substring(0,stop+2); } M.mcbSend(new McbSubword(special)); } class Animator extends Thread { public int count; int delay=512; public void run() { count=0; while(delay!=Integer.MAX_VALUE) { int r=word.length(); if(speed>0) ++count; if(speed<0) --count; count=(count+r)%r; try {sleep(delay);} catch(Exception e) {} if(speed!=0) location=count; repaint(); AC.repaint(); yield(); } } } public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public synchronized void mouseClicked(MouseEvent e) { ListenSquare frame=new ListenSquare(32,32,this.getWidth()-64,this.getHeight()-164,Color.white); MouseData MD=MouseData.process(M,e); int mode=MD.mode; Point X=MD.X; int test=-1; for(int i=0;i