Mark2020H

Java SlowPrinter for console display of text

Jan 30th, 2021 (edited)
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.76 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.FileReader;
  3. import java.io.IOException;
  4. import java.io.Reader;
  5. import static java.lang.Thread.sleep;
  6. import java.util.HashMap;
  7. import java.util.List;
  8. import java.util.Map;
  9. import java.util.Scanner;
  10.  
  11.  
  12.  
  13. /**
  14.  * Video at this address below
  15.  * https://www.facebook.com/mark.harrington.142892/videos/2843330349246302
  16.  * https://www.facebook.com/mark.harrington.142892/
  17.  * @author mark harrington
  18.  * Git Hub Page here below
  19.  * https://github.com/markh2016/javaSLowPrinterToConsole.git
  20.  * Personal web site here below
  21.  * http://www.eliteprojects.x10host.com/
  22.  **/
  23. public class MyReader implements Runnable{
  24.  
  25.    
  26.     private final String filename  ; // this holds file name
  27.     private Reader fileReader;   // file  reader reads the file in
  28.     private List<String> lines ;
  29.     private int paragraphcounter =0 ;
  30.     int charcounter = 0 ;
  31.     private Map<Integer, String> pmap ;
  32.     public String  m_string = "";
  33.     String message ;
  34.    
  35.     //Colours for console applications
  36.    
  37.     private final String m_CYAN="\033[1;36m" ;    
  38.     private final String m_GREEN ="\033[1;32m";
  39.     private final String m_RESET ="\033[0m" ;
  40.     private final String m_RED ="\033[0;31m" ;
  41.     private final String m_BLUE= "\033[0;34m" ;
  42.     private final String m_PURPLE="\033[1;35m" ;
  43.     private final String m_YELLOW="\033[0;33m";
  44.     private final String m_WHITE="\033[1;37m";
  45.     private  int index = 1 ;
  46.    
  47.     // Array to hold this colors
  48.    
  49.     String [] Carray = {"" , m_GREEN,m_RED,m_BLUE,m_YELLOW,m_WHITE};
  50.    
  51.     private volatile boolean running;
  52.    
  53.     //  I  pass the file object to the  constructor
  54.     public MyReader(String f)
  55.     {
  56.         running = true;
  57.         // contructor  will take a  file as parammeter and read this in
  58.        
  59.         this.filename = f ;
  60.         readFile(filename);
  61.         this.getmapEntry(1);
  62.     }
  63.  
  64.    
  65.     // the run method of the  class
  66.     // called via main
  67.    
  68.    
  69.    
  70.     @Override
  71.    public  void run(){
  72.         //  run method
  73.         System.out.println("Run method called");
  74.        
  75.        
  76.        
  77.         while(running)
  78.          {
  79.             try {
  80.                
  81.                 if(charcounter < message.length()){
  82.                 System.out.print(message.charAt(charcounter));
  83.                 }
  84.                 sleep(70);
  85.                 charcounter ++ ;
  86.                
  87.             } catch (InterruptedException ex) {
  88.                
  89.             }
  90.              
  91.             if(charcounter >=message.length())
  92.             {
  93.                 running= false ;
  94.                 charcounter =0 ;
  95.             }
  96.          }
  97.          
  98.          
  99.          
  100.        
  101.        
  102.              
  103.        
  104.    }
  105.  
  106.    
  107.    public void showMenu()
  108.    {
  109.       System.out.println("Select Choice           \n"
  110.                          +"Enter 1 to start..     \n"
  111.                          +"Enter 2 Next paragaph  \n"
  112.                          +"Enter 3 To stop Thread \n"
  113.                          +"Enter 4 To quit        \n\n");
  114.                              
  115.        
  116.    }
  117.    
  118.    
  119.     // get mesaage for self checks
  120.    
  121.     public String getMessage(){  
  122.         return message ;
  123.     }
  124.  
  125.    
  126.    // stop the thread  gracefully
  127.    
  128.    public void threadstopStart(boolean f)
  129.            
  130.    {
  131.        charcounter = 0  ;   // reset
  132.        running = f ;
  133.        
  134.    }
  135.    
  136.    public boolean checkrunstate()
  137.    {
  138.        return running;
  139.        
  140.    }
  141.            
  142.    
  143.    // method to  set message to map entry  for paragraphs
  144.    public void  getmapEntry(int entry)
  145.    {      
  146.        int iCol = getNumberOfParagrapghs()+1;
  147.        index = entry ;
  148.        message = (String)pmap.get(entry);
  149.        if(index < Carray.length){
  150.        message = Carray[index]+message+m_RESET ;
  151.        }
  152.        else{
  153.        index=1;
  154.        }
  155.        
  156.        
  157.        
  158.        
  159.    }
  160.      
  161.    
  162.     // to return the number of entries
  163.     public int getNumberOfParagrapghs()
  164.     {
  165.         return pmap.size();
  166.     }
  167.    
  168.    // read the file in and convert to List<Strings>
  169.    
  170.    public final void readFile(String filename)
  171.    {
  172.        
  173.        String line ;
  174.        
  175.         try {
  176.          
  177.         fileReader = new FileReader(filename);
  178.        
  179.            
  180.        
  181.            try ( // buffered read is what we stor the file contents in
  182.            // file reader is  the class that reads the file object
  183.                BufferedReader bufferedReader = new BufferedReader(fileReader)) {
  184.                pmap= new HashMap<>();
  185.                
  186.                while ((line = bufferedReader.readLine()) != null)
  187.                {
  188.                    
  189.                    // to check fro new paragrapghs  is use a simple html tag <P>
  190.                    if(!(line.startsWith("<P>")))
  191.                    {
  192.                        
  193.                        // if its a sentance then I stor this
  194.                           m_string+=line +"\n" ;
  195.                        
  196.                    }else
  197.                         {
  198.  
  199.                             // it not so its blank line
  200.                             paragraphcounter ++ ; // incremment paragraphs
  201.                             // place this into hashmap and store the entier
  202.                             // colletion of lines as a paragraph
  203.                             pmap.put(paragraphcounter, m_string);
  204.                             // reset string to hold nothing and loop
  205.                             // untill  eof  
  206.                              m_string ="" ;
  207.  
  208.                         }
  209.                    
  210.                }
  211.            }
  212.         } catch (IOException ex) {            
  213.        
  214.        
  215.         }
  216.          
  217.      //  just self check for my self          
  218.      //System.out.println(pmap.size());
  219.          
  220.    }
  221.    
  222.     public static void main(String args[]) throws InterruptedException{
  223.        
  224.         // the location of the file im reading
  225.         // this will change
  226.        
  227.         String m_file= "/home/mark/NetBeansProjects/helloworld/mytext.txt";
  228.        
  229.        
  230.         // this for prompting for  next paragraph to display
  231.         int selection = 1 ;
  232.         String inchar = "" ; // getting input from keyboard
  233.         int m_choice =0;
  234.         int max_paragraphs;
  235.         // create and instance of the class   and pass  file location to
  236.         //constructor
  237.        
  238.        
  239.         MyReader m_reader = new MyReader(m_file) ;
  240.            
  241.         // get the maxumum number of praragraphs
  242.        
  243.         max_paragraphs= m_reader.getNumberOfParagrapghs() ;
  244.        
  245.              
  246.        
  247.        System.out.println("Enter choice");
  248.        Scanner sc = new Scanner(System.in)  ;
  249.      
  250.    
  251.        
  252.    
  253.        
  254.        while(m_choice < 1||m_choice >4)
  255.        {
  256.          
  257.            m_reader.showMenu();
  258.            m_choice =sc.nextInt();
  259.            
  260.            switch(m_choice){
  261.            
  262.                case 1:                      
  263.                         m_reader.threadstopStart(true);
  264.                        
  265.                        
  266.                         Thread t = new Thread(m_reader);
  267.                         t.start();
  268.                         t.join();
  269.                        
  270.                        
  271.                        
  272.                        
  273.                         break ;
  274.                case 2 :
  275.                         m_reader.threadstopStart(true);
  276.                         selection+=1 ;
  277.                        
  278.                         if(selection< (max_paragraphs+1)){
  279.                         m_reader.getmapEntry(selection) ;
  280.                        
  281.                         }  
  282.                         Thread t2 = new Thread(m_reader);
  283.                         t2.start();  
  284.                        
  285.                         t2.join();
  286.                        
  287.                        
  288.                         break ;
  289.                    
  290.                case 3 : m_reader.threadstopStart(false);
  291.                         System.out.println("Stopping Thread\n\n");
  292.                         break ;
  293.                        
  294.                        
  295.                case 4 :
  296.                        
  297.                         m_reader.threadstopStart(false);
  298.                         System.out.println("Thankyou for using  Now Exiting application\n\n");
  299.                         System.exit(0);
  300.                        
  301.                         break ;
  302.                    
  303.                    
  304.                        
  305.                    
  306.                    
  307.            }
  308.            if(selection >= max_paragraphs)
  309.            {
  310.               selection = 0 ; // reset this
  311.            }
  312.            m_choice=0 ;  // reset m choice
  313.        }
  314.        
  315.            
  316.        }
  317. }// end class
  318.  
Add Comment
Please, Sign In to add comment