如何在 JApplet 中显示 BufferedImage

发布于 2024-10-14 01:21:35 字数 1378 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瞎闹 2024-10-21 01:21:35

文件驻留在哪里?在浏览器中运行的小程序无法读取计算机上的文件,它只能通过 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) by new ImageIcon(URL location).

一梦浮鱼 2024-10-21 01:21:35

自定义绘制不应通过重写 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文