Title: PaintPanel
*Description:此程序演示如何载入图片,并让其作为panel的背景
*Copyright: Copyright (c) 2005
*Company: gift2u
* @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); } }} Java, java, J2SE, j2se, J2EE, j2ee, J2ME, j2me, ejb, ejb3, JBOSS, jboss, spring, hibernate, jdo, struts, webwork, ajax, AJAX, mysql, MySQL, Oracle, Weblogic, Websphere, scjp, scjd 个人的?