fork download
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import javax.swing.border.*;
  5. import static java.lang.Math.*;
  6.  
  7. class Field {
  8. Color[][] mass;
  9. public Field() {
  10. this.mass = new Color[12][12];
  11. }
  12. public void putColor(int i, int j, Color c) {
  13. this.mass[i][j] = c;
  14. }
  15. public Color readColor(int i, int j) {
  16. return this.mass[i][j];
  17. }
  18. public void clear() {
  19. for(int i=0; i<mass.length; i++) {
  20. for(int j=0; j<mass[i].length; j++) {
  21. this.mass[i][j] = Color.WHITE;
  22. }
  23. }
  24. }
  25. public void changeMassColor(int i, int j) {
  26. int r = (int)(random() * 255);
  27. int g = (int)(random() * 255);
  28. int b = (int)(random() * 255);
  29. putColor(i, j, new Color(r, g, b));
  30. }
  31. public void changeAllColor() {
  32. for(int i=0; i<mass.length; i++) {
  33. for(int j=0; j<mass[i].length; j++) {
  34. changeMassColor(i, j);
  35. }
  36. }
  37. }
  38. public Color getColor(int i, int j) {
  39. return mass[i][j];
  40. }
  41. }
  42.  
  43. class MyFrame extends JFrame implements ActionListener, MouseListener {
  44. JLabel[][] labelss;
  45. Field field;
  46. JButton btnChange;
  47. public static final int WIDTH = 20;
  48. public static final int HEIGHT = 20;
  49. public MyFrame() {
  50. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  51. JPanel p = new JPanel(new BorderLayout());
  52. JPanel center = new JPanel(new GridLayout(12, 12));
  53. this.labelss = new JLabel[12][12];
  54. LineBorder border = new LineBorder(Color.BLACK, 1, false);
  55. for(int i=0; i<labelss.length; i++) {
  56. for(int j=0; j<labelss[i].length; j++) {
  57. this.labelss[i][j] = new JLabel();
  58. this.labelss[i][j].setPreferredSize(new Dimension(WIDTH, HEIGHT));
  59. this.labelss[i][j].setBorder(border);
  60. this.labelss[i][j].setOpaque(true);
  61. this.labelss[i][j].addMouseListener(this);
  62. center.add(labelss[i][j]);
  63. }
  64. }
  65. this.btnChange = new JButton("Random");
  66. this.btnChange.addActionListener(this);
  67. p.add(center, BorderLayout.CENTER);
  68. p.add(btnChange, BorderLayout.SOUTH);
  69. this.add(p);
  70. this.field = new Field();
  71. this.pack();
  72. }
  73.  
  74. @Override public void actionPerformed(ActionEvent e) {
  75. field.changeAllColor();
  76. for(int i=0; i<labelss.length; i++) {
  77. for(int j=0; j<labelss[i].length; j++) {
  78. labelss[i][j].setBackground(field.getColor(i, j));
  79. }
  80. }
  81. }
  82.  
  83. @Override public void mouseClicked(MouseEvent e) {
  84. int x = e.getX();
  85. int y = e.getY();
  86. JLabel source = (JLabel)e.getSource();
  87. field.changeMassColor(x/WIDTH, y/HEIGHT);
  88. source.setBackground(field.getColor(x/WIDTH, y/HEIGHT));
  89. }
  90. @Override public void mouseEntered(MouseEvent e) {}
  91. @Override public void mouseExited(MouseEvent e) {}
  92. @Override public void mousePressed(MouseEvent e) {}
  93. @Override public void mouseReleased(MouseEvent e) {}
  94. }
  95.  
  96. public class Main {
  97. public static void main(String[] args) {
  98. new MyFrame().setVisible(true);
  99. }
  100. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty