1
votes

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.
}

}

2
Painting at 100x100 could be painting beyond the visible range of the component. ImageIcon(String) expects that the String is a file reference, so it the image is an embedded resource, the image will not be found. Try using ImageIO.read instead, as it will at least throw an IOException when something goes wrong...MadProgrammer
The component has dimensions 720x528. I use ImageIO.read before but that also didn't work. At the moment, I just put the image file in the same directory as the java file to test it as reading straight from the file reference should work this way.ixin
First of all, you didn't provide enough information to make a "guess" about the component size. Second if the image file is in the same "package" as the class file (rpg) then the file path is wrong, from the context of the application, it would be more like rpg/SpriteSheet.png. You could do System.out.println(new File("SpriteSheet.png").exists()); to test it, it will most likely print false. If the SpriteSheet.png is within the context of the application, then you should try using ImageIO.read(getClass().getResource("SpriteSheet.png")) instead.MadProgrammer
Sorry about the component size, forgot to say that in my opening post. It did in fact say "false" so I guess the issue may be the path name? It still said false after adding "rpg/SpriteSheet.png". I'm not that knowledgeable about default pathnames. If I have my image in the same package as my java file what would I use. Also if i wanted my images to be in a separate folder, where should I put the folder and how would I reference the images?ixin
It's possible that the sprite has not being copied to the same location of the classes or is being treated as an embedded resource. How are you build your application? Did you try something ImageIO.read(getClass().getResource("SpriteSheet.png"))?MadProgrammer

2 Answers

1
votes

Summary of comments:

  • The SpriteSheet.png is not located within location that ImageIcon(String) would be able to find it. ImageIcon(String) expects that the String references a File on the file system, but SpriteSheet.png is stored within the application context (src/rpg/SpriteSheet.png), this makes it an embedded resource.
  • Use Class#getResource to load embedded resources, in this case either getClass().getResource("SpriteSheet.png") or getClass().getResource("/rpg/SpriteSheet.png") to be be sure.
  • Use ImageIO.read over ImageIcon. It will at least throw an IOException when the image can not be loaded for some reason, where as ImageIcon can fail silently.
  • Make sure that the instance of GraphicsPanel which is loading the resources is the same instance that is on the screen
0
votes

The following works for me. I took your code, and added the panel to a JFrame. Also, I call getParameters(); before adding the Panel to the JFrame.

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.JFrame;
import javax.swing.JLabel;
import java.awt.image.BufferedImage;

public class GraphicsPanel extends JPanel implements MouseListener,
        MouseMotionListener {

    public static void main(String args[]) {
        GraphicsPanel s = new GraphicsPanel();
        s.getParameters();
        JFrame frame = new JFrame();
        frame.add(s);       
        frame.setVisible(true);     
    }

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    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.drawImage(currentTile, 100, 100, this);//Nothing gets drawn here
    }

    public void getParameters() {
        currentTile = new ImageIcon("test.jpg").getImage();
        repaint();
    }

    @Override
    public void mouseDragged(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseMoved(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseClicked(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseEntered(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseExited(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mousePressed(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseReleased(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }   
}