package Current.popups.Unfold; import Current.gui.*; import Current.basic.*; import Current.popups.ImageSave.ImageSaver; import Current.manage.*; import Current.plot.Dyadic.DyadicTile; import java.applet.Applet; import java.awt.event.*; import java.awt.*; /*This class draws the unfolding of a word relative to a triangle.*/ public class UnfoldCanvas extends DBCanvas implements MouseListener, MouseMotionListener, ImageSaver { public UnfoldControlCanvas UC; public UnfoldContainer UContainer; public ListenTriangleEnhanced[] T=new ListenTriangleEnhanced[5000]; public CombinatorialTriangle[] CT=new CombinatorialTriangle[5000]; public String W; public Tile ST; public Manager M; public Corridor C; public double A; public Complex Z,B,START,END; public int mode,dyadic; /** Render the help button iff true*/ public boolean show_help=true; // turn true as soon as this component has been painted boolean painted_once=false; public UnfoldCanvas(Manager M, UnfoldContainer ucont) { // add the manager this.M=M; // listen for changes to the word and //listen for changes to the point in the parameter space M.addListener(this); // allow saving of images M.ISM.add("unfolding", this); dyadic=0; UContainer=ucont; C=new Corridor(); A=1.0; B=new Complex(); mode=2; START=new Complex(); END=new Complex(); addMouseListener(this); addMouseMotionListener(this); M.addListener(this); // get the triangle and the word Z=M.getZ(); W=M.getWord(); ST=M.getSelectedTile(); } public boolean shouldBoxAnalyze() { if (ST.getClass().getName()=="DyadicTile") { // check to see if the tile is a DyadicTile DyadicTile DT=(DyadicTile)(ST); if (DT.record_active==1) { return true; } } return false; } public void addUnfoldControlCanvas(UnfoldControlCanvas UC){ this.UC=UC; doUnfold(); } public void mousePressed(MouseEvent e) { e.consume(); MouseData MD=MouseData.process(M,e); START.x=MD.X.x; START.y=MD.X.y; Point X=MD.X; mode=MD.mode; if(mode==2) UC.recolor(X,W.length()); } public void mouseReleased(MouseEvent e) { UC.spineFunction(); } public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mouseClicked(MouseEvent e) { e.consume(); MouseData MD=MouseData.process(M,e); Point X=MD.X; mode=MD.mode; if(mode==1) { double u=1.1; A=A*u; B.x=u*B.x-(u-1)*X.x; B.y=u*B.y-(u-1)*X.y; } if(mode==3) { double u=1.0/1.1; A=A*u; B.x=u*B.x-(u-1)*X.x; B.y=u*B.y-(u-1)*X.y; } repaint(); UC.repaint(); } public void mouseMoved(MouseEvent e) {} public void mouseDragged(MouseEvent e) { e.consume(); MouseData MD=MouseData.process(M,e); Point X=MD.X; mode=MD.mode; END.x=MD.X.x; END.y=MD.X.y; if(mode==2) { B.x=B.x+END.x-START.x; B.y=B.y+END.y-START.y; START.x=END.x; START.y=END.y; } repaint(); UC.repaint(); } /** Added by Pat... * render what needs to be shown for writing images to files * This is exactly like "paint" . */ public void imageSave(Graphics gfx) { if (!painted_once) { painted_once=true; doUnfold(); } Graphics2D g=(Graphics2D) gfx; g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setColor(UContainer.colors.bgColor); g.fillRect(0,0,getWidth(),getHeight()); drawUnfold(g); //drawAnnulus(g); } /**drawing routines*/ public void paint(Graphics gfx) { if (!painted_once) { painted_once=true; doUnfold(); } Graphics2D g=(Graphics2D) gfx; g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setColor(UContainer.colors.bgColor); g.fillRect(0,0,getWidth(),getHeight()); drawUnfold(g); drawAnnulus(g); } public void drawUnfold(Graphics2D g) { int n=W.length(); ListenTriangleEnhanced[] T2=new ListenTriangleEnhanced[n+2]; for(int i=1;i<=n;++i) { T2[i]=T[i].scaleE(B,A); } for(int i=1;i<=n;++i) { T2[i].renderSmooth(g); } } public void setLeaders() { if(W.length()>0) { int v1=CT[C.i1].c[1]; if(v1>W.length()/2) {v1=1;C.i1=1;} UC.v1[1]=1; UC.v1[2]=C.i1; UC.v1[3]=v1; UC.v1[4]=C.ii1; int v2=CT[C.i2].c[2]; if(v2>W.length()/2) {v2=1;C.i2=1;} UC.v2[1]=2; UC.v2[2]=C.i2; UC.v2[3]=CT[C.i2].c[2]; UC.v2[4]=C.ii2; //*an exceptional case*/ int last=Converter.lastDigit(W); if((UC.v1[2]==W.length())&&(UC.v1[4]==last)) {UC.v1[2]=1;UC.v1[3]=1;} if((UC.v2[2]==W.length())&&(UC.v2[4]==last)) {UC.v2[2]=1;UC.v2[3]=1;} T[C.i1].C[5+C.ii1]=UContainer.colors.selectColor; T[C.i2].C[5+C.ii2]=UContainer.colors.selectColor; UC.spineFunction(); } } public void drawAnnulus(Graphics2D g) { if(C.t11) { for(int i=0;i<=n;++i) { if(iT2[i].z[j].x) mx=T2[i].z[j].x; if(my>T2[i].z[j].y) my=T2[i].z[j].y; if(Mxss) s=ss; Complex z1=new Complex(-s*mx+offsetX, Y/2-s*(My+my)/2.0-offsetY); z1=new Complex(-s*mx+offsetX, Y/2+s*(My+my)/2.0+offsetY); for(int i=0;i<=n+1;++i) T3[i]=T2[i].scale(z1,s); return(T3); } public void doUnfold() { if(W!="") { CT=CombinatorialTriangle.unfold(W); CT=CombinatorialTriangle.assignTails(CT); int x=getWidth()-10; int y=getHeight()-10; int n=W.length(); ListenTriangle[] T1=rawUnfold(ListenTriangle.init(Z),W); ListenTriangle[] T2=rotateHorizontal(T1,n); ListenTriangle[] T3=fitRectangle(x,y,5,5,T2,n); for(int i=0;i<=n+2;++i) T[i]=new ListenTriangleEnhanced(); for(int i=0;i<=n+1;++i) { T[i]=T[i].enhance(T3[i]); if(T[i].on[0]==0) T[i].C[0]=UContainer.colors.triColor; for(int q=1;q<=8;++q) {if(T[i].on[q]==0) T[i].C[q]=UContainer.colors.edgeColor;} } C.compute(W,T3,n); if(UC.L[1].on==1) setLeaders(); } } /**this routine is called by the PolynomialCanvas window*/ public void mcbReceive(VertexPair pair) { //record vertex pairs UC.v1[1]=pair.o1; UC.v1[2]=pair.e1; UC.v2[1]=pair.o2; UC.v2[2]=pair.e2; UC.CV=pair; doUnfold(); //convert vertex pair to names of vertices int[] m=Converter.typeToCoords(CT[pair.e1],pair.o1); UC.v1[3]=m[1]; m=Converter.typeToCoords(CT[pair.e2],pair.o2); UC.v2[3]=m[1]; //compute spine path UC.spineFunction(); //repaint UC.repaint(); UC.F.repaint(); repaint(); } /* Set the point in the parameter space used by this object */ public void mcbReceive(Parameter p){ Z=p.getZ(); doUnfold(); UC.doSpine(); repaint(); UC.repaint(); } public void mcbReceive(Tile st) { //pick out the word W=st.getStringWord(); //pick out the Tile ST=st; doUnfold(); repaint(); UC.repaint(); if (ST instanceof DyadicTile) { // check to see if the tile is a DyadicTile DyadicTile DT=(DyadicTile)(ST); if (DT.record_active==1) { UContainer.showBoxAnalyzer(); } else { UContainer.hideBoxAnalyzer(); } }else { UContainer.hideBoxAnalyzer(); } } }