在awt中添加图像

发布于 2024-12-07 01:55:30 字数 1023 浏览 0 评论 0原文

我试图在 GridBagLayout 单元格之一中添加图像。但我无法实现它。

  public class imageInAwt extends Applet{
     Panel p;
     BufferedImage img;
     public void init(){
     setLayout(new GridBagLayout());
     GridBagConstraints gbc =new GridBagConstraints();
     gbc.gridx = 0;
     gbc.gridy = 0;
     try {
        img = ImageIO.read(new File("settings.gif"));
     } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
     }
     add(new junaid(img),gbc);
     gbc.gridy = GridBagConstraints.RELATIVE;
     add(new Label("Junaid"),gbc);
     } 
}

    class junaid extends Canvas {
        Image img;
        public junaid(Image img) {
        this.img = img;
        }
        public void paint (Graphics g) {
            BufferedImage image = null;
                g.drawImage(image, 2, 3, null);
                g.drawString("Junaid", 22, 16);
        }
        public void setImage (Image img){
        this.img = img;
        }
    }

请帮忙。

提前致谢

I was trying to add an image in one of GridBagLayout cells. But i am not able to achieve it.

  public class imageInAwt extends Applet{
     Panel p;
     BufferedImage img;
     public void init(){
     setLayout(new GridBagLayout());
     GridBagConstraints gbc =new GridBagConstraints();
     gbc.gridx = 0;
     gbc.gridy = 0;
     try {
        img = ImageIO.read(new File("settings.gif"));
     } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
     }
     add(new junaid(img),gbc);
     gbc.gridy = GridBagConstraints.RELATIVE;
     add(new Label("Junaid"),gbc);
     } 
}

    class junaid extends Canvas {
        Image img;
        public junaid(Image img) {
        this.img = img;
        }
        public void paint (Graphics g) {
            BufferedImage image = null;
                g.drawImage(image, 2, 3, null);
                g.drawString("Junaid", 22, 16);
        }
        public void setImage (Image img){
        this.img = img;
        }
    }

Please help.

Thanks in advance

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

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

发布评论

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

评论(2

旧城烟雨 2024-12-14 01:55:30
    public void paint (Graphics g) {
        BufferedImage image = null;
            g.drawImage(image, 2, 3, null);
            g.drawString("Junaid", 22, 16);
    }

哈?!?

    public void paint (Graphics g) {
            g.drawImage(img, 2, 3, this);
            g.drawString("Junaid", 22, 16);
    }
    public void paint (Graphics g) {
        BufferedImage image = null;
            g.drawImage(image, 2, 3, null);
            g.drawString("Junaid", 22, 16);
    }

Huh?!?

    public void paint (Graphics g) {
            g.drawImage(img, 2, 3, this);
            g.drawString("Junaid", 22, 16);
    }
骷髅 2024-12-14 01:55:30

使用 Applet 类的 Applet.getImage() 方法。

Image img=getImage(getCodeBase(),"settings.gif");

Use Applet.getImage() method of the Applet class.

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