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