Estoy intentando aprender java y en este, que es mi primer código hecho por mi desde 0. Me da unos errores que no sé porque da los resultados que da al ejecutarlo.
La lámina que he hecho es la siguiente:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package buscacolor;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
/**
*
* @author Jose Alberto
*/
public class lamina2 extends JPanel {
public ArrayList<String> colores, lista;
public JButton boton;
private JButton boto[]= new JButton[6];
private JPanel pane1;
int posx=30, j=0, i=0;
String elcolor, color;
public lamina2() {
setLayout(new BorderLayout());
juego();
}
public void cargar() {
colores= new ArrayList();
colores.add("#FFFFFF");
colores.add("#000080");
colores.add("#008000");
colores.add("#00FFFF");
colores.add("#FF00FF");
colores.add("#800080");
colores.add("#FFFF00");
colores.add("#FF0000");
colores.add("#FFC0CB");
colores.add("#FFA500");
colores.add("#000000");
colores.add("#5454a8");
}
public void juego() {
cargar();
pane1= new JPanel();
pane1.setLayout(new GridLayout(3,8,3,3));
add(pane1, BorderLayout.CENTER);
Random rnd= new Random();
lista= new ArrayList();
JButton []pulsa= new JButton[4];
for( i=0; i<4; i++) {
j= rnd.nextInt(colores.size());
boto[i]= new JButton("");
//boton.setBounds(175, 175, posx, 20);
boto[i].setName("boto"+i);
boto[i].setBackground(Color.decode(String.format(colores. get(j));
boto[i].addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
System.out.println(i);
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
});
pane1.add(boto[i]);
posx += 45;
lista.add(colores.get(j));
colores.remove(j);
}
j= rnd.nextInt(lista.size());
elcolor= NomColor(lista.get(j));
boton= new JButton("Busca el color " + elcolor);
add(boton, BorderLayout.NORTH);
}
public String NomColor(String color) {
switch(color) {
case "#FFFFFF":
elcolor= "Blanco";
break;
case "#000080":
elcolor= "Azul";
break;
case "#008000":
elcolor= "Verde";
break;
case "#00FFFF":
elcolor= "Celeste";
break;
case "#FF00FF":
elcolor= "Rosa";
break;
case "#800080":
elcolor= "Rosa oscuro";
break;
case "#FFFF00":
elcolor= "Amarillo";
break;
case "#FF0000":
elcolor= "Rojo";
break;
case "#FFC0CB":
elcolor= "Rojo claro";
break;
case "#FFA500":
elcolor= "Naranja";
break;
case "#000000":
elcolor= "Negro";
break;
case "#5454a8":
elcolor= "Morado";
break;
}
return elcolor;
}
}
Al ejecutar…..
public void mouseClicked(MouseEvent e) {
System.out.println(i);
}
En los cuatro botones el resultado es siempre 4.
Y si pongo……….
@Override
public void mouseClicked(MouseEvent e) {
boto[i].getBackground();
}
Sale los errores….
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at buscacolor.lamina2$1.mouseClicked(lamina2.java:70)
at java.awt.AWTEventMulticaster.mouseClicked(AWTEvent Multicaster.java:270)
at java.awt.Component.processMouseEvent(Component.jav a:6538)
at javax.swing.JComponent.processMouseEvent(JComponen t.java:3324)
at java.awt.Component.processEvent(Component.java:630 0)
at java.awt.Container.processEvent(Container.java:223 6)
at java.awt.Component.dispatchEventImpl(Component.jav a:4891)
at java.awt.Container.dispatchEventImpl(Container.jav a:2294)
at java.awt.Component.dispatchEvent(Component.java:47 13)
at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4534)
at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4466)
at java.awt.Container.dispatchEventImpl(Container.jav a:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2750 )
at java.awt.Component.dispatchEvent(Component.java:47 13)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessI mpl.doIntersectionPrivilege(ProtectionDomain.java: 76)
at java.security.ProtectionDomain$JavaSecurityAccessI mpl.doIntersectionPrivilege(ProtectionDomain.java: 86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessI mpl.doIntersectionPrivilege(ProtectionDomain.java: 76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 728)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:82)
Por favor, orientarme un poquito para el ActionListener actúen cada botón de forma individual.
Gracias