Components.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /* File : Components.java
  2. * Program : Handle Reduction Animation - Applet
  3. * By Jean Fromentin <jean.froment@gmail.com>
  4. * Copyright 2000 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.Dimension;
  17. import java.awt.Insets;
  18. import java.awt.BorderLayout;
  19. import java.awt.Color;
  20. import java.awt.FlowLayout;
  21. import java.awt.GridLayout;
  22. import java.awt.GridBagConstraints;
  23. import java.awt.GridBagLayout;
  24. import java.awt.event.ActionListener;
  25. import java.awt.event.ActionEvent;
  26. import java.awt.event.AdjustmentEvent;
  27. import javax.swing.Action;
  28. import javax.swing.JButton;
  29. import javax.swing.JFrame;
  30. import javax.swing.JDialog;
  31. import javax.swing.JRadioButton;
  32. import javax.swing.JPanel;
  33. import javax.swing.JLabel;
  34. import javax.swing.JTextField;
  35. import javax.swing.JOptionPane;
  36. import javax.swing.JScrollBar;
  37. import javax.swing.ButtonGroup;
  38. import javax.swing.Timer;
  39. public class Components extends JPanel{
  40. private HandleReductionPanel handleReductionPanel;
  41. private JButton draw,compare,reduce,controls,controlsApply,controlsCancel;
  42. private JRadioButton continuous,stepByStep;
  43. private JTextField wordField,firstWordField,secondWordField;
  44. private JScrollBar delayScrollBar;
  45. private int pauseDelay,newPauseDelay;
  46. private JLabel wordLabel,delayLabel;
  47. private JDialog controlsFrame,compareFrame,drawFrame;
  48. private Listener listener;
  49. private Timer animationTimer;
  50. private Boolean reducing,continuousAnimation,comparing;
  51. private String word,word1,word2;
  52. public Components(){
  53. handleReductionPanel=new HandleReductionPanel();
  54. draw=new JButton("Draw");
  55. compare=new JButton("Compare");
  56. reduce=new JButton("Reduce");
  57. controls=new JButton("Controls");
  58. wordLabel=new JLabel("Empty word");
  59. //Layout
  60. setLayout(new BorderLayout());
  61. add(wordLabel,BorderLayout.NORTH);
  62. add(handleReductionPanel,BorderLayout.CENTER);
  63. JPanel buttonPanel=new JPanel();
  64. add(buttonPanel,BorderLayout.SOUTH);
  65. buttonPanel.setLayout(new FlowLayout());
  66. buttonPanel.add(draw);
  67. buttonPanel.add(compare);
  68. buttonPanel.add(reduce);
  69. buttonPanel.add(controls);
  70. //Action listenner
  71. listener=new Listener(this);
  72. draw.addActionListener(listener);
  73. compare.addActionListener(listener);
  74. reduce.addActionListener(listener);
  75. controls.addActionListener(listener);
  76. //Action command
  77. draw.setActionCommand("draw");
  78. compare.setActionCommand("compare");
  79. reduce.setActionCommand("reduce");
  80. controls.setActionCommand("controls");
  81. //States
  82. reducing=false;
  83. comparing=false;
  84. continuousAnimation=true;
  85. reduce.setEnabled(false);
  86. pauseDelay=10;
  87. createTimer();
  88. }
  89. //Buttons state
  90. public void setButtonsState(Boolean drawState,Boolean compareState,Boolean reduceState,Boolean controlsState){
  91. draw.setEnabled(drawState);
  92. compare.setEnabled(compareState);
  93. reduce.setEnabled(reduceState);
  94. controls.setEnabled(controlsState);
  95. }
  96. //Actions
  97. public void updateWord(){
  98. wordLabel.setText("Current word : "+handleReductionPanel.getCurrentWord());
  99. wordLabel.updateUI();
  100. }
  101. public void draw(){
  102. drawFrame=new JDialog();
  103. drawFrame.setResizable(false);
  104. drawFrame.setTitle("Draw braid");
  105. //First word
  106. JLabel wordLabel=new JLabel(" Braid word : ");
  107. wordField=new JTextField();
  108. wordField.setPreferredSize(new Dimension(200, 28));
  109. //Buttons
  110. JButton drawCancel,drawApply;
  111. drawCancel=new JButton("Cancel");
  112. drawApply=new JButton("Draw");
  113. //Action listenner
  114. drawCancel.addActionListener(listener);
  115. drawApply.addActionListener(listener);
  116. //Action command
  117. drawCancel.setActionCommand("drawCancel");
  118. drawApply.setActionCommand("drawApply");
  119. //Layout
  120. GridBagLayout gb=new GridBagLayout();
  121. GridBagConstraints gbc=new GridBagConstraints();
  122. drawFrame.setLayout(gb);
  123. gbc.anchor = GridBagConstraints.EAST;
  124. gbc.insets = new Insets(5, 5, 5, 5);
  125. gbc.gridx=0;
  126. gbc.gridy=0;
  127. gbc.gridwidth=1;
  128. gb.setConstraints(wordLabel,gbc);
  129. drawFrame.add(wordLabel);
  130. gbc.gridx=1;
  131. gbc.gridy=0;
  132. gbc.gridwidth=2;
  133. gb.setConstraints(wordField,gbc);
  134. drawFrame.add(wordField);
  135. gbc.gridx=1;
  136. gbc.gridy=1;
  137. gbc.gridwidth=1;
  138. gb.setConstraints(drawCancel,gbc);
  139. drawFrame.add(drawCancel);
  140. gbc.gridx=2;
  141. gbc.gridy=1;
  142. gbc.gridwidth=1;
  143. gb.setConstraints(drawApply,gbc);
  144. drawFrame.add(drawApply);
  145. drawFrame.pack();
  146. drawFrame.setVisible(true);
  147. /*JOptionPane dialog = new JOptionPane();
  148. String word = dialog.showInputDialog(this,"Enter the braid word to draw :");
  149. handleReductionPanel.writeBraidWord(word);
  150. updateWord();
  151. setButtonsState(true,true,true,true);
  152. reduce.setText("Reduce");
  153. reduce.updateUI();*/
  154. }
  155. public void drawBraid(){
  156. word=wordField.getText();
  157. comparing=false;
  158. handleReductionPanel.writeBraidWord(word);
  159. updateWord();
  160. setButtonsState(true,true,true,true);
  161. reduce.setText("Reduce");
  162. reduce.updateUI();
  163. }
  164. public void closeDraw(){
  165. drawFrame.dispose();
  166. }
  167. public void reduce(){
  168. if(reducing){
  169. animationTimer.stop();
  170. reducing=false;
  171. setButtonsState(true,true,true,true);
  172. reduce.setText("Continue");
  173. reduce.updateUI();
  174. }
  175. else{
  176. reducing=true;
  177. setButtonsState(false,false,true,false);
  178. reduce.setText("Pause");
  179. reduce.updateUI();
  180. animationTimer.start();
  181. }
  182. }
  183. public void controls(){
  184. controlsFrame=new JDialog();
  185. controlsFrame.setResizable(false);
  186. controlsFrame.setTitle("Controls");
  187. //Animation mode
  188. JLabel modeLabel=new JLabel(" Animation : ");
  189. continuous=new JRadioButton("Continuous",continuousAnimation);
  190. stepByStep=new JRadioButton("Step-by-step",!continuousAnimation);
  191. ButtonGroup group = new ButtonGroup();
  192. group.add(continuous);
  193. group.add(stepByStep);
  194. //Pause delay
  195. newPauseDelay=pauseDelay;
  196. JLabel pauseLabel=new JLabel(" Pause delay : ");
  197. delayLabel=new JLabel(" "+newPauseDelay+" ms ");
  198. delayScrollBar=new JScrollBar(JScrollBar.HORIZONTAL,newPauseDelay,1,0,100);
  199. delayScrollBar.setPreferredSize(new Dimension(150,20));
  200. //Buttons
  201. controlsApply=new JButton("Apply");
  202. controlsCancel=new JButton("Cancel");
  203. //Layout
  204. GridBagLayout gb=new GridBagLayout();
  205. GridBagConstraints gbc=new GridBagConstraints();
  206. controlsFrame.setLayout(gb);
  207. gbc.anchor = GridBagConstraints.WEST;
  208. gbc.insets = new Insets(5, 5, 5, 5);
  209. gbc.gridx=0;
  210. gbc.gridy=0;
  211. gbc.gridwidth=1;
  212. gb.setConstraints(pauseLabel,gbc);
  213. controlsFrame.add(pauseLabel);
  214. gbc.gridx=1;
  215. gbc.gridy=0;
  216. gbc.gridwidth=2;
  217. gb.setConstraints(delayScrollBar,gbc);
  218. controlsFrame.add(delayScrollBar);
  219. gbc.gridx=3;
  220. gbc.gridy=0;
  221. gbc.gridwidth=1;
  222. gb.setConstraints(delayLabel,gbc);
  223. controlsFrame.add(delayLabel);
  224. gbc.gridx=0;
  225. gbc.gridy=1;
  226. gbc.gridwidth=1;
  227. gb.setConstraints(modeLabel,gbc);
  228. controlsFrame.add(modeLabel);
  229. gbc.gridx=1;
  230. gbc.gridy=1;
  231. gbc.gridwidth=2;
  232. gb.setConstraints(continuous,gbc);
  233. controlsFrame.add(continuous);
  234. gbc.gridx=1;
  235. gbc.gridy=2;
  236. gbc.gridwidth=2;
  237. gb.setConstraints(stepByStep,gbc);
  238. controlsFrame.add(stepByStep);
  239. gbc.anchor = GridBagConstraints.EAST;
  240. gbc.gridx=2;
  241. gbc.gridy=3;
  242. gbc.gridwidth=1;
  243. gb.setConstraints(controlsCancel,gbc);
  244. controlsFrame.add(controlsCancel);
  245. gbc.gridx=3;
  246. gbc.gridy=3;
  247. gbc.gridwidth=1;
  248. gb.setConstraints(controlsApply,gbc);
  249. controlsFrame.add(controlsApply);
  250. //Action listenner
  251. controlsApply.addActionListener(listener);
  252. controlsCancel.addActionListener(listener);
  253. delayScrollBar.addAdjustmentListener(listener);
  254. //Action command
  255. controlsApply.setActionCommand("controlsApply");
  256. controlsCancel.setActionCommand("controlsCancel");
  257. //Active frame
  258. controlsFrame.pack();
  259. controlsFrame.setVisible(true);
  260. }
  261. public void delayAdjust(){
  262. newPauseDelay=delayScrollBar.getValue();
  263. delayLabel.setText(" "+newPauseDelay+" ms ");
  264. delayLabel.updateUI();
  265. }
  266. public void saveParameters(){
  267. continuousAnimation=continuous.isSelected();
  268. pauseDelay=newPauseDelay;
  269. animationTimer.setDelay(pauseDelay);
  270. }
  271. public void closeControls(){
  272. controlsFrame.dispose();
  273. }
  274. public void compare(){
  275. compareFrame=new JDialog();
  276. compareFrame.setResizable(false);
  277. compareFrame.setTitle("Compare braids");
  278. //First word
  279. JLabel firstWordLabel=new JLabel(" First braid word : ");
  280. firstWordField=new JTextField();
  281. firstWordField.setPreferredSize(new Dimension(200, 28));
  282. //Second word
  283. JLabel secondWordLabel=new JLabel(" Second braid word : ");
  284. secondWordField=new JTextField();
  285. secondWordField.setPreferredSize(new Dimension(200, 28));
  286. //Buttons
  287. JButton compareCancel,compareApply;
  288. compareCancel=new JButton("Cancel");
  289. compareApply=new JButton("Compare");
  290. //Action listenner
  291. compareCancel.addActionListener(listener);
  292. compareApply.addActionListener(listener);
  293. //Action command
  294. compareCancel.setActionCommand("compareCancel");
  295. compareApply.setActionCommand("compareApply");
  296. //Layout
  297. GridBagLayout gb=new GridBagLayout();
  298. GridBagConstraints gbc=new GridBagConstraints();
  299. compareFrame.setLayout(gb);
  300. gbc.anchor = GridBagConstraints.EAST;
  301. gbc.insets = new Insets(5, 5, 5, 5);
  302. gbc.gridx=0;
  303. gbc.gridy=0;
  304. gbc.gridwidth=1;
  305. gb.setConstraints(firstWordLabel,gbc);
  306. compareFrame.add(firstWordLabel);
  307. gbc.gridx=1;
  308. gbc.gridy=0;
  309. gbc.gridwidth=2;
  310. gb.setConstraints(firstWordField,gbc);
  311. compareFrame.add(firstWordField);
  312. gbc.gridx=0;
  313. gbc.gridy=1;
  314. gbc.gridwidth=1;
  315. gb.setConstraints(secondWordLabel,gbc);
  316. compareFrame.add(secondWordLabel);
  317. gbc.gridx=1;
  318. gbc.gridy=1;
  319. gbc.gridwidth=2;
  320. gb.setConstraints(secondWordField,gbc);
  321. compareFrame.add(secondWordField);
  322. gbc.gridx=1;
  323. gbc.gridy=2;
  324. gbc.gridwidth=1;
  325. gb.setConstraints(compareCancel,gbc);
  326. compareFrame.add(compareCancel);
  327. gbc.gridx=2;
  328. gbc.gridy=2;
  329. gbc.gridwidth=1;
  330. gb.setConstraints(compareApply,gbc);
  331. compareFrame.add(compareApply);
  332. compareFrame.pack();
  333. compareFrame.setVisible(true);
  334. }
  335. public void compareBraids(){
  336. word1=firstWordField.getText();
  337. word2=secondWordField.getText();
  338. System.out.println(word1+"/"+word2);
  339. word=word1;
  340. char l;
  341. for(int i=word2.length()-1;i>=0;i--){
  342. l=word2.charAt(i);
  343. if('a'<=word2.charAt(i) && word2.charAt(i)<='z'){
  344. l=(char)((int)l-(int)'a'+(int)'A');
  345. }
  346. else if('A'<=word2.charAt(i) && word2.charAt(i)<='Z'){
  347. l=(char)((int)l-(int)'A'+(int)'a'); }
  348. word+=l;
  349. }
  350. handleReductionPanel.writeBraidWord(word);
  351. comparing=true;
  352. updateWord();
  353. setButtonsState(true,true,true,true);
  354. reduce.setText("Reduce");
  355. reduce.updateUI();
  356. }
  357. public void closeCompare(){
  358. compareFrame.dispose();
  359. }
  360. //Timer
  361. public void createTimer(){
  362. if(animationTimer!=null){
  363. animationTimer.stop();
  364. }
  365. animationTimer=new Timer(pauseDelay,listener);
  366. animationTimer.setActionCommand("tic");
  367. }
  368. public void tic(){
  369. handleReductionPanel.refresh();
  370. int state=handleReductionPanel.reduce();
  371. if(state==2){
  372. updateWord();
  373. if(!continuousAnimation){
  374. reduce();
  375. }
  376. }
  377. else if(state==0){
  378. animationTimer.stop();
  379. reducing=false;
  380. setButtonsState(true,true,false,true);
  381. String finalWord=handleReductionPanel.getCurrentWord();
  382. String message;
  383. int ind=handleReductionPanel.braidDrawing.indice();
  384. if(!comparing){
  385. if(finalWord.equals("Empty")){
  386. message="The final word is empty, so your initial braid word \n"+word+" is trivial.";
  387. }
  388. else{
  389. message="There is no more handle and the final word is nonempty,\nso your initial braid word "+word+" is not trivial.";
  390. }
  391. }
  392. else{
  393. if(ind==0){
  394. message="The final word is empty,\nso your initial braid words "+word1+" and "+word2+" are equivalent.";
  395. }
  396. else{
  397. message="There is no more handle and the final word is nonempty,\nso your initial braid words "+word1+" and "+word2+" are not equivalent.";
  398. }
  399. }
  400. JOptionPane.showMessageDialog(null,message);
  401. reduce.setText("Reduce");
  402. reduce.updateUI();
  403. }
  404. }
  405. }