| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 507 人关注过本帖
标题:[原创]在 Application 中显示图象
只看楼主 加入收藏
xiuyuan123
Rank: 2
等 级:新手上路
威 望:3
帖 子:140
专家分:0
注 册:2006-4-25
收藏
 问题点数:0 回复次数:0 
[原创]在 Application 中显示图象

在 application 中显示图象有些人用的方法比较麻烦,要用到 Toolkit 类。
这里我发现了一个教为简单的办法,就是用getImage()方法来从一个 ImageIcon 对象获取 Image 对象。
具体做法如下:

//获取图象文件路径
//getResource()方法会自动的去CLASSPATH中找你的图象文件,这不失为一中好的办法
//即使你的图象文件在jar包中,我们也可以很轻易的找到它
URL imgURL = getClass().getResource("img/test.gif");

//建立ImageIcon 类
ImageIcon icon = new ImageIcon(imgURL);

//由icon得到img
Image img = icon.getImage();

这样一来,把我上一篇文章<小议如何在 Applet 中显示图象>的代码稍微改动一点就可以在application中显示
图象了,连附例程如下:

import javax.swing.*;
import java.awt.*;
import java.net.URL;
import java.awt.image.*;

public class MyFrame extends JFrame {

int xpoint = 100, ypoint = 100;

public MyFrame() {
//Do frame stuff.
super("MyFrame");
}

public void paint(Graphics g) {
URL imgURL = getClass().getResource("img/test.gif");
ImageIcon icon = new ImageIcon(imgURL);
g.drawImage(icon.getImage(),xpoint,ypoint,this);
}

// main function
public static void main(String[] args) {
MyFrame frame = new MyFrame();
frame.pack();
frame.setVisible(true);
}

}


搜索更多相关主题的帖子: Application 图象 
2006-05-31 15:24
快速回复:[原创]在 Application 中显示图象
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017151 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved