登录 | 注册
首页 | 博客 | 论坛 | 招聘 | 文章 | 下载
程序之巢2012
快捷域名:http://blog.pfan.cn/sword2008 [订阅]
  • 首页
  • 数据结构
  • VC_多线程
  • VC_TCP/IP
  • VC_控件
  • VC_串口
  • VC_数据库
  • VC_进制
  • VC_基础
  • 排序算法
  • VC_DLL
  • ASP基础
  • ASP数据库
  • INI专区
  • VFW视频开发
  • C#串口通讯
  • vc snmp
  • 广告
  • WP7
  • 奇闻异趣
  • 强贴转载
  • JAVA技术基础
  • 本人收藏
  • 本人作品
  • 本人程序
  • 社会视角
  • 科技前沿
  • 收录问题
  • 碎碎念帖
  • socket专区[转贴]
  • 线程专区[转贴]
  • io专区[转贴]
  • j2me手机游戏设计
  • vc++学习贴
  • 我学VC++总结大贴
  • 指纹识别

博客介绍

博主:qq14923349

趁年轻,还输的起的时候,多做些以后不敢做的事情,即使失败,也没有遗憾..
夫英雄者,胸怀大志,腹有良谋,

正文

载入图片的几种常用方法2006-05-10 08:23:00

【评论】 【打印】 【字体:大 中 小】 本文链接:http://blog.pfan.cn/sword2008/13840.html

分享到:

载入图片的几种常用方法
/**
* <p>Title: PaintPanel</p>
* <p>Description:此程序演示如何载入图片,并让其作为panel的背景</p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: gift2u</p>
* @author liwu chinajavaworld
* @version 1.0
*/
import javax.swing.*;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.net.URL;
import java.net.*;

import java.awt.MediaTracker;
import java.io.File;
import javax.imageio.ImageIO;
import java.io.*;
import java.awt.Toolkit;

public class PaintPanel
    extends JPanel {
  Image image = null;

  /**
   * PaintPanel
   * 外部给图片,直接载入
   * @param image Image
   */
  public PaintPanel(Image image) {
    this.image = image;
  }

  /**
   * PaintPanel
   * 外部给出file引用,通过ImageIO载入
   * @param file File
   */
  

public PaintPanel(File file) {
    try {
      Image readImage = ImageIO.read(file);
      this.image = readImage;
    }
    catch (IOException ex) {
    }
  }
 
/**
   * PaintPanel
   *外部给出string路径,通过Toolkit载入
   * @param string String
   */
  public PaintPanel(String string) {
    URL url = null;
    try {
      url = new URL(string);
    }
    catch (MalformedURLException ex) {
    }
    image = Toolkit.getDefaultToolkit().getImage(url);

    MediaTracker tracker = new MediaTracker(this);
    tracker.addImage(image, 0);
    try {
      tracker.waitForID(0);
    }
    catch (InterruptedException ie) {
    }

  }

  /**
   * PaintPanel
   *外部给出ImageIcon,利用ImageIcon载入
   * @param icon ImageIcon
   */
  public PaintPanel(ImageIcon icon) {
    this.image = icon.getImage();
  }

  /**
   * PaintPanel
   * 外部给出URL,利用ImageIcon载入
   * @param icon url
   */
  public PaintPanel(URL url) {
    ImageIcon icon = new ImageIcon(url);
    this.image = icon.getImage();
  }

  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;
    if (image != null) {
      g2d.drawImage(image, 0, 0, this);
    }
  }
}

  测试代码:

import javax.swing.JFrame;
import java.net.URL;
import javax.swing.ImageIcon;
import java.awt.MediaTracker;
import java.awt.Image;
import java.net.MalformedURLException;
import java.awt.GridLayout;
import javax.swing.JDialog;
import java.io.File;

public class TestPaintPanel  {
  public static void main(String[] args) {
    JFrame fr = new JFrame();
    fr.setTitle("GIFT-PaintPanel-演示载入图片的方法");
    String urlstr = "http://photo.sohu.com/20040823/Img221677764.jpg";
    String filestr="D://a.jpg";
//如果是自己的机器上...un comment following......
//   String urlstr="file:///D://a.jpg";
    URL url = null;
    try {
      url = new URL(urlstr);
    }
    catch (MalformedURLException ex) {
    }

    ImageIcon icon = new ImageIcon(url);

    //////////////////loadimage//////////////////////
    Image image = fr.getToolkit().getImage(url);
    MediaTracker tracker = new MediaTracker(fr);
    tracker.addImage(image, 0);
    try {
      tracker.waitForID(0);
    }
    catch (InterruptedException ie) {}
    ////////////////////////////////////////////////
    fr.getContentPane().setLayout(new GridLayout(2, 2));
    fr.setSize(500, 600);

    fr.getContentPane().add(new PaintPanel(image));
    fr.getContentPane().add(new PaintPanel(urlstr));
    fr.getContentPane().add(new PaintPanel(icon));
    fr.getContentPane().add(new PaintPanel(url));
    //this is a litter different...
    JDialog dialog = new JDialog(fr, "GIFT-演示让图片成为背景", true);
    //本机上的文件...
    dialog.getContentPane().add(new PaintPanel(new File(filestr)));
    dialog.setSize(200, 200);

    fr.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    fr.setVisible(true);
    dialog.setVisible(true);
    fr.validate();
  }

}
: 运行图:


来源: 网上

阅读(4648) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册

关于本站 - 联系我们 - 广告服务 - 赞助本站 - 官方微博

Copyright© 1999-2025 Programfan.com All Rights Reserved

网站制作&维护:Hannibal    Email: webmaster@pfan.cn