twitter
rss



   1:  /**
   2:   *
   3:   * @author Hariraj
   4:   */
   5:  import java.applet.Applet;
   6:  import java.awt.*;
   7:  import java.awt.event.*;
   8:   
   9:  public class magic2 extends Applet implements ActionListener {
  10:   
  11:           Panel rules=new Panel(new GridLayout(5,1));
  12:           Panel magic=new Panel(new GridLayout(3,3));
  13:           Panel bottomp=new Panel(new GridLayout(2,1));
  14:           Panel nos =new Panel(new GridLayout(3,3));
  15:           TextField [] input = new TextField[10];
  16:           Label rule=new Label("What is a Magic Square?");
  17:           Label rule2=new Label( "A magic square of order n is an arrangement of n^2 numbers,usually distinct");
  18:           Label rule3=new Label("integers, in a square, such that the n numbers in all rows, all columns, ");
  19:           Label rule4=new Label("and both diagonals sum to the same constant.");
  20:           Label result=new Label();
  21:           Button reset=new Button("Reset");
  22:           Button[] no=new Button[9];
  23:   
  24:           public void init() {
  25:              this.setSize(440, 440);
  26:              this.setLayout(new BorderLayout());
  27:              this.add(rules,BorderLayout.NORTH);
  28:              this.add(magic,BorderLayout.CENTER);
  29:              bottomp.add(nos);
  30:              bottomp.add(reset);
  31:              reset.setBackground(Color.pink);
  32:              this.add(bottomp,BorderLayout.SOUTH);
  33:              rules.add(result);
  34:              rule.setBackground(Color.LIGHT_GRAY);
  35:              rules.add(rule);
  36:              rules.add(rule2);
  37:              rules.add(rule3);
  38:              rules.add(rule4);
  39:              result.setBackground(Color.yellow);
  40:              result.setText("WELCOME!!! Click the numbers to begin");
  41:              for(int i=0;i<9;i++)
  42:              {
  43:                 magic.add(no[i]=new Button());
  44:                 no[i].setLabel(""+(i+1));
  45:                 no[i].setBackground(Color.lightGray);
  46:                 nos.add(no[i]);
  47:                 no[i].addActionListener(this);
  48:                 magic.add(input[i]=new TextField("",1));
  49:                 input[i].setEditable(false);           
  50:               }reset.addActionListener(this);
  51:      }
  52:           int g=0;
  53:   
  54:           public void actionPerformed(ActionEvent e) {
  55:            
  56:               if(e.getSource().equals(reset)){
  57:                  for(int i=0;i<9;i++)
  58:                  {
  59:                      input[i].setText("");
  60:                      input[i].setEditable(false);
  61:                      no[i].setEnabled(true);
  62:                  }
  63:                  g=0;
  64:                  result.setBackground(Color.WHITE);
  65:                  result.setText("");
  66:              }
  67:              if(e.getSource().equals(no[0]))
  68:             {
  69:                  input[g].setText("1");
  70:                  no[0].setEnabled(false);
  71:                  g++;
  72:              }
  73:   
  74:             if(e.getSource().equals(no[1]))
  75:             {
  76:                  input[g++].setText("2");
  77:                   no[1].setEnabled(false);
  78:   
  79:              }
  80:                  if(e.getSource().equals(no[2]))
  81:             {
  82:                  input[g++].setText("3");
  83:                   no[2].setEnabled(false);
  84:              }
  85:                  if(e.getSource().equals(no[3]))
  86:             {
  87:                  input[g++].setText("4");
  88:                   no[3].setEnabled(false);
  89:              }
  90:                  if(e.getSource().equals(no[4]))
  91:             {
  92:                  input[g++].setText("5");
  93:                   no[4].setEnabled(false);
  94:              }
  95:                  if(e.getSource().equals(no[5]))
  96:             {
  97:                  input[g++].setText("6");
  98:                   no[5].setEnabled(false);
  99:              }
 100:                  if(e.getSource().equals(no[6]))
 101:             {
 102:                  input[g++].setText("7");
 103:                   no[6].setEnabled(false);
 104:              }
 105:                  if(e.getSource().equals(no[7]))
 106:             {
 107:                  input[g++].setText("8");
 108:                   no[7].setEnabled(false);
 109:              }
 110:              if(e.getSource().equals(no[8]))
 111:             {
 112:                  input[g++].setText("9");
 113:                   no[8].setEnabled(false);
 114:              }
 115:                if(g<9){
 116:                 result.setBackground(Color.yellow);
 117:                 result.setText("Click the remaining " +(9-g)+ " enabled button to see the result");
 118:             }
 119:             if(g==9){
 120:                  int r1=Integer.parseInt(input[0].getText())+Integer.parseInt(input[1].getText())+Integer.parseInt(input[2].getText());
 121:                  int r2=Integer.parseInt(input[3].getText())+Integer.parseInt(input[4].getText())+Integer.parseInt(input[5].getText());
 122:                  int r3=Integer.parseInt(input[6].getText())+Integer.parseInt(input[7].getText())+Integer.parseInt(input[8].getText());
 123:                  int d1=Integer.parseInt(input[0].getText())+Integer.parseInt(input[4].getText())+Integer.parseInt(input[8].getText());
 124:                  int d2=Integer.parseInt(input[2].getText())+Integer.parseInt(input[4].getText())+Integer.parseInt(input[6].getText());
 125:   
 126:      if((r1==r2)&&(r1==r3)&&(r1==d1)&&(r1==d2))
 127:      {
 128:                      result.setBackground(Color.GREEN);
 129:                      result.setText("                          CONGRATS!!! IT'S A MAGIC SQUARE");
 130:   
 131:      }else {         result.setBackground(Color.red);
 132:                      result.setText("            NOT A MAGIC SQUARE!!! Try Again :-(");
 133:   
 134:                 }
 135:             }
 136:   }
 137:  }