// a simple prime factor demonstration
// John Sklar, CED 512 - fall, 2k   
   import java.applet.*;
   import java.awt.*;
   import java.awt.event.*;
   public class PrimeFactorB extends Applet implements ActionListener
   {
      TextField target,answer;
      Button Factor = new Button ("Prime Factor");
      Label answer2, Banner;
      Panel top,bottom,middle;
   
      public void init()
      {
         setLayout(new GridLayout(3,1));
         top = new Panel();
         bottom = new Panel();
         middle = new Panel();
      
         Font bigFont = new Font("TimesRoman", Font.ITALIC, 24);
         Font miniFont = new Font("TimesRoman", Font.BOLD, 18);
         Font opFont = new Font("Courier", Font.BOLD, 24);
      
         target = new TextField(10);
         answer = new TextField(65);
         Banner = new Label ("Find the Prime factors of:");
         answer2 = new Label("                                                            ");
      
         Factor.setFont(opFont);
         answer.setFont(miniFont);
         answer2.setFont(bigFont);
      
         Banner.setFont(bigFont);
         target.setFont(bigFont);
      
         add(top);
         add(middle);
         add(bottom);
      
         top.add (Banner);
         top.add (target);
         top.add (Factor);
         middle.add (answer);
         bottom.add (answer2);
      
         Factor.addActionListener(this);         
         target.addActionListener(this);
         setBackground(Color.white);
      }
   
      public void start()
      {
         Banner.setText ("Find the Prime factors of:");
         target.setText("");
         answer.setVisible(false);
         answer2.setText("                                                                     ");
         Factor.setLabel("Prime Factor");
         target.setEnabled(true);// ok, you can type
         invalidate();
         validate();
      }
      public void actionPerformed (ActionEvent e)
      {
         int[] powers = new int[1000000];
         int finalanswer=0;
         String facts = new String(""); 
         String type = new String("");
         boolean prime=true;
         int hold,temp, divisor=2,input;       
         if (Factor.getLabel()=="Prime Factor")
         {
            input=Integer.parseInt(target.getText());       
            hold = input;
            if(input < 0){
               input = Math.abs(input);
               facts += "-1 * ";
            }
            temp = input;
            do{
               while(temp>1){
                  if(temp%divisor==0){
                     ++powers[divisor];
                     facts += divisor;
                     if(temp!=divisor){
                        prime = false;
                        facts += " * ";
                     }
                     else {
                        facts += " = " + hold;
                        type = "The Number " +  hold ;
                     }
                     temp = temp/divisor;
                  }
                  else
                     divisor++;
               }
            }while(divisor <= Math.sqrt(temp));
         
            Banner.setText (" The Prime Factors of");
            if(input==1)
            {
               Banner.setText (" Please Note: ");
               facts += "One is neither prime nor composite!";
            }
            else if(input==0)
            {
               Banner.setText (" Please Note: ");
               facts += "Zero is neither prime nor composite!";
            }
            else if(prime==true){
               Banner.setText (" Please Note: ");
               type += " is Prime " ;
            }
            else if(prime==false)
               type  += " is composite ";
            answer.setText (facts); 
            answer2.setText (type);
            answer.setVisible(true);
            Factor.setLabel("   Clear  ");
            target.setEnabled(false);// no typing now
         }
         else
            start();
      }
   }