/* * The Magic Square * written by: Arthur Gapusan; Dexter Santiago * Language: Java */ import java.awt.*; import java.awt.event.*; import java.applet.*; /* */ public class Magic3 extends Applet implements ActionListener { int[][] square; int x, y; boolean left = false, below = false, finish = false; int n; TextField tf; Button start; public void init() { setLayout(null); n = 13; tf = new TextField(2); tf.setBounds(570,20, 25,20); add(tf); start = new Button("Start"); start.setBounds(535,50, 40, 30); add(start); start.addActionListener(this); setBackground(new Color(200, 200, 200)); Label l = new Label("Rows &"); Label l2 = new Label("Columns:"); l.setFont(new Font("Verdana", Font.BOLD, 12)); add(l); l.setBounds(510,10,100,10); l2.setFont(new Font("Verdana", Font.BOLD, 12)); add(l2); l2.setBounds(510,22,100,10); } public void actionPerformed(ActionEvent ae) { Graphics g = getGraphics(); if (start == ae.getSource()) { if (finish == true) { g.setColor(new Color(200,200,200)); g.fillRect(0,0,600,500); } n = Integer.parseInt(tf.getText()); if ((n%2==0) || (n >17)); else { run(g); finish = true; } } } public void drawBox(String value, int x, int y, boolean left, boolean below, Graphics g) { x = x * 28+10; y = y * 28+10; g.setColor(new Color(55, 55, 55)); if (left==false) { g.drawLine(x,y,x+5,y-5); g.drawLine(x,y,x,y+25); g.drawLine(x,y+25,x+5,y-5+25); } if (below==false) { g.drawLine(x,y+25,x+25,y+25); g.drawLine(x+25,y+25,x+5+25,y-5+25); } g.setColor(new Color(110,110,140)); drawLinear(x+5, y-5, g); g.setColor(new Color(230,230,230)); g.setFont(new Font("SanSerif", Font.BOLD, 13)); g.drawString(value, x+5+2,y-5+17); } public void run(Graphics g) { g.setColor(new Color(170, 170, 170)); g.fillRect((n * 28) + 24, 0, 5, (n * 28)+24); g.fillRect(0, (n * 28) + 19, (n * 28) + 24, 5); g.setColor(new Color(140, 140, 140)); g.fillRect(505,0,5,600); square = new int[n][n]; for (int i=0;i