import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class Diamond extends Applet
             implements ActionListener {
   
	Label prompt1;     
	TextField input1;  
	
	int ArraySize;
	int XPosition;  
	int YPosition = 70;
	char MyArray[][];
	boolean BadInput = false;
	
  
   public void init()
   {
      prompt1 = new Label( "Enter an odd integer and press Enter" );
      add( prompt1 );  

      input1 = new TextField( 10 );
      input1.addActionListener( this );
      add( input1 );
	  
   }

   
   public void paint( Graphics g )
   {
		if (BadInput == true) 
		  {
				g.drawString("Your input of " + ArraySize + " is not an odd number; Please try again", 40, 40);
				BadInput = false;
		  }
		 else
		 {
			 DrawDiamond(MyArray, g);
			 
		 }				
   }

   public void DrawDiamond(char DiamondArray[][], Graphics z)
    {
		  YPosition = 70;
		  for (int i=0; i < DiamondArray.length; i++) {
			  YPosition += 15;
		      XPosition = 120;
			  for (int j=0; j < DiamondArray[i].length; j++)
				{
				  z.drawString( String.valueOf(DiamondArray[i][j]), XPosition, YPosition);
					XPosition += 10;
				}
		  }		 
   }
	   
	   
   public void actionPerformed( ActionEvent e )
   {   
	  int IsItOddOrEven;
	  MyArray = null;
      ArraySize = Integer.parseInt( input1.getText() );
	  input1.setText("");
	  IsItOddOrEven = ArraySize % 2;
	  int TheMiddle;
	  int TheLeft;
	  int TheRight;
	  int TheOtherLeft;
	  int TheOtherRight;
	  if (IsItOddOrEven == 0)
	  {
		 	BadInput = true;
			repaint();
			return;
	  }
	  else
	  {
			MyArray = new char[ArraySize][ArraySize];
			TheMiddle = ((int)Math.round((double) ArraySize / 2) -1);
			TheLeft = TheMiddle - 1;
			TheRight = TheMiddle + 1;
			TheOtherLeft = 1;
			TheOtherRight = ArraySize - 2;
			
			for (int i=0; i < MyArray.length; i++) {
				if (i == 0 || i == (MyArray.length - 1))
					MyArray[i][TheMiddle] = '*';
				else if (i > 0 && i <= TheMiddle)
				{	
					for (int j=TheLeft; j <= TheRight; j++)
						{
						  MyArray[i][j] = '*';
						}
					if (TheLeft > 0)
						TheLeft--;
					if (TheRight < MyArray[i].length)
						TheRight++;
				}
				else if (i < MyArray.length && i > TheMiddle)
				{	
					for (int j=TheOtherLeft; j <= TheOtherRight; j++)
						{
						  MyArray[i][j] = '*';
						}
						TheOtherLeft++;
						TheOtherRight--;
				}
				else 
				{
					for (int j=0; j < MyArray[i].length; j++)
						{
						  MyArray[i][j] = '*';
						}
				}
		  }		 
	  
	  }
	  	
	  repaint();
   }

}
