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 PaintingReading/Loading an Image以获得更多详细信息

10-06 02:38