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

public class  inverseA1 extends JApplet implements ActionListener
{ 
  JLabel label1;
  JTextField sentencebox;
  JPanel inputPanel;
  JTextField t;
  String sentence;
  JButton b;
  String s;
  int red,green,blue;
  
  public void init()
  {    
	s="Methinks it is like a weasel                                                ";
	label1=new JLabel("enter your sentence : ");
	t=new JTextField(s);
	t.setFont(new Font("Serif",Font.BOLD,24));
	sentencebox=new JTextField(s);
	b=new JButton("invert the writing " );	
    setLayout(new BorderLayout());
    inputPanel=new JPanel();
    inputPanel.add(label1);
    inputPanel.add(sentencebox);
    inputPanel.add(b);
    add(inputPanel,BorderLayout.NORTH);
    add(t,BorderLayout.CENTER);
    sentencebox.addActionListener(this);
    b.addActionListener(this);
  }
  
  public static String invert(String source) 
  { int i, len = source.length(); 
    StringBuffer dest = new StringBuffer(len); 
    for (i = (len - 1); i >= 0; i--) { 
    dest.append(source.charAt(i)); } 
    return dest.toString(); 
  }

public void actionPerformed( ActionEvent e)
{
	 if(e.getSource()==sentencebox)
        {   s=sentencebox.getText();
	        t.setText(s); 
	    }
	 else if(e.getSource()==b)
	    {  t.setText(invert(t.getText())); 
	       red    =(int)(255.0*Math.random());
           green  =(int)(255.0*Math.random());
           blue   =(int)(255.0*Math.random());
           Color c1=new Color(red,green,blue);
           t.setBackground(c1);
	    }   
}

    public static void main(String s[])
    {
        JFrame f = new JFrame("inversion of a writing Applet Graphic");
        f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        JApplet applet = new inverseA1();
        f.getContentPane().add("Center", applet);
        applet.init();
        f.setSize(800,500);
        f.setVisible(true); 
    }  
}
