我正在寻找向小程序添加图像的好例子

发布于 2025-01-08 01:07:49 字数 818 浏览 1 评论 0原文

我正在尝试将图像添加到我的小程序中。我已经用谷歌搜索过这个,但我还没有找到一个我理解的像样的例子。有谁知道我在哪里可以找到向小程序添加图像的好例子?

我在网上得到了这个,但是当我运行该小程序时,它不显示我的图像。

   public class Lab5 extends JApplet {
        //Lab5(){}


        Image img;

        public void init() {
                img = getImage(getDocumentBase(), "img\flag0.gif");                 
        }


        public void paintComponent(Graphics g) {
            g.drawImage(img, 50, 50, this);
        }
}

下面是我的 HTML 文件

<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
    <applet code="Lab5.class" width= 250 height = 50></applet>
    </body>
</html>

I am trying to add an image to my applet. I have googled this but I have not found a decent example that I understand. Does anyone know where I can find a good example of adding an image to and applet?

I got this online but when I run the applet it doesn't display my image.

   public class Lab5 extends JApplet {
        //Lab5(){}


        Image img;

        public void init() {
                img = getImage(getDocumentBase(), "img\flag0.gif");                 
        }


        public void paintComponent(Graphics g) {
            g.drawImage(img, 50, 50, this);
        }
}

Below is my HTML file

<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
    <applet code="Lab5.class" width= 250 height = 50></applet>
    </body>
</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

冷弦 2025-01-15 01:07:49

下面是一个简单的示例,显示来自 Internet 的 URL 的图像。您可能会在 Internet url 的位置使用资源,例如应用程序 jar 目录中保存的图像:

Class SimpleAppletImage.java

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;

@SuppressWarnings("serial")
public class SimpleAppletImage extends JApplet {
   @Override
   public void init() {
      try {
         SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
               try {
                  // you might want to use a file in place of a URL here
                  URL url = new URL("http://duke.kenai.com/gun/Gun.jpg");
                  BufferedImage img = ImageIO.read(url);
                  MyPanel myPanel = new MyPanel(img );
                  getContentPane().add(myPanel);
               } catch (MalformedURLException e) {
                  e.printStackTrace();
               } catch (IOException e) {
                  e.printStackTrace();
               }
            }
         });
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

class MyPanel.java

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

import javax.swing.JPanel;

@SuppressWarnings("serial")
class MyPanel extends JPanel {
   private BufferedImage img;

   public MyPanel(BufferedImage img) {
      this.img = img;
      setPreferredSize(new Dimension(img.getWidth(), img.getHeight()));
   }

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      if (img != null) {
         g.drawImage(img, 0, 0, this); // corrected
      }
   }
}

Here's a simple example that shows an image from a URL from the internet. You'd probably use a resource in the internet url's place, such as an image held in a directory of the application's jar:

Class SimpleAppletImage.java

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;

@SuppressWarnings("serial")
public class SimpleAppletImage extends JApplet {
   @Override
   public void init() {
      try {
         SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
               try {
                  // you might want to use a file in place of a URL here
                  URL url = new URL("http://duke.kenai.com/gun/Gun.jpg");
                  BufferedImage img = ImageIO.read(url);
                  MyPanel myPanel = new MyPanel(img );
                  getContentPane().add(myPanel);
               } catch (MalformedURLException e) {
                  e.printStackTrace();
               } catch (IOException e) {
                  e.printStackTrace();
               }
            }
         });
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

class MyPanel.java

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

import javax.swing.JPanel;

@SuppressWarnings("serial")
class MyPanel extends JPanel {
   private BufferedImage img;

   public MyPanel(BufferedImage img) {
      this.img = img;
      setPreferredSize(new Dimension(img.getWidth(), img.getHeight()));
   }

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      if (img != null) {
         g.drawImage(img, 0, 0, this); // corrected
      }
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文