Jerryberry123
Jerryberry123

Reputation: 25

Drawing this Image in my Java Applet wont work

import java.awt.*;

import javax.swing.ImageIcon;
import javax.swing.JApplet;


public class MonoplyDriver extends JApplet {

boolean isFirst=true;
Player John = new Player(1500,"Circle","John");
Board board = new Board();
Image imgBoard;

public void init()
{
    //imgBoard = new ImageIcon("res/board.png").getImage();
    imgBoard = getImage(getDocumentBase(),"res/board.png");
    setSize(750,750);
    System.out.println(getDocumentBase());
}
public void paint(Graphics g)
{
    //super.paint(g);
    if(isFirst)
    {
        isFirst=false;
    }
    g.drawImage(imgBoard, 0, 0, this);

}

}

Upvotes: 0

Views: 86

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347184

From the sounds of it, the image is not being found because it is a internal resource.

You could try something like...

imgBoard = ImageIO.read(getClass().getResource("res/board.png"));

This will throw an IOException if the image cannot be loaded for some reason, which is more useful than what you're getting right now

As an aside. You should avoid painting directly to top level containers, but instead using something that extends from JComponent and override it's paintComponent method

Take a look at Performing Custom Painting and Reading/Loading an Image for more details

Upvotes: 1

Related Questions