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

public class SDL extends MiniJava {
	private static final Object syncObj = new Object();
	private static final int w = 20;

	private static class But extends JButton {
		boolean[][] arena;
		int i,j;

		But(boolean[][] _arena, int _i, int _j) {
			arena = _arena;
			i = _i;
			j = _j;
			setLabel();
			addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e) {
					arena[i][j] = !arena[i][j];
					setLabel();
				}
			});
			setLocation(w*j,w*i);
		}

		void setLabel() {
			if (arena[i][j])
				setBackground(Color.RED);
			else
				setBackground(Color.BLUE);
			setSize(w,w);
		}
	}

	private static class UpdateArenaFrm extends JFrame {
		UpdateArenaFrm(boolean[][] arena) {
			JPanel panel = new JPanel(null);
			panel.setPreferredSize(new Dimension(400,400));
			panel.setMinimumSize(new Dimension(400,400));
			JButton okBut = new JButton("Weiter!");
			getContentPane().add(BorderLayout.SOUTH, okBut);
			okBut.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e) {
					dispose();
					synchronized(syncObj) {
						syncObj.notifyAll();
					}
				}
			});
			addWindowListener(new WindowAdapter() {
				public void windowClosing(WindowEvent e) {
					System.exit(0);
				}
			});
			getContentPane().add(panel);
			for (int i=0; i<arena.length; i++)
				for (int j=0; j<arena[i].length; j++) {
					panel.add(new But(arena,i,j));
				}
			pack();
		}
	}

	private static JFrame frm;

	public static void updateArena(boolean[][] arena) {
		UpdateArenaFrm frm = new UpdateArenaFrm(arena);
		frm.setVisible(true);
		synchronized (syncObj) {
			try {
				syncObj.wait();
			} catch(Exception e) {
				//nichts zu tun
			}
		}
	}

	public static void update(boolean[][] arena) {
		updateArena(arena);
	}
}

