I'm having trouble painting an image in my GraphicsPanel (extension of JPanel). I tried loading from a file with the pathname, using getCodeBase(), getDocumentBase(), getResource(), and using BufferedImage. Is there any way to draw the image without having to make it an ImageIcon inside a JLabel?
package rpg;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JPanel;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import java.awt.image.BufferedImage;
public class GraphicsPanel extends JPanel implements MouseListener, MouseMotionListener {
private WorldBuilder wb;
public int currentTileType = 0;//tile types. 0=bgtile, 1=object, 2=NPC
public String currentTileName = "";
public Image currentTile;
public GraphicsPanel() {
super();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.red);
g.fillRect(0, 0, 720, 528);
g.drawImage(currentTile, 100, 100, this);//Nothing gets drawn here
}
public void getParameters(WorldBuilder wb) {
this.wb = wb;
this.currentTileType = wb.currentTileType;
this.currentTileName = wb.currentTileName;
/*
try {
currentTile = ImageIO.read(new File("SpriteSheet.png"));
} catch (IOException e) {
System.out.println("failed");
}
*/
currentTile = new ImageIcon("SpriteSheet.png").getImage();
repaint();
}
@Override
public void mouseClicked(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void mousePressed(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void mouseReleased(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void mouseEntered(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void mouseExited(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void mouseDragged(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void mouseMoved(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
100x100
could be painting beyond the visible range of the component.ImageIcon(String)
expects that theString
is a file reference, so it the image is an embedded resource, the image will not be found. Try usingImageIO.read
instead, as it will at least throw anIOException
when something goes wrong... – MadProgrammerrpg
) then the file path is wrong, from the context of the application, it would be more likerpg/SpriteSheet.png
. You could doSystem.out.println(new File("SpriteSheet.png").exists());
to test it, it will most likely printfalse
. If theSpriteSheet.png
is within the context of the application, then you should try usingImageIO.read(getClass().getResource("SpriteSheet.png"))
instead. – MadProgrammerImageIO.read(getClass().getResource("SpriteSheet.png"))
? – MadProgrammer