Java - Ayuda getGraphics()

 
Vista:

Ayuda getGraphics()

Publicado por Noelia (2 intervenciones) el 18/02/2009 20:54:41
Hola, tengo un problema con el siguiente codigo:

public class PruebaNube extends JPanel{

int numImagenes = 3;
Imagen imagenes[];

Image buffer;
Graphics pantallaVirtual;

public PruebaNube(){
this.setBackground(new Color(255,255,255));

if(buffer == null){
buffer = createImage(this.getWidth(), this.getHeight());
}

pantallaVirtual = buffer.getGraphics();
}
}


Este codigo realmente no tiene funcionalidad, pero simplemente con esto ya me salta la Excepcion NullPointerExcepcion. El problema es que ---buffer = createImage(this.getWidth(), this.getHeight())--- me devuelve null; y por lo tanto al hacer ---pantallaVirtual = buffer.getGraphics();- salta dicha excepcion.
He buscado por todos lados pero no doy con la solucion, por favor espero su ayuda.

Muchas gracias
Un saludo.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

RE:Ayuda getGraphics()

Publicado por Tom (1831 intervenciones) el 19/02/2009 13:50:28
Prueba a hacer this.setVisible(true) antes de crear la Image;

Pero de todos modos no creo que funcione hasta que no coloques tu PruebaNube en un componente de primer nivel (un JFrame, por ejemplo).
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:Ayuda getGraphics()

Publicado por Noelia (2 intervenciones) el 19/02/2009 15:27:13
Hola de nuevo,
no lo dije en el anterior mensaje, pero tengo creada la clase principal donde coloco mi PruebaNube en un JFrame; a continuación pongo el codigo:

public class PrincipalPrueba {


public static void main(String[] args) throws MalformedURLException {

PruebaNube panelEscribir = new PruebaNube();
JFrame ventana = new JFrame("PRUEBA");
ventana.setContentPane(panelEscribir);
ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ventana.setVisible(true);
ventana.setSize(1280, 1024);
ventana.setAlwaysOnTop(true)
}
}

La clase PruebaNube la he modificado añadiendole el metodo paint(), a continuación lo muestro:

public class PruebaNube extends JPanel{

int numImagenes = 1;
Imagen imagenes[];

int posX,posY;

Image buffer;
Graphics pantallaVirtual;

public PruebaNube(){
this.setVisible(true);
this.setPreferredSize(new Dimension(500,500));
this.setMaximumSize(this.getPreferredSize());
}

public void paint(Graphics g) {
update(g);
}

public void update(Graphics g) {

this.setVisible(true);
buffer = this.createImage(this.getWidth(), this.getHeight());
pantallaVirtual = buffer.getGraphics();

imagenes = new Imagen [numImagenes];

for (int j=0;j<numImagenes;j++){
imagenes[j]= new Imagen();
}

Image image = null;

for (int i=0;i<numImagenes;i++){
image = Toolkit.getDefaultToolkit().getImage( "/resources/dibujo.png" );

imagenes[i].setImagen(image);
imagenes[i].setPosX(Integer.parseInt("100"));
imagenes[i].setPosY(Integer.parseInt("100"));
imagenes[i].setTamX(Integer.parseInt("150"));
imagenes[i].setTamY(Integer.parseInt("200"));

}

pantallaVirtual.drawImage(imagenes[0].getImagen(),imagenes[0].getPosX(),imagenes[0].getPosY(), this);
g.drawImage(buffer, 0, 0, this);
}
}

De esta manera ya no me salta la excepcion, pero no se me muestra ninguna imagen.

Espero que me podais ayudar.

Un saludo
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar