在我的Java Applet中绘制此图像将无法正常工作

时间:2013-11-06 03:01:26

标签: java image applet

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);

}

}

1 个答案:

答案 0 :(得分:1)

根据它的声音,找不到图像,因为它是内部资源。

您可以尝试类似......

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

如果由于某种原因无法加载图像,则会抛出IOException,这比您现在获得的图片更有用

暂且不说。您应该避免直接绘制到顶级容器,而是使用从JComponent延伸的内容并覆盖它的paintComponent方法

请查看Performing Custom PaintingReading/Loading an Image了解详情