Ayuda con imagen
¿Alguien podrÃa decirme porque no se me muestra la imagen? La ruta esta bien. No entiendo porque se dibuja. Gracias.
/*
* @(#)CanvasPrincipal.java
*
* This work is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This work is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* Copyright (c) 2010 Per Andros Fenollosa Hurtado. All rights reserved.
*/
package gnu.andros.nodo;
/* Librerias */
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import javax.swing.ImageIcon;
/**
* Esta clase se encarga de dibujar el juego
* @author Andros Fenollosa Hurtado, [email protected]
* @version 1.0 (20-1-2010)
*/
public class CanvasPrincipal extends Canvas {
/* Atributos estaticos */
/* Atributos dinamicos */
ImageIcon iBloque = null;
Image img = null;
/**
* Constructor
*/
public CanvasPrincipal() {
iBloque = new ImageIcon(getClass().getResource("niveles" + File.separator + "spriters" + File.separator + "bloque.png").toString());
img = iBloque.getImage();
}
/* Metodos publicos */
public void paint(Graphics g) {
g.setColor(Color.red);
pintarNivel(g);
System.out.println(iBloque.getDescription());
}
/* Metodos privados */
private void pintarNivel(Graphics g) {
for(int i = 0; i < Vista.SAABNIVEL[0].length; i++) {
for(int j = 0; j < Vista.SAABNIVEL.length; j++) {
if(Vista.SAABNIVEL[j][i] == 0) {
g.drawImage(img, j * 20, i * 20, iBloque.getImageObserver());
}
}
}
}
}//Fin de la clase
/*
* @(#)CanvasPrincipal.java
*
* This work is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This work is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* Copyright (c) 2010 Per Andros Fenollosa Hurtado. All rights reserved.
*/
package gnu.andros.nodo;
/* Librerias */
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import javax.swing.ImageIcon;
/**
* Esta clase se encarga de dibujar el juego
* @author Andros Fenollosa Hurtado, [email protected]
* @version 1.0 (20-1-2010)
*/
public class CanvasPrincipal extends Canvas {
/* Atributos estaticos */
/* Atributos dinamicos */
ImageIcon iBloque = null;
Image img = null;
/**
* Constructor
*/
public CanvasPrincipal() {
iBloque = new ImageIcon(getClass().getResource("niveles" + File.separator + "spriters" + File.separator + "bloque.png").toString());
img = iBloque.getImage();
}
/* Metodos publicos */
public void paint(Graphics g) {
g.setColor(Color.red);
pintarNivel(g);
System.out.println(iBloque.getDescription());
}
/* Metodos privados */
private void pintarNivel(Graphics g) {
for(int i = 0; i < Vista.SAABNIVEL[0].length; i++) {
for(int j = 0; j < Vista.SAABNIVEL.length; j++) {
if(Vista.SAABNIVEL[j][i] == 0) {
g.drawImage(img, j * 20, i * 20, iBloque.getImageObserver());
}
}
}
}
}//Fin de la clase
Andros,
No te muestra nada porque te falta el método main(), pon algo como esto y podrás ver las imágenes o al menos te lanzará los errores:
public static void main(String[] args) {
new CanvasPrincipal();
}
No te muestra nada porque te falta el método main(), pon algo como esto y podrás ver las imágenes o al menos te lanzará los errores:
public static void main(String[] args) {
new CanvasPrincipal();
}
El main lo tenÃa en otro archivo.
Ya lo he solucionado: en la ruta no hay que transformala en String. En vez de :
iBloque = new ImageIcon(getClass().getResource("niveles" + File.separator + "spriters" + File.separator + "bloque.png").toString());
SerÃa:
iBloque = new ImageIcon(getClass().getResource("niveles" + File.separator + "spriters" + File.separator + "bloque.png"));
Gracias de todos modos
Ya lo he solucionado: en la ruta no hay que transformala en String. En vez de :
iBloque = new ImageIcon(getClass().getResource("niveles" + File.separator + "spriters" + File.separator + "bloque.png").toString());
SerÃa:
iBloque = new ImageIcon(getClass().getResource("niveles" + File.separator + "spriters" + File.separator + "bloque.png"));
Gracias de todos modos
