import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Feldsalat {
public MyFrame frame;
public boolean circle = true;
IconButton[] button;
JPanel[] panel;
ImageIcon kreis = new ImageIcon(Feldsalat.class.getResource("Kreis.gif"));
ImageIcon kreuz = new ImageIcon(Feldsalat.class.getResource("Kreuz.gif"));
public Feldsalat(int n) {
this(n, n);
}
public Feldsalat(int n, int m) {
frame = new MyFrame(n, m);
frame.add(new MyPanel(n, m));
frame.setVisible(true);
}
class MyFrame extends JFrame {
public MyFrame(int n, int m) {
this.setTitle("Feldsalat");
this.setSize(n * 100, m * 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class MyPanel extends JPanel implements ActionListener {
public MyPanel(int n, int m) {
int buttonwidth = (int) frame.getWidth()/n;
int buttonheight = (int) frame.getHeight()/m;
int buttonsize = 0;
if (buttonwidth > buttonheight)
buttonsize = buttonheight;
buttonsize = buttonwidth;
button = new IconButton[n * m];
panel = new JPanel[n * m];
GridLayout gl = new GridLayout(n, m, 0, 0);
this.setLayout(gl);
for (int i = 0; i < n * m; i++) {
panel[i] = new JPanel();
button[i] = new IconButton(buttonsize, buttonsize);
button[i].setActionCommand(new Integer(i).toString());
button[i].addActionListener(this);
//panel[i].setLayout(new BorderLayout());
panel[i].add(BorderLayout.CENTER, button[i]);
this.add(panel[i]);
}
}
@Override
public void actionPerformed(ActionEvent event) {
int i = Integer.valueOf(event.getActionCommand()).intValue();
button[i].setBackground(Color.WHITE);
button[i].setEnabled(false);
if (circle) {
button[i].setIcon(kreis);
circle = false;
} else {
button[i].setIcon(kreuz);
circle = true;
}
}
}
class IconButton extends JButton {
private int oldWidth = 0;
private int oldHeight = 0;
private ImageIcon orgImage = null;
private ImageIcon scaledImage = null;
public IconButton(int width, int height) {
super.setSize(new Dimension(width,height));
if (height > width) {
super.setPreferredSize(new Dimension(height,height));
} else {
super.setPreferredSize(new Dimension(width,width));
}
this.oldWidth = width;
this.oldHeight = height;
this.setPreferredSize(new Dimension(width, height));
}
private Image scaleIcon(Icon i, int width, int height) {
if (i == null)
return null;
return new ImageIcon(((ImageIcon) i).getImage().getScaledInstance(
width, height, Image.SCALE_AREA_AVERAGING)).getImage();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if ((getWidth() != oldWidth) || (getHeight() != oldHeight))
rescaleImage();
if (scaledImage != null)
g.drawImage(scaledImage.getImage(), 0, 0, this);
}
private ImageIcon rescaleImage() {
oldWidth = getWidth();
oldHeight = getHeight();
orgImage = (ImageIcon) getIcon();
Object result = scaleIcon(orgImage, oldWidth, oldHeight);
scaledImage = (result == null) ? null : (new ImageIcon(
(Image) result));
return scaledImage;
}
@Override
public void setIcon(Icon i) {
if ((getWidth() == 0) || (getHeight() == 0)) {
super.setIcon(i);
} else {
super.setIcon(i);
rescaleImage();
}
}
}
}