import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.net.*; import java.io.*; import java.applet.*; class Node { //********************************************* //* * //* This class is needed for the class list. * //* A Node looks like an element of the list. * //* * //********************************************* private int value; //Value of the Node private Node next; //The next Node in the list private Node previous; //The previous Node in the list public Node(){ value=0; } public Node(int _value){ value=_value; } public void setValue(int _value){ value=_value; } public void setPrevious(Node _previous){ previous=_previous; } public void setNext(Node _next){ next=_next; } public int getValue(){ return value; } public Node getPrevious(){ return previous; } public Node getNext(){ return next; } } class List { //****************************************** //* * //* This class decribe a bichained list. * //* I don't use the linkedList of java * //* because it need use of complex Object. * //* * //****************************************** private Node first; //The first Node of the list private Node last; //The last Node of the list public Node current; //A Node we play the game of an iterator public int length; //The length of the list public List(){ length=0; } public Node getFirst(){ return first; } public Node getLast(){ return last; } public void initCurrent(){ current=first; } public void setCurrent(Node _current){ current=_current; } public Node getCurrent(){ return current; } public int isEnd(){ if(current==last){ return 1; } else{ return 0; } } public void shift(){ // --------------------------------------- //| Change the current node with the next | // --------------------------------------- current=current.getNext(); } public void addLast(int value){ // ---------------------------------------------------- //| Add a Node of value 'value' at the end of the list | // ---------------------------------------------------- Node node=new Node(value); if(length==0){ first=node; last=node; } else{ last.setNext(node); node.setPrevious(last); last=node; } length++; } public Node addBefore(int value){ // ----------------------------------------------------------------- //| Add a Node of value 'value' before the current Node of the list | //| and return the new Node | // ----------------------------------------------------------------- Node node=new Node(value); if(current==first){ first=node; node.setNext(current); current.setPrevious(node); } else { Node temp=current.getPrevious(); temp.setNext(node); node.setPrevious(temp); current.setPrevious(node); node.setNext(current); } length++; return node; } public Node addAfter(int value){ // ---------------------------------------------------------------- //| Add a Node of value 'value' after the current Node of the list | //| and return the new Node | // ---------------------------------------------------------------- Node node=new Node(value); if(current==last){ last=node; node.setPrevious(current); current.setNext(node); } else{ Node temp=current.getNext(); temp.setPrevious(node); node.setNext(temp); current.setNext(node); node.setPrevious(current); } length++; return node; } public void remove(Node node){ // ---------------------------------- //| Remove the Node node of the list | // ---------------------------------- if(length!=0){ if(length==1){ length=0; } else{ if(node==first){ first=node.getNext(); node=first; } else if(node==last){ last=node.getPrevious(); current=last; } else{ (node.getPrevious()).setNext(node.getNext()); (node.getNext()).setPrevious(node.getPrevious()); } length--; } } } public int value(){ return current.getValue(); } public int length(){ return length; } } public class Diagram extends JApplet implements ActionListener{ //******************************************************* //* * //* This class represent the window of the application. * //* * //******************************************************* public static final long serialVersionUID = 0; private Panel panel; //Will draw graphics on the panel private JTextField textField; //An area in which will enter a braid word public void init(){ JLabel label=new JLabel("Enter a word :"); label.setBounds(0,0,100,20); add(label); textField=new JTextField(10); textField.addActionListener(this); //Active the action listener of textField.setBounds(100,0,380,20); add(textField); panel=new Panel(); panel.setBackground(Color.darkGray); panel.setBounds(0,20,480,600); add(panel); add(new JLabel("")); } public void actionPerformed (ActionEvent event){ // ---------------------------------------------------------- //| Specify what the action on the element of the windows do | // ---------------------------------------------------------- if(event.getSource()==textField){ StringBuffer word=new StringBuffer(event.getActionCommand()); panel.writeBraidWord(word); } } public String getAppletInfo() { return "Handle reduction algorithm."; } } class Panel extends JPanel{ //************************************************ //* * //* This class is the key class of this program. * //* It contains algoritms and drawing tools. * //* * //************************************************ public static final long serialVersionUID = 0; //=====> Braid variable private StringBuffer braidWord; //The initial braid word private int braidWordSet=0; private List braid; //The braid word as a list //=====> Strand variable private int strandNumber=5; //The number of strand private float strandStep; //The distance between two strand private float strandWidth; //The width of a strand private float strandLeft; //The x position of the leftmost strand private float[] xPos; //The x postion of strands private float[] xPosNext; //The next x position of strands private float yUp; //The y position of the first element of the braid //======> Color variable private Color[] strandColor; //The color of different strand private Color handleColor=Color.black; //The color of the handle private Color originalHandleStrandColor; //The original color of the handle strand //======> Way variable private GeneralPath[] continuousWay; //The currant continuous way of strands //======> Double Buffering variable private Image offScreenBuffer; //A buffer in wich we draw and after drawing we will show this buffer private int isDraw; //For not repeat drawing // ================= //I I //I Braid functions I //I I // ================= private void initBraid(){ // ----------------------------------------------------- //| Iniliasition of braid as a list and of strandNumber | // ----------------------------------------------------- int length; int value; char letter; strandNumber=2; value=0; braid=new List(); length=braidWord.length(); //get the length of the braid word braid.addLast(0); //add a trivial at the begin for(int i=0;i='a' && letter<='z'){ //if we have a lower case letter value=(int)(letter-'a')+1; //value is 1 for 'a', 2 for 'b', ... if(value+1>strandNumber){ //update the strand number strandNumber=value+1; } } else if(letter>='A' && letter<='Z'){ //if we have an upper case letter value=-(int)(letter-'A')-1; //value is -1 for 'A', -2 for 'B', ... if(-value+1>strandNumber){ //update the strand number strandNumber=-value+1; } } else{ value=0; } braid.addLast(value); //add the value at the list of the braid } braid.addLast(0); //add a trivial at th end } public void writeBraidWord(StringBuffer buffer){ // ----------------------------------------------------- //| Initialise the braid word and some other parameters | //------------------------------------------------------ braidWord=buffer; initBraid(); //initialise the braid list isDraw=0; //we draw a new image braidWordSet=1; update(super.getGraphics()); //update draw } // ================= //I I //I Color functions I //I I // ================= public void initStrandColor(){ // --------------------------- //| Init the color of strands | // --------------------------- strandColor=new Color[strandNumber]; //Make the table of strand color for(int strand=1;strandb){ return a; } else{ return b; } } public double square(double a){ return a*a; } // =================== //I I //I Position function I //I I // =================== public void calcXPosNext(){ // ---------------------------------------- //| Compute the nex x position of strands. | //| It's a key function for animation. | // ---------------------------------------- for(int strand=0;strand