图片不出现在小程序中

Images do not appear in applet

我正在开发一个 game.I 想要将图像添加到小程序,但是在我调整大小或最小化之前它们不会出现在那里 applet.I 不知道我的图像观察者是否有误. 图像位于正确的 path.Their in build 和 class 文件夹中,拼写正确。我的代码有什么问题? 任何帮助将不胜感激。

public class game extends JApplet implements KeyListener, Runnable {

ScreenDir sd;
Image ball;
Image flower;

public void init() {

    sd = new ScreenDir(this);
    ball = getImage(getCodeBase(), "ball.jpg");
    ballb = new Ball(50, 50, 30, 40, Color.red, ball, sd);
    sd.add(ball);
    flower = getImage(getCodeBase(), "flower.gif");
     //class Flower is just defined like class Ball
    flowerf = new Flower(60, 100, 30, 30, Color.green, flower, sd);
    sd.add(flower);

}

public void paint(Graphics g) {
    sd.draw(g);

}

//These are for moving the ball

@Override
public void keyTyped(KeyEvent e) {
}

@Override
public void keyPressed(KeyEvent e) {
}

@Override
public void keyReleased(KeyEvent e) {
}

@Override
public void run() {
    while (true) {
        repaint();
        try {
            Thread.sleep(15);
        } catch (InterruptedException ex) {}}}



  public class ScreenDir {

ArrayList<Object> olist;
JApplet parent;

public ScreenDir(JApplet parent) {
    this.parent = parent;
    olist = new ArrayList<>();
}

public void add(Object o) {
    olist.add(o);
}

Image Img;
Graphics oG;

Img  = parent.createImage(parent.getWidth(), parent.getHeight());
oG  = offImg.getGraphics();

for(int i = 0;i<olist.size ();i++){
         olist.get(i).draw(offG);
}

g.drawImage (Img,0, 0, parent);
 }


    public class Ball extends Object {

ScreenDir sd;

public ball(int x, int y, int w, int h, Color c, Image pi, ScreenDir sd) {
    {
        this.sd = sd;
    }
public void draw(Graphics g) {
    g.drawImage(pi, x, y, w, h, null);
}


public abstract class Object {

    int x, y, w, h;
    Color c;
    Image pi;

    public Object(int x, int y, int w, int h, Color c, Image pi) {
        this.x = x;
        this.y = y;
        this.w = w;
        this.h = h;
        this.c = c;
        this.pi = pi;
    }

    public abstract void draw(Graphics g) {
    }

小程序异步加载图像,因此很可能在小程序开始绘制之前图像未完全加载。但是每个值得一提的 Java 组件都实现了一个 ImageObserver,这意味着它将在图像加载时获得更新。所以要解决这个问题,改变这个:

g.drawImage(pi, x, y, w, h, null);

这个:

g.drawImage(pi, x, y, w, h, this);

更新

我误以为 drawImage 方法是 JApplet 的一部分(这是一个 ImageObserver)。您可以简单地将方法声明和绘画行更改为:

public void draw(Graphics g, ImageObserver io) {
    g.drawImage(pi, x, y, w, h, io);
}

然后调用它,更改:

sd.draw(g);

收件人:

sd.draw(g, this);