Java 图像不会绘制为动画
在java小程序中加载/显示图像时出现问题。不确定是否加载了错误的图像,或者是否访问了错误的图像。这是绘制飞船和背景的代码这是一个类似小行星的游戏。背景画得正确,但船没有。这是我正在处理的主要类方法:Java 图像不会绘制为动画,java,animation,awt,drawimage,Java,Animation,Awt,Drawimage,在java小程序中加载/显示图像时出现问题。不确定是否加载了错误的图像,或者是否访问了错误的图像。这是绘制飞船和背景的代码这是一个类似小行星的游戏。背景画得正确,但船没有。这是我正在处理的主要类方法: public void paintFrame(Graphics g) { Dimension d = size(); g.fillRect(0, 0, d.width, d.height); g.drawImage(ship.getImage(),
public void paintFrame(Graphics g) {
Dimension d = size();
g.fillRect(0, 0, d.width, d.height);
g.drawImage(ship.getImage(), d.width/2, d.height/2, null);
}
我在类的开头创建了ship类的一个实例。但是,如果我尝试在一个方法中实例化ship类,例如ship=new ship;,它说变量船从未被使用过
这是整艘船的等级:
public class Ship {
private int dx;
private int dy;
private int x;
private int y;
private Image image;
public Ship() {
ImageIcon ii = new ImageIcon("ship1.png");
image = ii.getImage();
}
public Image getImage() {
return image;
}
}
如果我按原样运行它,它运行时不会出错,但它不会显示飞船。如果我尝试在除顶部之外的任何其他位置创建ship实例,它会给我一个NullPointerException
更新
这是我的整个主要课程:
import java.applet.Applet;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
public class RunGame extends Applet implements Runnable {
int frame;
int delay;
Thread animator;
Ship ship = new Ship();
Level level;
Dimension offDimension;
Image offImage;
Graphics offGraphics;
/**
* Initialize the applet and compute the delay between frames.
*/
public void init() {
String str = getParameter("fps");
int fps = (str != null) ? Integer.parseInt(str) : 10;
delay = (fps > 0) ? (1000 / fps) : 100;
}
/**
* Method is called when the applet becomes visible on
* the screen.
*/
public void start() {
animator = new Thread(this);
animator.start();
}
/**
* This method is called by the thread that was created in
* the start method. It does the main animation.
*/
public void run() {
// Remember the starting time
long tm = System.currentTimeMillis();
while (Thread.currentThread() == animator) {
// Display the next frame of animation.
repaint();
// Delay depending on how far we are behind.
try {
tm += delay;
Thread.sleep(Math.max(0, tm - System.currentTimeMillis()));
} catch (InterruptedException e) {
break;
}
// Advance the frame
frame++;
}
}
/**
* This method is called when the applet is no longer
* visible. Set the animator variable to null so that the
* thread will exit before displaying the next frame.
*/
public void stop() {
animator = null;
offImage = null;
offGraphics = null;
}
/**
* Update a frame of animation.
*/
public void update(Graphics g) {
Dimension d = size();
// Create the offscreen graphics context
if ((offGraphics == null) || (d.width != offDimension.width) || (d.height != offDimension.height)) {
offDimension = d;
offImage = createImage(d.width, d.height);
offGraphics = offImage.getGraphics();
}
// Erase the previous image
offGraphics.setColor(getBackground());
offGraphics.fillRect(0, 0, d.width, d.height);
offGraphics.setColor(Color.black);
// Paint the frame into the image
paintFrame(offGraphics);
// Paint the image onto the screen
g.drawImage(offImage, 0, 0, null);
}
/**
* Paint the previous frame (if any).
*/
public void paint(Graphics g) {
if (offImage != null) {
g.drawImage(offImage, 0, 0, null);
}
}
/**
* Paint a frame of animation.
*/
public void paintFrame(Graphics g) {
Dimension d = size();
g.fillRect(0, 0, d.width, d.height);
//g.drawImage(level.getImage(), 0, 0, null);
g.drawImage(ship.getImage(), 400, 300, null);
}
}
在没有看到完整代码的情况下,我们无法真正了解异常情况,但在调用drawImage时,请将“this”指定为最后一个参数,而不是null。图像是异步加载的,因此它们需要一些实现ImageObserver的东西,例如您的帧,来告诉它们何时加载了更多或全部图像,以便它们可以重新绘制 在没有看到完整代码的情况下,我们无法真正了解异常情况,但在调用drawImage时,请将“this”指定为最后一个参数,而不是null。图像是异步加载的,因此它们需要一些实现ImageObserver的东西,例如您的帧,来告诉它们何时加载了更多或全部图像,以便它们可以重新绘制
ImageIcon ii = new ImageIcon("ship1.png");
接受字符串的假定字符串表示
..文件名或文件路径
“小程序和文件不能混合。”只有受信任的小程序才能加载文件对象,甚至只能从最终用户的文件系统加载。文件对象不能指向服务器
小程序通常使用由URL形成的路径。Applet类提供了许多方法来帮助形成该URL,ImageIcon构造函数被重载以接受URL
…不确定是否加载了错误的图像或访问了错误的图像
调试101是在加载图像后立即显示图像。将图像图标放入标签中,并使用JOptionPane显示标签
接受字符串的假定字符串表示
..文件名或文件路径
“小程序和文件不能混合。”只有受信任的小程序才能加载文件对象,甚至只能从最终用户的文件系统加载。文件对象不能指向服务器
小程序通常使用由URL形成的路径。Applet类提供了许多方法来帮助形成该URL,ImageIcon构造函数被重载以接受URL
…不确定是否加载了错误的图像或访问了错误的图像
调试101是在加载图像后立即显示图像。将图像图标放入标签中,并使用JOptionPane显示标签。是否从paintComponent方法中调用paintFrame?请在此处查看:。您不应该从绘图方法中读入图像-读入一次,比如在类的构造函数中,然后使用ImageIO.read。。。这样做。你的NPE被扔到哪里?您是否正在测试图像是否正在被读取?您的图像是否位于user.dir中?或者图像文件相对于所有东西的具体位置是什么?@HovercraftFullOfEels:请原谅,但是日本人没有绘画组件。。。方法,似乎OP正在使用JApplet,因为他的问题是在java小程序中加载/显示图像时遇到问题。@GagandeepBali:请您原谅,但您不应该直接在JApplet中绘图。你应该画一个油漆组件。。。由JApplet持有的JPanel或JComponent的方法。你们所有人都应该知道这一点。@HovercraftFullOfEels:是的,毫无疑问,在JPanel上画画是一条路要走。我只是指你的第一句话。你最近的评论太真实了。怀疑引发了许多不必要的问题:-是否从paintComponent方法中调用paintFrame?请在此处查看:。您不应该从绘图方法中读入图像-读入一次,比如在类的构造函数中,然后使用ImageIO.read。。。这样做。你的NPE被扔到哪里?您是否正在测试图像是否正在被读取?您的图像是否位于user.dir中?或者图像文件相对于所有东西的具体位置是什么?@HovercraftFullOfEels:请原谅,但是日本人没有绘画组件。。。方法,似乎OP正在使用JApplet,因为他的问题是在java小程序中加载/显示图像时遇到问题。@GagandeepBali:请您原谅,但您不应该直接在JApplet中绘图。你应该画一个油漆组件。。。由保存的JPanel或JComponent的方法
日本人。你们所有人都应该知道这一点。@HovercraftFullOfEels:是的,毫无疑问,在JPanel上画画是一条路要走。我只是指你的第一句话。你最近的评论太真实了。怀疑会引发许多不必要的问题:-在绘制图像时使用ImageObserver的好建议+1在绘制图像时使用ImageObserver的好建议+1.