problemas con graphics e hilos

iratxegc
11 de Mayo del 2004
Hola a todos. Tengo un problema con un ejercicio que estamos haciendo en clase. Dado un hilo que dibuja burbujas queremos poder dibujar tantas burbujas como queramos pero al intentar pasar un elemento graphics como parametro nos da error. Incluyo el código por si alguien pudiera ayudarme. MUCHISIMAS GRACIAS de antemano
Un saludo
Este es el hilo que dibuja la burbuja
package burbujita;
import java.awt.*;
import java.lang.*;
import java.util.*;

/**
* <p>Título: </p>
* <p>Descripción: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Empresa: </p>
* @author sin atribuir
* @version 1.0
*/

public class BurbujaHilo extends Thread{
static final int espera=20;//para ver burbuja
static final int incmax=7;//para mover burbujas
int radio;//el radio de las burbujas
Graphics gra;
int posX,posY,incX,incY,ancho,largo;
Random rnd;

public BurbujaHilo(int ancho,int largo,Graphics gr) {//constructor
this.ancho = ancho;
this.largo=largo;
gra=gr;
rnd=new Random();
radio=rnd.nextInt(15)+10;//me genera radio entre 0 y 14 +10
//posicion inicial de la burbuja
posX = rnd.nextInt(ancho);
posY = rnd.nextInt(largo);
// Obtain the initial increases in coordinates for the bubble. Use a loop to
// avoid values equal to zero.
do {
incX = rnd.nextInt() % incmax;
} while (incX == 0);
do {
incY = rnd.nextInt() % incmax;
} while (incY == 0);
start();
}

/**
* run
*/
public void run() {//main de hilos
while (true) {

// Set the color black and draw the bubble in the current position
gra.setColor(Color.black);
gra.drawOval(posX,posY, radio - 1 , radio - 1);

// Delay the execution to guarantee a visible effect
try {
sleep(espera);
}
catch (InterruptedException ex) {
}

// Erase the bubble in the current position by drawing a bubble with
// background color
gra.setColor(Color.white);
gra.drawOval(posX,posY, radio - 1, radio - 1);

// Update the bubble's position
posX += incX;
posY += incY;

// If the future position goes out of bounds, change the sign of the
// increment in both dimensions
if (posX + radio > ancho) {
posX = ancho - radio;
incX = -incX;
}
else if (posX < 0) {
posX = 0;
incX = -incX;
}

if (posY + radio >largo) {
posY = largo - radio;
incY = -incY;
}
else if (posY < 0) {
posY = 0;
incY = -incY;
}
}

}

}

Y este es el codigo que lanza las burbujas
package burbujita;

import javax.swing.*;
import java.awt.*;

/**
* <p>Título: </p>
* <p>Descripción: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Empresa: </p>
* @author sin atribuir
* @version 1.0
*/

public class LanzaBurbujas extends JFrame {
BorderLayout borderLayout1 = new BorderLayout();
//Container elcontenedor;
static Graphics g;
//Canvas elcanvas= new Canvas();
static final int elancho=300;
static final int ellargo=300;
JPanel jPanel1 = new JPanel();


public LanzaBurbujas() {
try {
jbInit();
}
catch(Exception ex) {
ex.printStackTrace();
}
}

void jbInit() throws Exception {
this.getContentPane().setLayout(borderLayout1);
//elcontenedor=this.getContentPane();
jPanel1.setBounds(0, 0, elancho, ellargo);
jPanel1.setBackground(Color.white);
//elcontenedor.add(elcanvas, borderLayout1.CENTER);
g = jPanel1.getGraphics();
this.getContentPane().add(jPanel1, BorderLayout.CENTER);
}

public static void main(String[] args) {
LanzaBurbujas lanzaBurbujas = new LanzaBurbujas();
BurbujaHilo hilo1=new BurbujaHilo(elancho,ellargo,g);
BurbujaHilo hilo2=new BurbujaHilo(elancho,ellargo,g);
BurbujaHilo hilo3=new BurbujaHilo(elancho,ellargo,g);
BurbujaHilo hilo4=new BurbujaHilo(elancho,ellargo,g);
BurbujaHilo hilo5=new BurbujaHilo(elancho,ellargo,g);
try {
Thread.sleep(20000);//duermo al main
}
catch (InterruptedException ex) {
}
hilo1.destroy();
hilo2.destroy();
hilo3.destroy();
hilo4.destroy();
hilo5.destroy();
}
}

muchas gracias

ALM
11 de Mayo del 2004
pues yo no lo se ya que tengo el mismo problema ;) .....