user3211411
user3211411

Reputation: 3

Setting JPanel image from file

I'm working on a Yahtzee game and am having trouble loading a dice image for my dice. I'm using NetBeans with the JFrame component, but would like to handle the file directly in my class as the image will need to change when the die is rolled.

Here's my code...that doesn't work...`

public class Die extends JPanel {

    //current number of die.  Starts with 1.
    private int number;
    //boolean showing whether user wants to roll this die
    private boolean userSelectToRoll;
    private Random generate;
    private boolean rolled;
    private Graphics2D g;
    private BufferedImage image;


    public Die() {
        this.userSelectToRoll = true;
        this.generate = new Random();
        this.rolled = false;
        try{
            image = ImageIO.read(new File("src/dice1.png"));
        }catch (IOException ex){
     //       System.out.println("Dice picture error");
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        g.drawImage(image, 0, 0, null);
    }
}`

I've also tried using a jLabel icon and it also has not worked. When I try to debug it, I get an error that says:

non-static method tostring() cannot be referenced from a static context

But I don't understand because I'm not calling a toString() method and I can't figure out what is. I have used file images successfully in other programs but can not get this one to work! Any help would be appreciated!

Upvotes: 0

Views: 79

Answers (1)

Paul Samsotha
Paul Samsotha

Reputation: 208994

Not sure if this is the problem, but you should read the image from a URL path for embedded resources

image = ImageIO.read(Die.class.getResource("dice.png"));

Notice how I don't need the src in the path. Run the below code and see if it works for you. It works fine for me (given the path change)

And BTW, I don't get the error about the toString with your code. And use descriptive things like ex.printStackTrace() in your catch blocks so you can see what the actual exception is if one is thrown. Oh and use the JPanel and the ImageObserver, this in drawImage

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;


public class Die extends JPanel {

//current number of die.  Starts with 1.
    private int number;
//boolean showing whether user wants to roll this die
    private boolean userSelectToRoll;
    private Random generate;
    private boolean rolled;
    private Graphics2D g;
    private BufferedImage image;

    public Die() {
        this.userSelectToRoll = true;
        this.generate = new Random();
        this.rolled = false;
        try {
            image = ImageIO.read(Die.class.getResource("stackoverflow5.png"));
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        g.drawImage(image, 0, 0, this);
    }

    public Dimension getPreferredSize() {
        return new Dimension(400, 400);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new Die());
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}

Upvotes: 2

Related Questions