如何在 JApplet 中显示 BufferedImage
japplet 上不显示任何图像。
public void setCharacter(String type)
{
try
{
character = ImageIO.read(new File(type));
}
catch (Exception e)
{
e.printStackTrace();
}
}
这就是我检索文件的方法。
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setBackground(Color.BLACK);
switch (manipulateCase)
{
case 0:
g2.setColor(Color.BLACK);
g2.fill(new Rectangle(0, 0, xBound, yBound)); // painting the background.
break;
case 1:
normandy.delete(g2); // delete previous position
ImageIcon stuff = new ImageIcon("spaceship.jpg");
stuff.paintIcon(this, g2, 0, 0);
//g2.drawImage(normandy.getImage(), normandy.getBounds().x, normandy.getBounds().y, null); // draw new position
break;
/*case 2:
for (int i = 0; i < aLevel.getInvaders().length; i++) {
g2.fill(aLevel.getInvaders()[i].getCharacter());
}
break;
case 3:
g2.setColor(Color.WHITE);
if (bullets.getBullets().size() > 0)
for (int i = 0; i < bullets.getBullets().size(); i++) {
g2.fill(bullets.getBullets().get(i).getBullet());
System.out.println("BULLETS: "
+ bullets.getBullets().size());
}
break;
case 4:
break; */
}
}
上面的代码在小程序中。
我做错了什么?为什么我收到错误。 “无法读取输入文件!”
任何帮助将不胜感激。
谢谢你, 布拉格曼
No images are displayed on the japplet.
public void setCharacter(String type)
{
try
{
character = ImageIO.read(new File(type));
}
catch (Exception e)
{
e.printStackTrace();
}
}
That's how I retrieve the file.
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setBackground(Color.BLACK);
switch (manipulateCase)
{
case 0:
g2.setColor(Color.BLACK);
g2.fill(new Rectangle(0, 0, xBound, yBound)); // painting the background.
break;
case 1:
normandy.delete(g2); // delete previous position
ImageIcon stuff = new ImageIcon("spaceship.jpg");
stuff.paintIcon(this, g2, 0, 0);
//g2.drawImage(normandy.getImage(), normandy.getBounds().x, normandy.getBounds().y, null); // draw new position
break;
/*case 2:
for (int i = 0; i < aLevel.getInvaders().length; i++) {
g2.fill(aLevel.getInvaders()[i].getCharacter());
}
break;
case 3:
g2.setColor(Color.WHITE);
if (bullets.getBullets().size() > 0)
for (int i = 0; i < bullets.getBullets().size(); i++) {
g2.fill(bullets.getBullets().get(i).getBullet());
System.out.println("BULLETS: "
+ bullets.getBullets().size());
}
break;
case 4:
break; */
}
}
The above code is in the applet.
What am i doing wrong? Why am I getting the error. "Cannot read input file!"
Any help would be appreciated.
Thank you,
blargman
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
文件驻留在哪里?在浏览器中运行的小程序无法读取计算机上的文件,它只能通过 URL 访问该文件。它可以访问远程计算机上的文件,但通常(出于充分的理由)不允许这样做。因此,将
new ImageIcon(String filename)
替换为new ImageIcon(URL location)
。Where does the file reside? An applet running in a browser can't read a file on your computer, it can only access it via a URL. It could access a file on the remote computer, but is normally (for a good reason) not allowed to. So replace
new ImageIcon(String filename)
bynew ImageIcon(URL location)
.自定义绘制不应通过重写 Paint() 方法来完成。
首先阅读 Swing 教程中关于如何制作 Applet 有关编写小程序的更多详细信息。还有一个关于“将图像加载到小程序中”的部分。
我也同意学习如何进行绘画更容易通过使用 JFrame 的简单应用程序进行练习。您可以阅读 Swing 教程中有关“自定义绘画”的部分以获取更多示例。本教程充满了丰富的信息和示例。
Custom painting should not be done by overriding the paint() method.
Start by reading the section from the Swing tutorial on How to Make Applets for more details on writing an applet. There is also a section on "Loading Images into Applets".
I also agree to learn how to do painting is is easier to just practice with a simple application that uses a JFrame. You can read the section from the Swing tutorial on "Custom Painting" for more examples. The tutorial is full of great information and examples.