Monday 16 September 2013

SIMPLE TEXT EDITOR IN JAVA

This section takes you on step by step process to build a text editor with functionalities such as copy, cut, select and paste.

Note: This tutorial assumes you have a prior knowledge of java and this is not your first time of writing a java code.

         
 import java.awt.*;  
 import javax.swing.*;  
 import java.awt.event.*;  

 public class TextEditor extends JFrame implements ActionListener  {
  private JMenuBar menu = new JMenuBar();
  private JMenu file, edit
  private JMenuItem cut, copy, paste, exit;
  private JTextArea notepad = new JTextArea(10, 20);  
  private String contentCollector = ""; 
  Container pane;
   public TextEditor() {
   super("NOTEPAD");
   setSize(300, 200);
   setVisible(true); 
   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
   pane = getContentPane();  
   pane.setLayout(new BorderLayout());  
   pane.add(notepad, BorderLayout.CENTER);  
   pane.add(new JScrollPane(notepad));  
   notepad.setLineWrap(true);  
   setJMenuBar(menu); 
            
   file = new JMenu("File");  
   menu.add(file);  
   exit = new JMenuItem("Exit");  
   file.add(exit);  
   exit.addActionListener(this);
            
   edit = new JMenu("Edit");  
   menu.add(edit);
              
   cut = new JMenuItem("Cut"); 
   cut.addActionListener(this);
            
   copy = new JMenuItem("Copy");  
   copy.addActionListener(this);
            
   paste = new JMenuItem("Paste");
   paste.addActionListener(this); 
            
   edit.add(cut);  
   edit.add(copy);  
   edit.add(paste);  
   
  }
  public static void main(String[] args){
   new TextEditor();
  }
  public void actionPerformed(ActionEvent e) {
   JMenuItem Item = (JMenuItem) e.getSource();  
   if (Item == cut)  {  
    contentCollector = notepad.getSelectedText();  
    notepad.replaceRange("", notepad.getSelectionStart(), notepad.getSelectionEnd());  
   }  else if (Item == copy)  {
    contentCollector = notepad.getSelectedText(); 
   }  else if (Item == paste)  {
    notepad.insert(contentCollector, notepad.getCaretPosition());  
   }  else if (Item == exit)  {
    System.exit(0); 
   }
  }
 }

         
        

At certain times you may want to check how fast your code ran i.e. the execution time of your code, how fast did it start and end successfully. To do that, there is a trick to go about it. For instance: say i wrote an algorithm to search through an array containing integer number for a given number such that once the number is found, the program exits and if not found, the program returns an error message as shown below:


   public class Search {
      public static void main(String[] args){
          java.util.Scanner in = new java.util.Scanner(System.in);
          int[] array = new int[1000];
          for(int i = 0; i < array.length; i++){
             array[i] = (int) (2.3 * Math.random());
          }
          System.out.print("Enter a number to search for: ");
          int number = in.nextInt();
          if(searchAlgorithm(array, number)){
              System.out.println(number+" was found in the array!");
          } else {
              System.out.println(number+" was not found in the array!");
          }
      }
      public static boolean searchAlgorithm(int[] array, int key){
          boolean found = false;
          for(int i = 0; i < array.length; i++) {
              if(array[i] == key) {
                 found = !found;
                 break;
              } else {
                 found = found;
              }
          }
          return found;
      }
   }
         

Now supposing i want to know how fast the code ran, i would just make a few modifications: in the main method i would do the following:

   public static void main(String[] args){
          java.util.Scanner in = new java.util.Scanner(System.in);
          int[] array = new int[1000];
          for(int i = 0; i < array.length; i++){
             array[i] = (int) (2.3 * Math.random());
          }
          System.out.print("Enter a number to search for: ");
          int number = in.nextInt();
          //get beginning of runtime
          int startTime = System.currentTimeMillis();
          if(searchAlgorithm(array, number)){
              System.out.println(number+" was found in the array!");
          } else {
              System.out.println(number+" was not found in the array!");
          }
          //get end time
          int endTime = System.currentTimeMillis();
          System.out.println("Total time of execution is: " + (endTime - startTime));
      }
There you have it: the execution time. This can help you become conscious of avoiding ambigous code and for better code optimization.

1 comment:

Your suggestion counts...