japplet上没有显示图像。如何在JApplet中显示BufferedImage
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; */
}
}
上述代码位于applet中。
我在做什么错了?为什么我得到错误。 “无法读取输入文件!”
任何帮助,将不胜感激。
谢谢 blargman
我不知道人们还在使用小程序吗? – VoodooChild 2011-01-24 03:13:26
@VoodooChild - 这是一个陈述还是一个问题?把它作为一个问题。当然,他们做!实际上,随着新一代插件中引入的大量额外功能(包括可拖动的applet以及对JNLP服务的访问),对嵌入式applet的兴趣重新抬头。 – 2011-01-24 05:01:00
除了maaartinus的建议,我会建议1)不要在paint()/ paintComponent()方法中加载图像 - 而是将它们加载到init()中并将它们缓存为类级别属性。 2)切勿覆盖顶层Swing容器中的paint()。而是在JComponent中执行并重写paintComponent()。这既可以避免混淆,并且允许定制组件在放置位置和其他GUI控件可能被添加的位置更加通用。 – 2011-01-24 05:05:51