import java.io.*; import java.applet.Applet; import java.awt.*; import java.awt.event.*; import java.util.regex.*; import java.util.StringTokenizer; public class pad extends Panel implements ActionListener, KeyListener { static int pressed = 0; static final int WIDTH = 800; static final int HEIGHT = 600; TextArea ta1 = new TextArea(35, 80); TextField tf1 = new TextField(); TextField tf2 = new TextField(); Button newf = new Button("New"); Button save = new Button("Save"); Button open = new Button("Open"); public pad(boolean ismain) { Panel b1 = new Panel(new GridLayout(1, 2)); if (ismain) { b1.add(newf); newf.addActionListener(this); } b1.add(save); save.addActionListener(this); b1.add(open); open.addActionListener(this); Panel b2 = new Panel(new GridLayout(1, 2)); b2.add(new Label("File:")); b2.add(tf1); Panel b3 = new Panel(new GridLayout(1, 2)); b3.add(new Label("Line:")); b3.add(tf2); Panel p1 = new Panel(new GridLayout(3, 1)); p1.add(b1); p1.add(b2); p1.add(b3); setLayout(new BorderLayout()); add(ta1, BorderLayout.CENTER); add(p1, BorderLayout.NORTH); ta1.addKeyListener(this); } public void keyPressed(KeyEvent e) { pressed++; } public void keyReleased(KeyEvent e) {} public void keyTyped(KeyEvent e) {} public void actionPerformed(ActionEvent e) { int act = 0; try { if (e.getSource() == save) act = 1; else if (e.getSource() == open) act = 2; else if (e.getSource() == newf) act = 3; switch(act) { case 1: int cut = Integer.parseInt(tf2.getText()); int len = 0; PrintWriter savefile = new PrintWriter(new FileWriter(tf1.getText())); String text = ta1.getText(); for (int i = 0; i < text.length(); i++) { len++; char a = text.charAt(i); if (a == '\n') len = 0; else if (len == cut) { String b = text.substring(0, i + 1); String c = text.substring(i + 1); text = b + "\n" + c; } } ta1.setText(text); for (int i = 0; i < text.length(); i++) savefile.print((char)(text.charAt(i) + 10)); savefile.close(); System.out.println("Text saved into " + tf1.getText()); break; case 2: ta1.setText(""); BufferedReader loadfile = new BufferedReader(new FileReader(tf1.getText())); String line = loadfile.readLine(); char dec[] = new char[line.length()]; while (line != null) { for (int b = 0; b < line.length(); b++) dec[b] = (char)(line.charAt(b) - 10); ta1.append(new String(dec)); line = loadfile.readLine(); } loadfile.close(); System.out.println("Text loaded from " + tf1.getText()); break; case 3: Frame f = new Frame(); f.add(new pad(false)); f.setSize(WIDTH, HEIGHT); f.setVisible(true); f.addWindowListener(new React(0)); break; } } catch (Exception exc) { System.out.println(exc); } } public static void main(String[] args) { Frame f1 = new Frame(); f1.add(new pad(true)); f1.setSize(WIDTH, HEIGHT); f1.setTitle(""); f1.setVisible(true); f1.addWindowListener(new React(1)); } } class React extends WindowAdapter { int count; int exit; public React(int x) { new timer().start(); exit = x; } class timer extends Thread { public timer() { count = 0; } public void run() { try { while (true) { sleep(1000); count++; } } catch (Exception e) {} } } //public void windowClosing(WindowEvent e) throws IOException { public void windowClosing(WindowEvent e) { try { PrintWriter cf = new PrintWriter(new FileWriter("counter.txt")); // cf - counter file cf.println("Last session\n==="); cf.println("Lasted for " + count + " seconds."); cf.println("Keys were pressed " + pad.pressed + " times."); cf.close(); } catch (Exception exc) { System.out.println(exc); } System.out.println("Window closed"); if (exit == 1) System.exit(0); else e.getWindow().dispose(); } }