import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import static java.
lang.
Math.
*;
this.
mass = new Color[12][12]; }
public void putColor
(int i,
int j,
Color c
) { this.mass[i][j] = c;
}
public Color readColor
(int i,
int j
) { return this.mass[i][j];
}
public void clear() {
for(int i=0; i<mass.length; i++) {
for(int j=0; j<mass[i].length; j++) {
this.
mass[i
][j
] = Color.
WHITE; }
}
}
public void changeMassColor(int i, int j) {
int r = (int)(random() * 255);
int g = (int)(random() * 255);
int b = (int)(random() * 255);
putColor
(i, j,
new Color(r, g, b
)); }
public void changeAllColor() {
for(int i=0; i<mass.length; i++) {
for(int j=0; j<mass[i].length; j++) {
changeMassColor(i, j);
}
}
}
public Color getColor
(int i,
int j
) { return mass[i][j];
}
}
public static final int WIDTH = 20;
public static final int HEIGHT = 20;
public MyFrame() {
this.
setDefaultCloseOperation(JFrame.
EXIT_ON_CLOSE); this.
labelss = new JLabel[12][12]; for(int i=0; i<labelss.length; i++) {
for(int j=0; j<labelss[i].length; j++) {
this.
labelss[i
][j
] = new JLabel(); this.
labelss[i
][j
].
setPreferredSize(new Dimension(WIDTH, HEIGHT
)); this.labelss[i][j].setBorder(border);
this.labelss[i][j].setOpaque(true);
this.labelss[i][j].addMouseListener(this);
center.add(labelss[i][j]);
}
}
this.
btnChange = new JButton("Random"); this.btnChange.addActionListener(this);
this.add(p);
this.
field = new Field(); this.pack();
}
field.changeAllColor();
for(int i=0; i<labelss.length; i++) {
for(int j=0; j<labelss[i].length; j++) {
labelss[i][j].setBackground(field.getColor(i, j));
}
}
}
@Override
public void mouseClicked
(MouseEvent e
) { int x = e.getX();
int y = e.getY();
field.changeMassColor(x/WIDTH, y/HEIGHT);
source.setBackground(field.getColor(x/WIDTH, y/HEIGHT));
}
@Override
public void mouseEntered
(MouseEvent e
) {} @Override
public void mouseExited
(MouseEvent e
) {} @Override
public void mousePressed
(MouseEvent e
) {} @Override
public void mouseReleased
(MouseEvent e
) {}}
public class Main {
public static void main
(String[] args
) { new MyFrame().setVisible(true);
}
}
aW1wb3J0IGphdmEuYXd0Lio7CmltcG9ydCBqYXZhLmF3dC5ldmVudC4qOwppbXBvcnQgamF2YXguc3dpbmcuKjsKaW1wb3J0IGphdmF4LnN3aW5nLmJvcmRlci4qOwppbXBvcnQgc3RhdGljIGphdmEubGFuZy5NYXRoLio7CgpjbGFzcyBGaWVsZCB7CiAgICBDb2xvcltdW10gbWFzczsKICAgIHB1YmxpYyBGaWVsZCgpIHsKICAgICAgICB0aGlzLm1hc3MgPSBuZXcgQ29sb3JbMTJdWzEyXTsKICAgIH0KICAgIHB1YmxpYyB2b2lkIHB1dENvbG9yKGludCBpLCBpbnQgaiwgQ29sb3IgYykgewogICAgICAgIHRoaXMubWFzc1tpXVtqXSA9IGM7CiAgICB9CiAgICBwdWJsaWMgQ29sb3IgcmVhZENvbG9yKGludCBpLCBpbnQgaikgewogICAgICAgIHJldHVybiB0aGlzLm1hc3NbaV1bal07CiAgICB9CiAgICBwdWJsaWMgdm9pZCBjbGVhcigpIHsKICAgICAgICBmb3IoaW50IGk9MDsgaTxtYXNzLmxlbmd0aDsgaSsrKSB7CiAgICAgICAgICAgIGZvcihpbnQgaj0wOyBqPG1hc3NbaV0ubGVuZ3RoOyBqKyspIHsKICAgICAgICAgICAgICAgIHRoaXMubWFzc1tpXVtqXSA9IENvbG9yLldISVRFOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQogICAgcHVibGljIHZvaWQgY2hhbmdlTWFzc0NvbG9yKGludCBpLCBpbnQgaikgewogICAgICAgIGludCByID0gKGludCkocmFuZG9tKCkgKiAyNTUpOwogICAgICAgIGludCBnID0gKGludCkocmFuZG9tKCkgKiAyNTUpOwogICAgICAgIGludCBiID0gKGludCkocmFuZG9tKCkgKiAyNTUpOwogICAgICAgIHB1dENvbG9yKGksIGosIG5ldyBDb2xvcihyLCBnLCBiKSk7CiAgICB9CiAgICBwdWJsaWMgdm9pZCBjaGFuZ2VBbGxDb2xvcigpIH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