Ver Mensaje Individual
  #1 (permalink)  
Antiguo 26/07/2018, 19:41
berseco
Invitado
 
Mensajes: n/a
Puntos:
Seleccionar color de una arraylist

Buenas, Estoy creando una lámina en cual un rectángulo quiero que seleccione el color aleatoriamente de una arraylist de colores personalizados. por favor, decirme que estoy haciendo mal.

/*
* 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.geom.*;
import java.util.*;
import javax.swing.*;

/**
*
* @author pcx
*/
public class Lamina extends JPanel {
int i=0, f, e, c, j=0, num=1, numColor=6, colore;
double pos_x=50;
//public Rectangle2D cuadro[];

public ArrayList<Color> colores, lista;
int [] jugada;
Color blanco, azul, verde, celeste, rosa, rosaOscuro, amarillo, rojo, rojoClaro, naranja, negro, morado;
Random rnd= new Random();

public Lamina(){
blanco= new Color(255,255,255);
azul= new Color(0,0,125);
verde= new Color(0,128,0);
celeste= new Color(0,255,255);
rosa= new Color(255,0,255);
rosaOscuro= new Color(128,0,128);
amarillo= new Color(255,255,0);
rojo= new Color(255,0,0);
rojoClaro= new Color(255,192,203);
naranja= new Color(255,165,0);
blanco= new Color(0,0,0);
morado= new Color(84,84,168);
}
public void cargar() {
colores= new ArrayList<Color>();
colores.add(0, azul);
colores.add(1, verde);
colores.add(2,celeste);
colores.add(3, rosa);
colores.add(4, rosaOscuro);
colores.add(5, amarillo);
colores.add(6, rojo);
colores.add(7, rojoClaro);
colores.add(8, naranja);
colores.add(9, blanco);
colores.add(10, morado);
}

public void paintComponent(Graphics g) {


super.paintComponent(g);
Graphics2D g2= (Graphics2D) g;
numColor=4;
while(numColor!=0){
Rectangle2D cuadro= new Rectangle2D.Double(pos_x, 100, 250, 250);



j= (int) Math.round(Math.random()*5);

g2.setColor(colores.get(i));
g2.fill(cuadro);
g2.draw(cuadro);
// System.out.println(colores.toArray());
pos_x+= 350;
numColor--;
// lista.add(colores.get(j));
// colores.remove(j);

// jugada= new int[numColor];
}

/*
for(f=0; f<jugada.length; f++) {

j= rnd.nextInt(colores.length);

jugada[f]=j;
for(int c=0; c<f; c++) {

System.out.println("indice c " + c + " array c" + jugada[c] + " num aleatorio " + j );
if(jugada[f]== jugada[c]) {

j= rnd.nextInt(colores.length);
jugada[f]= j;
f=0;
}


}
System.out.println("indicee c " + c + " array c " + jugada[c] + " num aleatorio " + j );

Rectangle2D cuadro= new Rectangle2D.Double(pos_x, 70, 50, 50);


g2.setColor(new Color(hex(colores[jugada[f] ])));
g2.fill(cuadro);
g2.draw(cuadro);

System.out.println("Color " + g2.getBackground() );


pos_x+= 350;


*/
}
}


Grecias