小程序图像丢失

发布于 2024-12-02 09:45:00 字数 2349 浏览 2 评论 0原文

Stackoverflow 成员们,大家好,

CatchTheCreature Applet 类应该显示在不同位置通过时间延迟重新绘制的图像,但由于某种原因该图像没有显示。

  import java.awt.Color;
  import java.awt.Graphics;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.awt.event.MouseEvent;
  import java.awt.event.MouseListener;
  import java.util.Random;

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

  public class CatchTheCreature extends JApplet {

private int height = 300;
private int width = 600;
private final int delay = 1001;

private ImageIcon image;
private Timer timer;
private int x, y;
private int counter = 0;
Random gn = new Random();

public void init() {
    DotListener dot = new DotListener();
    addMouseListener(dot);

    image = new ImageIcon("Monster.png");

    timer = new Timer(delay, new timerListener());
    x = 40;
    y = 40;
    getContentPane().setBackground(Color.black);

}

// Action Listener Methods
private class timerListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {

        x = gn.nextInt(width);
        y = gn.nextInt(height);

        repaint();
    }

}

private class DotListener implements MouseListener {

    public void mousePressed(MouseEvent event) {

    }

    @Override
    public void mouseClicked(MouseEvent event) {
        if (event.getX() > (x) && event.getX() < (x + 60)
                && event.getY() < (y + 60) && event.getY() > (y)) {
            x = gn.nextInt(width);
            y = gn.nextInt(height);
            counter = counter + 1;
            repaint();
        }
    }

    @Override
    public void mouseEntered(MouseEvent event) {

    }

    @Override
    public void mouseExited(MouseEvent event) {

    }

    @Override
    public void mouseReleased(MouseEvent event) {

    }

}

public void paint(Graphics g) {
    super.paint(g);
    g.setColor(Color.yellow);
    image.paintIcon(this, g, x, y);
    g.drawString("Clicked accuratly: " + counter, 5, 15);
}

public void start() {
    timer.start();

}

public void stop() {
    timer.stop();
}

是我的 html 文件

     <applet code = CatchTheCreature width = 250 height = 300>

     </applet>

如果有人能告诉我如何在小程序上显示图像图标,我将非常感激。

谢谢

Hello again Stackoverflow members,

The CatchTheCreature Applet class is supposed to display an image being repainted in different locations by a time delay, but for some reason the image is not being displayed.

  import java.awt.Color;
  import java.awt.Graphics;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.awt.event.MouseEvent;
  import java.awt.event.MouseListener;
  import java.util.Random;

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

  public class CatchTheCreature extends JApplet {

private int height = 300;
private int width = 600;
private final int delay = 1001;

private ImageIcon image;
private Timer timer;
private int x, y;
private int counter = 0;
Random gn = new Random();

public void init() {
    DotListener dot = new DotListener();
    addMouseListener(dot);

    image = new ImageIcon("Monster.png");

    timer = new Timer(delay, new timerListener());
    x = 40;
    y = 40;
    getContentPane().setBackground(Color.black);

}

// Action Listener Methods
private class timerListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {

        x = gn.nextInt(width);
        y = gn.nextInt(height);

        repaint();
    }

}

private class DotListener implements MouseListener {

    public void mousePressed(MouseEvent event) {

    }

    @Override
    public void mouseClicked(MouseEvent event) {
        if (event.getX() > (x) && event.getX() < (x + 60)
                && event.getY() < (y + 60) && event.getY() > (y)) {
            x = gn.nextInt(width);
            y = gn.nextInt(height);
            counter = counter + 1;
            repaint();
        }
    }

    @Override
    public void mouseEntered(MouseEvent event) {

    }

    @Override
    public void mouseExited(MouseEvent event) {

    }

    @Override
    public void mouseReleased(MouseEvent event) {

    }

}

public void paint(Graphics g) {
    super.paint(g);
    g.setColor(Color.yellow);
    image.paintIcon(this, g, x, y);
    g.drawString("Clicked accuratly: " + counter, 5, 15);
}

public void start() {
    timer.start();

}

public void stop() {
    timer.stop();
}

}

This is my html file

     <applet code = CatchTheCreature width = 250 height = 300>

     </applet>

If someone can tell me how i can display the image icon on the applet I would be very grateful.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

败给现实 2024-12-09 09:45:00

..image = new ImageIcon("Monster.png");

  • ImageIcon 的基于 String 的构造函数假定 String 代表一个 File
  • 沙盒小程序无法访问File对象,但可以访问来自相同代码库/文档库的URL
  • 使用 getDocumentBase()/getCodeBase() 以及图像的相对路径,小程序将是可移植的(假设图像也上传到同一位置)。

..image = new ImageIcon("Monster.png");

  • The String based constructor to ImageIcon presumes the String to represent a File.
  • A sand-boxed applet cannot access File objects, but can access URLs coming from the same code base/document base.
  • Use getDocumentBase()/getCodeBase() with an relative path to the image, and the applet will be portable (assuming the image is also uploaded to the same place).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文