帮助在java小程序中显示图像
下面是我编写的一个简单的小程序,用于显示单张图片。代码编译良好,小程序会加载,但图像文件永远不会绘制到小程序中。我认为它无法使用 this.getImage(appletBaseURL, filename); 找到图像我将图像文件存储在与该包关联的所有文件夹中,但它仍然没有绘制它。
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
public class imageTest extends Applet {
private Image spaceShip;
private final String filename = "spaceshipcropped.jpg";
public void init() {
java.net.URL appletBaseURL = getCodeBase();
File file = new File("spaceshipcropped.jpg");
try {
spaceShip = ImageIO.read(file);
} catch (IOException ex) {
Logger.getLogger(imageTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void paint(Graphics g)
{
Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(spaceShip, 0,0, null);
}
public void update(Graphics g) {
repaint();
}
}
在我进行这些更改后,它起作用了。非常感谢大家的帮助!
Below is a simple applet im writing to display a single picture. The code compiles fine, and the applet loads but the image file is never drawn to the applet. Im thinking that it cant find the image using the this.getImage(appletBaseURL, filename); I have the image file stored in all the folders associated with this package but its still not drawing it.
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
public class imageTest extends Applet {
private Image spaceShip;
private final String filename = "spaceshipcropped.jpg";
public void init() {
java.net.URL appletBaseURL = getCodeBase();
File file = new File("spaceshipcropped.jpg");
try {
spaceShip = ImageIO.read(file);
} catch (IOException ex) {
Logger.getLogger(imageTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void paint(Graphics g)
{
Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(spaceShip, 0,0, null);
}
public void update(Graphics g) {
repaint();
}
}
After i made theses changes it worked. thank you all very much for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
setSize()
。大小由 HTML 设置。Graphics2D
对象,但我从未听说过Applet
也有同样的说法。您正在检查 Java 控制台吗?paint
方法中,检查图像是否为null
。getImage(URL,String)
方法的 JavaDocs 声明“此方法始终立即返回,无论图像是否存在。” 添加一个MediaTracker code> 或在第三个千年加入我们并使用
ImageIO.read(URL)
- 它会阻塞,直到图像加载完毕。我希望修复第 6 点可以解决问题,但也要注意其他 5 点。
setSize()
in an applet. The size is set by HTML.Graphics2D
object, but I've never heard the same said of anApplet
. Are you checking the Java Console?paint
method, check to see if the image isnull
.getImage(URL,String)
method state "This method always returns immediately, whether or not the image exists." Either add aMediaTracker
or join us in the 3rd millennium and useImageIO.read(URL)
- which blocks until the image is loaded.I expect that fixing point 6 will solve the problem, but attend to the other 5 points as well.
添加这个
add this