java/CaesarovaŠifra/src/cz/frantovo/příklady/Caesar.java
author František Kučera <franta-hg@frantovo.cz>
Fri, 18 Nov 2016 23:07:02 +0100
changeset 29 9c0f52aa4772
parent 19 c681a3a6cbac
permissions -rw-r--r--
libevent: unixové sokety místo TCP
franta-hg@19
     1
package cz.frantovo.příklady;
franta-hg@19
     2
franta-hg@19
     3
/**
franta-hg@19
     4
 * This program is free software: you can redistribute it and/or modify
franta-hg@19
     5
 * it under the terms of the GNU General Public License as published by
franta-hg@19
     6
 * the Free Software Foundation, either version 3 of the License, or
franta-hg@19
     7
 * (at your option) any later version.
franta-hg@19
     8
 *
franta-hg@19
     9
 * This program is distributed in the hope that it will be useful,
franta-hg@19
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@19
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@19
    12
 * GNU General Public License for more details.
franta-hg@19
    13
 *
franta-hg@19
    14
 * You should have received a copy of the GNU General Public License
franta-hg@19
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
franta-hg@19
    16
 */
franta-hg@19
    17
import java.awt.GridLayout;
franta-hg@19
    18
import java.awt.HeadlessException;
franta-hg@19
    19
import java.awt.event.ActionEvent;
franta-hg@19
    20
import java.awt.event.ActionListener;
franta-hg@19
    21
import java.util.ArrayList;
franta-hg@19
    22
import java.util.Collections;
franta-hg@19
    23
import java.util.List;
franta-hg@19
    24
import javax.swing.JButton;
franta-hg@19
    25
import javax.swing.JFrame;
franta-hg@19
    26
import javax.swing.JLabel;
franta-hg@19
    27
import javax.swing.JTextField;
franta-hg@19
    28
franta-hg@19
    29
/**
franta-hg@19
    30
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@19
    31
 */
franta-hg@19
    32
public class Caesar extends JFrame {
franta-hg@19
    33
franta-hg@19
    34
	private JTextField prostýText = new JTextField();
franta-hg@19
    35
	private JTextField šifrovanýText = new JTextField();
franta-hg@19
    36
	private JButton šifruj = new JButton("Šifruj");
franta-hg@19
    37
	private JButton dešifruj = new JButton("Dešifruj");
franta-hg@19
    38
	private static final char[] ABECEDA_ZNAKY = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();
franta-hg@19
    39
	private static final List<Character> ABECEDA;
franta-hg@19
    40
	private static final int SKOK = 3;
franta-hg@19
    41
franta-hg@19
    42
	static {
franta-hg@19
    43
		List<Character> abeceda = new ArrayList<>();
franta-hg@19
    44
		for (char z : ABECEDA_ZNAKY) {
franta-hg@19
    45
			abeceda.add(z);
franta-hg@19
    46
		}
franta-hg@19
    47
		ABECEDA = Collections.unmodifiableList(abeceda);
franta-hg@19
    48
	}
franta-hg@19
    49
franta-hg@19
    50
	public static void main(String[] args) {
franta-hg@19
    51
		Caesar c = new Caesar();
franta-hg@19
    52
		c.setVisible(true);
franta-hg@19
    53
	}
franta-hg@19
    54
franta-hg@19
    55
	public Caesar() throws HeadlessException {
franta-hg@19
    56
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
franta-hg@19
    57
		setTitle("Caesarova šifra");
franta-hg@19
    58
		setLocationRelativeTo(null);
franta-hg@19
    59
		setLayout(new GridLayout(3, 2));
franta-hg@19
    60
		
franta-hg@19
    61
		add(new JLabel("Prostý text"));
franta-hg@19
    62
		add(prostýText);
franta-hg@19
    63
franta-hg@19
    64
		add(new JLabel("Šifrovaný text"));
franta-hg@19
    65
		add(šifrovanýText);
franta-hg@19
    66
franta-hg@19
    67
		add(šifruj);
franta-hg@19
    68
		add(dešifruj);
franta-hg@19
    69
franta-hg@19
    70
		šifruj.addActionListener(new ActionListener() {
franta-hg@19
    71
			@Override
franta-hg@19
    72
			public void actionPerformed(ActionEvent e) {
franta-hg@19
    73
				šifrovanýText.setText(posuň(prostýText.getText(), SKOK));
franta-hg@19
    74
			}
franta-hg@19
    75
		});
franta-hg@19
    76
franta-hg@19
    77
		dešifruj.addActionListener(new ActionListener() {
franta-hg@19
    78
			@Override
franta-hg@19
    79
			public void actionPerformed(ActionEvent e) {
franta-hg@19
    80
				prostýText.setText(posuň(šifrovanýText.getText(), -SKOK));
franta-hg@19
    81
			}
franta-hg@19
    82
		});
franta-hg@19
    83
franta-hg@19
    84
		pack();
franta-hg@19
    85
	}
franta-hg@19
    86
franta-hg@19
    87
	private static String posuň(String text, int oKolik) {
franta-hg@19
    88
		StringBuilder posunutý = new StringBuilder(text.length());
franta-hg@19
    89
franta-hg@19
    90
		for (char z : text.toCharArray()) {
franta-hg@19
    91
			posunutý.append(posuň(z, oKolik));
franta-hg@19
    92
		}
franta-hg@19
    93
franta-hg@19
    94
		return posunutý.toString();
franta-hg@19
    95
	}
franta-hg@19
    96
franta-hg@19
    97
	private static char posuň(char znak, int oKolik) {
franta-hg@19
    98
		int původníPozice = ABECEDA.indexOf(znak);
franta-hg@19
    99
franta-hg@19
   100
		if (původníPozice < 0) {
franta-hg@19
   101
			return znak;
franta-hg@19
   102
		} else {
franta-hg@19
   103
			int nováPozice = (původníPozice + oKolik) % ABECEDA_ZNAKY.length;
franta-hg@19
   104
			nováPozice = nováPozice < 0 ? ABECEDA_ZNAKY.length + nováPozice : nováPozice;
franta-hg@19
   105
			return ABECEDA.get(nováPozice);
franta-hg@19
   106
		}
franta-hg@19
   107
	}
franta-hg@19
   108
}