Guest User

Untitled

a guest
Oct 22nd, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.97 KB | None | 0 0
  1. import javax.swing.*;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6.  
  7. public class ProgramScytale {
  8.  
  9. JTextArea tArea;
  10. JTextArea tArea1;
  11. JPanel p1;
  12. JButton button0;
  13. JButton button1;
  14. JButton button2;
  15. JScrollPane scroll;
  16. JScrollPane scroll1;
  17.  
  18. String Message;
  19. String SPARTAMessage;
  20. int length;
  21.  
  22. ProgramScytale(){
  23.  
  24. JFrame window = new JFrame ("Шифр древней спарты");
  25. p1 =new JPanel(null);
  26.  
  27. tArea = new JTextArea();
  28. tArea.setLineWrap(true);
  29. tArea.setWrapStyleWord(true);
  30. tArea.setFont(new Font("Dialog", Font.BOLD, 16));
  31.  
  32. button0 =new JButton ("ЗАШИФРОВАТЬ");
  33. button0.setIcon(new ImageIcon("src\images\icon1.jpg"));
  34. TActionListener actionListener = new TActionListener();
  35. button0.addActionListener(actionListener);
  36. button0.setBounds(10, 205, 195, 50);
  37.  
  38. button1 =new JButton ("ДЕШИФРОВАТЬ");
  39. button1.setIcon(new ImageIcon("src\images\icon2.jpg"));
  40. TActionListener1 actionListener1 = new TActionListener1();
  41. button1.addActionListener(actionListener1);
  42. button1.setBounds(390, 205, 195, 50);
  43.  
  44. scroll = new JScrollPane(tArea);
  45. scroll.setBounds(10, 265, 575, 100);
  46. scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS );
  47.  
  48. tArea1 =new JTextArea();
  49. tArea1.setLineWrap(true);
  50. tArea1.setWrapStyleWord(true);
  51. tArea1.setEditable(false);
  52. tArea1.setFont(new Font("Dialog", Font.BOLD, 16));
  53.  
  54. scroll1 = new JScrollPane(tArea1);
  55. scroll1.setBounds(10, 10, 575, 150);
  56. scroll1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS );
  57.  
  58. window.add(p1);
  59. p1.add(scroll);
  60. p1.add(button0);
  61. p1.add(button1);
  62. p1.add(scroll1);
  63.  
  64. centeringFrame(600,400,window); // Вызываем метод центрирования для созданного окна
  65. window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Завершаем программу при закрытии окна
  66. window.setVisible(true); // Делаем окно видимым
  67. window.setResizable(false);
  68.  
  69. }
  70.  
  71. private static void centeringFrame (int sizeWidth, int sizeHeight, JFrame frame){ // Метод принимает размеры окна (ширину и высоту), а также его название
  72. Dimension s = Toolkit.getDefaultToolkit().getScreenSize();
  73. int X = (s.width - sizeWidth) / 2;
  74. int Y = (s.height - sizeHeight) / 2;
  75. frame.setBounds(X, Y, sizeWidth, sizeHeight);
  76. }
  77.  
  78. public class TActionListener implements ActionListener {
  79. public void actionPerformed(ActionEvent event) {
  80.  
  81. Message = tArea.getText();
  82. tArea.setText(null);
  83.  
  84. length = Message.length();
  85. int n =(((Integer) (length-1)/4)+1);
  86. String s = new String();
  87. for(int i = 0;i <n; i++){
  88. for(int j = 0;j < 4; j++)
  89. {
  90. try{
  91. s+=Message.charAt(i+n*j);
  92. }catch(IndexOutOfBoundsException e){
  93. s+=" ";
  94. }
  95. }
  96. }
  97. SPARTAMessage = s;
  98. Message = "";
  99.  
  100. tArea1.setText(null);
  101. tArea1.append(SPARTAMessage);
  102. }
  103. }
  104.  
  105. public class TActionListener1 implements ActionListener {
  106. public void actionPerformed(ActionEvent e){
  107.  
  108. SPARTAMessage= tArea1.getText();
  109.  
  110. int n =(((Integer) (length-1)/4)+1);
  111. char[] s = new char[SPARTAMessage.length()];
  112. int number =0;
  113. for(int i = 0; i < n;i++){
  114. for(int j = 0;j < 4; j++)
  115. {
  116. s[i+n*j]= SPARTAMessage.charAt(number);
  117. number++;
  118. }
  119. }
  120. for (int i = 0; i < s.length; i++) {
  121. Message+=s[i];
  122. };
  123. tArea.setText(null);
  124. tArea.append(Message);
  125.  
  126. }
  127. }
  128.  
  129. public static void main(String[] args) {
  130. ProgramScytale pro = new ProgramScytale();
  131. }
  132. }
  133.  
  134. public class TActionListener1 implements ActionListener {
  135. public void actionPerformed(ActionEvent e){
  136.  
  137. SPARTAMessage= tArea.getText();
  138.  
  139. int n =(((Integer) (length-1)/4)+1);
  140. char[] s = new char[SPARTAMessage.length()];
  141. int number =0;
  142. for(int i = 0; i < n;i++){
  143. for(int j = 0;j < 4; j++)
  144. {
  145. s[i+n*j]= SPARTAMessage.charAt(number);
  146. number++;
  147. }
  148. }
  149. for (int i = 0; i < s.length; i++) {
  150. Message+=s[i];
  151. };
  152.  
  153. tArea1.append(Message);
  154. }
  155. }
  156.  
  157. Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
  158. at java.lang.String.charAt(Unknown Source)
  159. at ProgramScytale$TActionListener1.actionPerformed(ProgramScytale.java:121)
  160.  
  161. public class SPARTA {
  162. static String Message="Привет";
  163. static String SPARTAMessage;
  164.  
  165. int length;
  166.  
  167. public void SHIFR(){
  168. this.length = Message.length();
  169. int n =(((Integer) (length-1)/4)+1);
  170. String s = new String();
  171. for(int i = 0;i <n; i++){
  172. for(int j = 0;j < 4; j++)
  173. {
  174. try{
  175. s+=Message.charAt(i+n*j);
  176. }catch(IndexOutOfBoundsException e){
  177. s+=" ";
  178. }
  179. }
  180. }
  181. SPARTAMessage = s;
  182. }
  183. public void deSHIFR(){
  184. int n =(((Integer) (length-1)/4)+1);
  185. char[] s = new char[SPARTAMessage.length()];
  186. int number =0;
  187. for(int i = 0; i < n;i++){
  188. for(int j = 0;j < 4; j++)
  189. {
  190. s[i+n*j]= SPARTAMessage.charAt(number);
  191. number++;
  192. }
  193. }
  194. for (int i = 0; i < s.length; i++) {
  195. Message+=s[i];
  196. };
  197. }
  198. public static void main(String args[])throws Exception
  199. {
  200. SPARTA obj = new SPARTA();
  201. obj.SHIFR();
  202. System.out.println(SPARTAMessage);
  203. obj.deSHIFR();
  204. System.out.println(Message);
  205. }
  206. }
Add Comment
Please, Sign In to add comment