javaz中背景图片的添加
2009-06-01 19:08
337 查看
这段程序演示了如何载入图片的几种常用方法,
并同时演示了如何让其作为JPanel的背景.
1:
2:
import java.awt.*;
import java.awt.event.*;
public class TestImage {
public TestImage() {
}
Frame f;
public static void main(String args[]){
TestImage b=new TestImage();
b.go();
}
public void go() {
Frame f=new Frame();
Image im=f.getToolkit().getImage("F://Wallpaper//宽屏壁纸d.jpg");
f.setSize(585,300);
f.setVisible(true);
Graphics e=f.getGraphics();
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
while(f.getFocusableWindowState()){
e.drawImage(im,0,0,580,300,f);
}
}
}
并同时演示了如何让其作为JPanel的背景.
1:
package test.International.chinajavaworld; 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(); } }
2:
package test.International.chinajavaworld; /** * 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); } } }
一个简单的frame背景
import java.awt.*;
import java.awt.event.*;
public class TestImage {
public TestImage() {
}
Frame f;
public static void main(String args[]){
TestImage b=new TestImage();
b.go();
}
public void go() {
Frame f=new Frame();
Image im=f.getToolkit().getImage("F://Wallpaper//宽屏壁纸d.jpg");
f.setSize(585,300);
f.setVisible(true);
Graphics e=f.getGraphics();
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
while(f.getFocusableWindowState()){
e.drawImage(im,0,0,580,300,f);
}
}
}
相关文章推荐
- 如何在MFC对话框中添加背景图片
- 添加水印-合并背景图片+动态生成码
- Swing编程---添加背景图片的方法
- Qt中添加背景图片
- Canvas添加一个背景图片,让字体在背景图片上从左向右滚动
- ios 给UIView添加背景图片
- Qt中添加背景图片的方法
- 如何给程序界面添加背景图片
- QT 对话框添加背景图片的方法
- mfc中 控件 对话框 添加颜色 背景图片
- 如何在使用itext生成pdf文档时给文档添加背景图片
- MFC中在界面上添加背景图片的方法
- jQuery动态移除和添加背景图片的方法详解
- 给navigationbar添加背景图片,并且按比例延伸
- VC中为SDI添加背景图片总结
- mfc中添加背景图片
- 怎样在symbian 程序中添加背景图片
- 代码方式给控件添加背景图片(WPF)
- 在MFC单文档窗口中添加背景图片
- JFram 添加背景图片