S
From what I understand, it would be enough for you to create a class that implements https://docs.oracle.com/javase/9/docs/api/java/awt/event/ActionListener.html , and assign to all buttons. The class could be like this:class AcaoBotaoColoridoListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
JButton button = (JButton) e.getSource();
if ("lapis".equals(tipo)) {
switch (cor) {
case "cinza":
button.setBackground(new Color(101, 101, 101));
break;
case "branco":
button.setBackground(new Color(255, 255, 255));
break;
case "preto":
button.setBackground(new Color(0, 0, 0));
break;
case "azulE":
button.setBackground(new Color(0, 0, 255));
break;
case "vermelho":
button.setBackground(new Color(255, 0, 0));
break;
case "verde":
button.setBackground(new Color(0, 204, 0));
break;
case "amarelo":
button.setBackground(new Color(255, 255, 0));
break;
case "laranja":
button.setBackground(new Color(255, 204, 0));
break;
case "rosa":
button.setBackground(new Color(255, 153, 153));
break;
case "azulC":
button.setBackground(new Color(0, 204, 204));
break;
default:
break;
}
}
}
}
Then just apply to each button, using the same loop you already use to create the buttons:JButton[] button = new JButton[100];
for (int i = 0; i < 100; i++){
button[i] = new JButton();
pnlPixels.add(button[i]);
button[i].addActionListener(new AcaoBotaoColoridoListener());
}
Note: inverted your if comparison to "lapis".equals(tipo), therefore, case tipo get naked, avoid problems with https://pt.stackoverflow.com/q/119426/28595 .UpdateThere is a more "eligant" way of doing this, which is using Map to keep the colors, so you don't need to make a swith...case with such a choice.First create a type variable Map where the key will be the string with the color names, and the value will be the proper colors:private Map<String, Color> colorMap;
(...)
colorMap = new HashMap<String, Color>();
colorMap.put("cinza", new Color(101, 101, 101));
colorMap.put("branco", new Color(255, 255, 255));
colorMap.put("preto", new Color(0, 0, 0));
colorMap.put("azulE", new Color(0, 0, 255));
colorMap.put("vermelho", new Color(255, 0, 0));
colorMap.put("verde", new Color(0, 204, 0));
colorMap.put("amarelho", new Color(255, 255, 0));
colorMap.put("laranja", new Color(255, 204, 0));
colorMap.put("rosa", new Color(255, 153, 153));
colorMap.put("azulC", new Color(0, 204, 204));
Then just check if the color exists in Map and set the color to the button. See how the class got:class AcaoBotaoColoridoListener implements ActionListener {
private Map<String, Color> colorMap;
public AcaoBotaoColoridoListener() {
colorMap = new HashMap<String, Color>();
colorMap.put("cinza", new Color(101, 101, 101));
colorMap.put("branco", new Color(255, 255, 255));
colorMap.put("preto", new Color(0, 0, 0));
colorMap.put("azulE", new Color(0, 0, 255));
colorMap.put("vermelho", new Color(255, 0, 0));
colorMap.put("verde", new Color(0, 204, 0));
colorMap.put("amarelho", new Color(255, 255, 0));
colorMap.put("laranja", new Color(255, 204, 0));
colorMap.put("rosa", new Color(255, 153, 153));
colorMap.put("azulC", new Color(0, 204, 204));
}
@Override
public void actionPerformed(ActionEvent e) {
JButton button = (JButton) e.getSource();
if ("lapis".equals(tipo) && colorMap.containsKey(cor)) {
button.setBackground(colorMap.get(cor));
}
}
}
Notice that the code has now become more readable and easier to maintain.