Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- public class ProgramScytale {
- JTextArea tArea;
- JTextArea tArea1;
- JPanel p1;
- JButton button0;
- JButton button1;
- JButton button2;
- JScrollPane scroll;
- JScrollPane scroll1;
- String Message;
- String SPARTAMessage;
- int length;
- ProgramScytale(){
- JFrame window = new JFrame ("Шифр древней спарты");
- p1 =new JPanel(null);
- tArea = new JTextArea();
- tArea.setLineWrap(true);
- tArea.setWrapStyleWord(true);
- tArea.setFont(new Font("Dialog", Font.BOLD, 16));
- button0 =new JButton ("ЗАШИФРОВАТЬ");
- button0.setIcon(new ImageIcon("src\images\icon1.jpg"));
- TActionListener actionListener = new TActionListener();
- button0.addActionListener(actionListener);
- button0.setBounds(10, 205, 195, 50);
- button1 =new JButton ("ДЕШИФРОВАТЬ");
- button1.setIcon(new ImageIcon("src\images\icon2.jpg"));
- TActionListener1 actionListener1 = new TActionListener1();
- button1.addActionListener(actionListener1);
- button1.setBounds(390, 205, 195, 50);
- scroll = new JScrollPane(tArea);
- scroll.setBounds(10, 265, 575, 100);
- scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS );
- tArea1 =new JTextArea();
- tArea1.setLineWrap(true);
- tArea1.setWrapStyleWord(true);
- tArea1.setEditable(false);
- tArea1.setFont(new Font("Dialog", Font.BOLD, 16));
- scroll1 = new JScrollPane(tArea1);
- scroll1.setBounds(10, 10, 575, 150);
- scroll1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS );
- window.add(p1);
- p1.add(scroll);
- p1.add(button0);
- p1.add(button1);
- p1.add(scroll1);
- centeringFrame(600,400,window); // Вызываем метод центрирования для созданного окна
- window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Завершаем программу при закрытии окна
- window.setVisible(true); // Делаем окно видимым
- window.setResizable(false);
- }
- private static void centeringFrame (int sizeWidth, int sizeHeight, JFrame frame){ // Метод принимает размеры окна (ширину и высоту), а также его название
- Dimension s = Toolkit.getDefaultToolkit().getScreenSize();
- int X = (s.width - sizeWidth) / 2;
- int Y = (s.height - sizeHeight) / 2;
- frame.setBounds(X, Y, sizeWidth, sizeHeight);
- }
- public class TActionListener implements ActionListener {
- public void actionPerformed(ActionEvent event) {
- Message = tArea.getText();
- tArea.setText(null);
- length = Message.length();
- int n =(((Integer) (length-1)/4)+1);
- String s = new String();
- for(int i = 0;i <n; i++){
- for(int j = 0;j < 4; j++)
- {
- try{
- s+=Message.charAt(i+n*j);
- }catch(IndexOutOfBoundsException e){
- s+=" ";
- }
- }
- }
- SPARTAMessage = s;
- Message = "";
- tArea1.setText(null);
- tArea1.append(SPARTAMessage);
- }
- }
- public class TActionListener1 implements ActionListener {
- public void actionPerformed(ActionEvent e){
- SPARTAMessage= tArea1.getText();
- int n =(((Integer) (length-1)/4)+1);
- char[] s = new char[SPARTAMessage.length()];
- int number =0;
- for(int i = 0; i < n;i++){
- for(int j = 0;j < 4; j++)
- {
- s[i+n*j]= SPARTAMessage.charAt(number);
- number++;
- }
- }
- for (int i = 0; i < s.length; i++) {
- Message+=s[i];
- };
- tArea.setText(null);
- tArea.append(Message);
- }
- }
- public static void main(String[] args) {
- ProgramScytale pro = new ProgramScytale();
- }
- }
- public class TActionListener1 implements ActionListener {
- public void actionPerformed(ActionEvent e){
- SPARTAMessage= tArea.getText();
- int n =(((Integer) (length-1)/4)+1);
- char[] s = new char[SPARTAMessage.length()];
- int number =0;
- for(int i = 0; i < n;i++){
- for(int j = 0;j < 4; j++)
- {
- s[i+n*j]= SPARTAMessage.charAt(number);
- number++;
- }
- }
- for (int i = 0; i < s.length; i++) {
- Message+=s[i];
- };
- tArea1.append(Message);
- }
- }
- Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
- at java.lang.String.charAt(Unknown Source)
- at ProgramScytale$TActionListener1.actionPerformed(ProgramScytale.java:121)
- public class SPARTA {
- static String Message="Привет";
- static String SPARTAMessage;
- int length;
- public void SHIFR(){
- this.length = Message.length();
- int n =(((Integer) (length-1)/4)+1);
- String s = new String();
- for(int i = 0;i <n; i++){
- for(int j = 0;j < 4; j++)
- {
- try{
- s+=Message.charAt(i+n*j);
- }catch(IndexOutOfBoundsException e){
- s+=" ";
- }
- }
- }
- SPARTAMessage = s;
- }
- public void deSHIFR(){
- int n =(((Integer) (length-1)/4)+1);
- char[] s = new char[SPARTAMessage.length()];
- int number =0;
- for(int i = 0; i < n;i++){
- for(int j = 0;j < 4; j++)
- {
- s[i+n*j]= SPARTAMessage.charAt(number);
- number++;
- }
- }
- for (int i = 0; i < s.length; i++) {
- Message+=s[i];
- };
- }
- public static void main(String args[])throws Exception
- {
- SPARTA obj = new SPARTA();
- obj.SHIFR();
- System.out.println(SPARTAMessage);
- obj.deSHIFR();
- System.out.println(Message);
- }
- }
Add Comment
Please, Sign In to add comment