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);
}
}
最佳答案
从声音来看,找不到图像,因为它是内部资源。
您可以尝试类似...
imgBoard = ImageIO.read(getClass().getResource("res/board.png"));
如果由于某种原因无法加载图像,则会抛出一个
IOException
,它比您现在得到的要有用作为旁白。您应该避免直接绘制到顶级容器,而应使用从
JComponent
扩展并覆盖其paintComponent
方法的内容查看Performing Custom Painting和Reading/Loading an Image以获得更多详细信息