HandleReductionPanel.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /* File : HandleReductionPanel.java
  2. * Program : Handle Reduction Aimation - Applet
  3. * By Jean Fromentin <jfroment@info.unicaen.fr>
  4. * Copyright 2008 Jean Fromentin
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. import java.awt.Color;
  17. import java.awt.Graphics;
  18. import java.awt.Image;
  19. import javax.swing.JLabel;
  20. import javax.swing.JOptionPane;
  21. import javax.swing.JPanel;
  22. public class HandleReductionPanel extends JPanel {
  23. private JLabel currentBraidWord;
  24. private String braidWord="";
  25. private boolean braidWordSet=false;
  26. public BraidDrawing braidDrawing;
  27. public String currentWord="";
  28. public boolean working=false;
  29. private int animationStep;
  30. private float parameterAnimationStep2=1;
  31. private float parameterAnimationStep4;
  32. private float parameterAnimationStep5;
  33. private boolean continuousAnimation=true;
  34. private Image offScreenBuffer;
  35. public HandleReductionPanel(){
  36. setBackground(new Color(220,220,220));
  37. setVisible(true);
  38. }
  39. public void add(char c){
  40. braidWord+=c;
  41. writeBraidWord(braidWord);
  42. }
  43. public void remove(){
  44. if(braidWord.length()!=0){
  45. braidWord=braidWord.substring(0,braidWord.length()-1);
  46. writeBraidWord(braidWord);
  47. }
  48. }
  49. public void clear(){
  50. braidWord="";
  51. writeBraidWord(braidWord);
  52. }
  53. public void writeBraidWord(String buffer){
  54. braidWord=buffer;
  55. braidDrawing=new BraidDrawing(braidWord);
  56. animationStep=0;
  57. updateCurrentBraidWord();
  58. braidWordSet=true;
  59. update(getGraphics());
  60. }
  61. public void updateCurrentBraidWord(){
  62. currentWord=braidDrawing.word();
  63. }
  64. public String getCurrentWord(){
  65. return braidWord;
  66. }
  67. public void paint(Graphics g){
  68. if(!braidDrawing.isDraw){
  69. update(g);
  70. braidDrawing.isDraw=true;
  71. }
  72. else{
  73. g.drawImage(offScreenBuffer,0,0,this);
  74. }
  75. }
  76. public void refresh(){
  77. update(getGraphics());
  78. }
  79. public void update(Graphics g) {
  80. if(braidWordSet){
  81. Graphics gr;
  82. if (offScreenBuffer==null||(!(offScreenBuffer.getWidth(this)==getBounds().width && offScreenBuffer.getHeight(this)==getBounds().height))){
  83. offScreenBuffer=createImage(getBounds().width,getBounds().height);
  84. }
  85. gr=offScreenBuffer.getGraphics();
  86. braidDrawing.calcXPos(gr,getBounds(),animationStep,parameterAnimationStep2,parameterAnimationStep4,parameterAnimationStep5);
  87. braidDrawing.draw(gr);
  88. g.drawImage(offScreenBuffer,0,0,this);
  89. }
  90. }
  91. public int reduce(){
  92. if(!braidWordSet){
  93. JOptionPane.showMessageDialog(null,"Vous devez d'abord saisir un mot de tresse ","Erreur",JOptionPane.ERROR_MESSAGE);
  94. return 0;
  95. }
  96. else if(animationStep==0){
  97. braidDrawing.handleNormalDraw=true;
  98. braidDrawing.findHandle();
  99. if(braidDrawing.handleStrand==0){
  100. return 0;
  101. }
  102. braidDrawing.firstDraw=true;
  103. braidDrawing.insertTrivials();
  104. braidDrawing.trivialHeight=0;
  105. animationStep=1;
  106. }
  107. else if(animationStep==1){
  108. if(braidDrawing.trivialHeight>=1){
  109. braidDrawing.trivialHeight=1;
  110. parameterAnimationStep2=1;
  111. animationStep=2;
  112. }
  113. else{
  114. braidDrawing.trivialHeight+=0.1;
  115. }
  116. }
  117. else if(animationStep==2){
  118. braidDrawing.handleNormalDraw=false;
  119. if(parameterAnimationStep2<0){
  120. parameterAnimationStep2=0;
  121. animationStep=3;
  122. }
  123. else{
  124. parameterAnimationStep2-=(float)(0.05);
  125. }
  126. }
  127. else if(animationStep==3){
  128. parameterAnimationStep4=0;
  129. animationStep=4;
  130. }
  131. else if(animationStep==4){
  132. if(parameterAnimationStep4>=1){
  133. braidDrawing.removeHandle();
  134. parameterAnimationStep2=1;
  135. parameterAnimationStep5=1;
  136. animationStep=5;
  137. }
  138. else{
  139. parameterAnimationStep4+=(float)0.05;
  140. }
  141. }
  142. else if(animationStep==5){
  143. if(parameterAnimationStep5<=0){
  144. parameterAnimationStep5=0;
  145. animationStep=6;
  146. }
  147. else{
  148. parameterAnimationStep5-=(float)0.05;
  149. }
  150. }
  151. else if(animationStep==6){
  152. if(braidDrawing.trivialHeight<=0){
  153. braidDrawing.trivialHeight=0;
  154. animationStep=7;
  155. }
  156. else{
  157. braidDrawing.trivialHeight-=0.1;
  158. }
  159. }
  160. else if(animationStep==7){
  161. braidDrawing.removeTrivials();
  162. braidDrawing.handleNormalDraw=true;
  163. braidDrawing.handleStrand=0;
  164. animationStep=0;
  165. braidDrawing.handleBeginIndice=0;
  166. braidDrawing.handleEndIndice=0;
  167. return 2;
  168. }
  169. else{
  170. return 0;
  171. }
  172. braidWord=braidDrawing.word();
  173. return 1;
  174. }
  175. }