0

The issue I'm having is I cannot see any of the pictures I'm referencing with the ImageIO class on my Applet Viewer in JAVA. I was following a tutorial online and have yet to hear back from the author - there were similar questions on his page he didn't answer, so I'm assuming he's trying to figure it out himself.

Here's what I have so far.

import java.applet.Applet;
import java.awt.Graphics;


public class SuperheroGame extends SuperHeroGameLoop{


/**
 * 
 */
private static final long serialVersionUID = 1L;

public void init () {
    setSize(320,240);
    Thread th = new Thread(this) ;
    th.start();
    offscreen = createImage(320,240);
    d = offscreen.getGraphics();
    addKeyListener(this);
}

public void paint (Graphics g) {
    d.clearRect(0, 0, 320, 240);
    d.drawImage(background, 0, 0, this);
    d.drawImage(w2, x,y, this);
    //d.drawImage(foreground, 0, 0, this);
    //g.drawImage(offscreen, 0, 0, this);

}

public void update(Graphics g) {
    paint(g);
}

}

And below is my Tester class with the bulk of the functions nested in the code.

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;


public class SuperHeroGameLoop extends Applet implements Runnable, KeyListener {

/**
 * 
 */
private static final long serialVersionUID = 1L;
public int x,y;
public Image offscreen;
public Graphics d;
public boolean up, down, left, right;
public BufferedImage background, foreground, w1, w2, w3, w4;

public void run() {

    x = 100;
    y = 100;
    try {
        //background = ImageIO.read(new File ("background.png"));
        //foreground = ImageIO.read(new File ("foreground.png"));
        w1 = ImageIO.read(new File("red copy.png"));
        w2 = ImageIO.read(new File("red copy.png"));
        w3 = ImageIO.read(new File("red copy.png"));
        w4 = ImageIO.read(new File("red copy.png"));
    } catch (IOException e1) {

        e1.printStackTrace();
    }
    while(true) {
    if (left == true) {
        x-=2;
    }
    if (right == true) {
        x+=2;
    }
    if (up == true) {
        y-=2;
    }
    if (down == true) {
        y+=2;
    }

    repaint();
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void keyPressed(KeyEvent e) {
    if (e.getKeyCode() == 37) {
        left = true;
    }

    if (e.getKeyCode() == 39) {
        right = true;
    }

    if (e.getKeyCode() == 38) {
        up = true;
    }

    if (e.getKeyCode() == 40) {
        down = true;
    }
}
public void keyReleased(KeyEvent e) {
    if (e.getKeyCode() == 37) {
        left = false;
    }

    if (e.getKeyCode() == 39) {
        right = false;
    }

    if (e.getKeyCode() == 38) {
        up = false;
    }

    if (e.getKeyCode() == 40) {
        down = false;
    }
}
public void keyTyped(KeyEvent e) {

}

}

Any help or feedback will be appreciated.

4

2 回答 2

4

..我的图像文件仅在我的计算机上,不能从 URL 或网络上的任何内容远程读取。

当小程序部署到万维网上的观众时,它应该如何工作?

当小程序从您的站点加载到我的 PC上时,不再有任何选项可以从您的PC上加载图像(除非您的 PC 是服务器)。applet 的图像必须来自 aURL而不是 a File,并且除非受信任的 applet,否则它们必须来自与 applet 位于同一站点的 URL。

ImageIO.read(..)为流、文件或 URL 重载,因此仍可用于加载图像。

要形成小程序的 URL,至少有 3 种方法。如果图像是:

  • 同一服务器上的松散资源。形成相对于文档库或代码库的 URL。
  • archive属性中提到的 Jar 中(在运行时类路径上)。利用:
URL urlToImage = this.getClass().getResource("/path/to/image.png");

进一步的提示。

  1. 这并不完全清楚,但似乎SuperheroGameApplet. 这个千年使用 Swing,所以应该扩展JApplet.
  2. 但接下来我们来到第二个问题,其中SuperheroGame实际上是扩展SuperHeroGameLoop(颤抖)。在这一点上,我们意识到SuperHeroGameLoop应该在 a Panelor中进行编码,JPanel而不是扩展Applet or JApplet。一般原则是“在容器中进行自定义绘画,该容器本身可以添加到小程序、框架、对话框或窗口、布局中的约束、选项卡或内部框架......”。
  3. setSize(320,240); 这在大多数 GUI 中是不可取的,但在 applet 中更是如此。小程序的大小在 HTML 中设置,并且应该遵守该大小。
  4. Thread th = new Thread(this) ; To do animation, use a Swing Timer. The following message was written for Swing, but applies equally to AWT AFAIU (after all, the EDT is itself a thread started, used and controlled by the AWT).
    Don't block the EDT (Event Dispatch Thread) - the GUI will 'freeze' when that happens. Instead of calling Thread.sleep(n) implement a Swing Timer for repeating tasks or a SwingWorker for long running tasks. See Concurrency in Swing for more details.
  5. addKeyListener(this);
    1. For more reliable focus make the GUI free floating. To do that, put the game in a frame (JFrame) and launch it from a link using Java Web Start.
    2. For a Swing based GUI, look to Key Bindings.

那是 5 个以上的提示,而我们刚刚到达init. 这表明学习从不同来源编写代码比学习编写代码更理想。

于 2013-01-01T02:46:31.980 回答
3

您正在使用一个小程序 - 它旨在在浏览器中运行并受安全管理器的约束。安全管理器可能会阻止您执行本地磁盘 i/o。

Trashgod 和 Philip 为您指出了有关此主题的有用文章。

但是,如果我们采取不同的策略...

AnAppletComponent可以添加到 aJFrame的内容窗格中的。您可以通过执行以下操作将您的小程序变成一个独立的应用程序(没有限制性安全管理器)......

添加一个新类:

public class SwingApp
{
  public static void main(String[] args)
  {
    final JFrame frame = new JFrame("Swing App");

    SwingUtilities.invokeLater(new Runnable()
    {
      public void run()
      {
        final SuperHeroGameLoop loop = new SuperHeroGameLoop();

        frame.getContentPane().add(loop);
        frame.setSize(320, 240);

        loop.init();

        frame.setVisible(true);
      }
    });
  }
}

(但是,我确实只是从内存中输入了这个,而不是通过编译器运行它——所以我为任何编译错误道歉)......

编译并运行它作为一个独立的应用程序。

另一种选择是完全放弃小程序方法,并将其编写为桌面 Swing 应用程序,使用JComponent...

于 2013-01-01T01:36:30.237 回答