我遇到的问题是我在 JAVA 的 Applet Viewer 上看不到我用 ImageIO
类引用的任何图片。我正在网上学习教程,但尚未收到作者的回复 - 他的页面上有类似的问题,他没有回答,所以我假设他正在尝试自己解决这个问题。
这是我到目前为止所拥有的。
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);
}
}
下面是我的 Tester 类,其中大部分函数嵌套在代码中。
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) {
}
}
如有任何帮助或反馈,我们将不胜感激。
最佳答案
..my image files are only on my computer and not being read remotely from a URL or anything on the net.
当小程序部署到万维网上的受众时,它应该如何工作?
当小程序从您的网站加载到我的电脑时,不再有任何选项可以从您的PC<加载图像/strong>(除非您的 PC 是服务器)。小程序的图像必须来自 URL
而不是File
,并且除非受信任的小程序,它们必须来自与小程序位于同一站点的 URL。
ImageIO.read(..)
对于流、文件或 URL 来说是重载的,因此仍然可以用来加载图像。
要形成小程序的 URL,至少有 3 种方法。如果图像是:
- 同一服务器上的松散资源。形成相对于文档库或代码库的 URL。
- 在
archive
中提到的 jar 里属性(位于运行时类路径上)。使用:
URL urlToImage = this.getClass().getResource("/path/to/image.png");
更多提示。
- 尚不完全清楚,但似乎
SuperheroGame
是Applet
的一个实例。本世纪使用 Swing,因此应该扩展JApplet
. - 然后我们来到第二个问题,其中
SuperheroGame
实际上延伸了SuperHeroGameLoop
(不寒而栗)。此时我们意识到SuperHeroGameLoop
应该被编码为Panel
或JPanel
而不是扩展Applet
或JApplet
。一般原则是“在容器中进行自定义绘制,该容器本身可以添加到小程序、框架、对话框或窗口、布局中的约束、选项卡或内部框架......”。< -
setSize(320,240);
这在大多数 GUI 中都是不可取的,但在小程序中更是如此。小程序的大小是在 HTML 中设置的,并且应该遵守该大小。 -
Thread th = new Thread(this) ;
要制作动画,请使用 SwingTimer
。以下消息是为 Swing 编写的,但同样适用于 AWT AFAIU(毕竟,EDT 本身就是由 AWT 启动、使用和控制的线程)。
不要阻塞 EDT(事件调度线程)——发生这种情况时 GUI 将“卡住”。而不是调用Thread.sleep(n)
实现 SwingTimer
用于重复任务或SwingWorker
对于长时间运行的任务。请参阅Concurrency in Swing更多细节。 -
addKeyListener(this);
- 为了获得更可靠的焦点,请使 GUI 自由 float 。为此,请将游戏放入框架 (
JFrame
) 中并使用 Java Web Start 从链接启动它。 . - 对于基于 Swing 的 GUI,请参阅 Key Bindings .
- 为了获得更可靠的焦点,请使 GUI 自由 float 。为此,请将游戏放入框架 (
这是 5 个以上的提示,我们刚刚到达 init
的结尾。 。这表明学习从不同来源编写代码比生成该代码的来源更为理想。
关于java - Java 中的 ImageIO 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14107162/