import java.applet.Applet; import java.awt.*;
public class MSquare1 extends Applet {
String board[][]; int size; Label sizeLab; TextField sizeTxt;
public void init() { board = new String[15][15]; sizeLab = new Label("Size?"); add(sizeLab); sizeTxt = new TextField(); add(sizeTxt);
}
public void paint(Graphics g) { for (int r=0; r<size; r++) { for (int c=0; c<size; c++) { g.drawString(board[r][c],c*15+10,r*15+40); } } }
public boolean action(Event e, Object o) { if (e.target == sizeTxt) { size = Integer.parseInt(sizeTxt.getText());
for (int x=0; x<size*size; x++) boardStr(x);
repaint(); } return true; }
public void boardStr(int pos) { board[pos/size][pos % size] = String.valueOf(pos); }
}
Your task is to write a magic square program, which will be an adaptation of the above code. We will go over the rules in class. This is what your finished Applet should run like:
|