图象缓冲的应用双缓冲绘图:
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // @title:BufImgJpane
4 //
5 // @discription 实现了双缓冲画图
6 //
7 // @author 爪洼河
8 //
9 // @date 2006-8
10 //
11 ////////////////////////////////////////////////////////////////////////////////////
12 import java.awt.Graphics;
13 import java.awt.Graphics2D;
14 import java.awt.Image;
15 import java.awt.image.BufferedImage;
16 import javax.swing.ImageIcon;
17 import javax.swing.JButton;
18 import javax.swing.JFrame;
19 import javax.swing.JPanel;
20 public class BufImgJpane extends JPanel
21 {
22 Image img ;
23 BufferedImage bufimg ;
24 public BufImgJpane ()
25 {
26 ImageIcon icon = new ImageIcon (getClass ().getResource ("1.jpg")) ;
27 img = icon.getImage () ;
28 bufimg = new BufferedImage (img.getWidth (this) , img.getHeight (this) ,
29 BufferedImage.TYPE_3BYTE_BGR) ;
30 Graphics2D g2 = bufimg.createGraphics () ;
31 g2.clearRect (0 , 0 , img.getWidth (this) , img.getHeight (this)) ;
32 g2.drawImage (img , 0 , 0 , img.getWidth (this) , img.getHeight (this) ,
33 this) ;
34 }
35 public void paintComponent (Graphics g)
36 {
37
38 g.drawImage (bufimg , 0 , 0 , this.getWidth () , this.getHeight () ,
39 this) ;
40 }
41 public static void main (String args[])
42 {
43 BufImgJpane bufimgpane = new BufImgJpane () ;
44 JFrame frame = new JFrame ("双缓冲画图") ;
45 frame.add (bufimgpane) ;
46 frame.setSize (610 , 625) ;
47 frame.setVisible (true) ;
48 frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE) ;
49 }
50 }
51
图象缓冲的应用截图:
1 ///////////////////////////////////////////////////
2 //
3 // @name TestImageIO.java
4 //
5 // @discription 首先测试把一个容器通过按钮事件存为一张图片。
6 //
7 // @author java&he
8 //
9 // @date 2006-12
10 //
11 ////////////////////////////////////////////////////
12 import java.awt.Graphics;
13 import java.awt.event.ActionEvent;
14 import java.awt.event.ActionListener;
15 import java.awt.image.BufferedImage;
16 import java.io.File;
17 import javax.imageio.ImageIO;
18 import javax.swing.*;
19 public class TestImageIO
20 {
21 JFrame frame = new JFrame ("测试截屏");
22 JPanel panel = new JPanel ();
23 public TestImageIO ()
24 {
25
26 frame.setSize (200,300);
27
28 frame.add ("Center",panel);
29 panel.setLayout (null);
30 JButton btn = new JButton ("paint");
31 btn.setBounds (100,100,100,50);
32 panel.add (btn);
33 btn.addActionListener (new ActionListener ()
34 {
35 public void actionPerformed (ActionEvent evt)
36 {
37 BufferedImage bufimg = new BufferedImage (TestImageIO.this.frame.getBounds ().width,TestImageIO.this.frame.getBounds ().height,BufferedImage.TYPE_INT_RGB);
38 Graphics g = bufimg.getGraphics ();
39 TestImageIO.this.frame.paint (g);
40 g.dispose ();
41 try
42 {
43 ImageIO.write (bufimg,"jpg",new File ("Test.jpg"));
44
45 }
46 catch(Exception e)
47 {
48 e.printStackTrace ();
49 }
50 }
51 });
52 frame.setVisible (true);
53 frame.setDefaultCloseOperation (frame.EXIT_ON_CLOSE);
54 }
55 public static void main (String[] args)
56 {
57 new TestImageIO ();
58 }
59
60 }
61
posted on 2007-02-07 09:45
-274°C 阅读(345)
评论(0) 编辑 收藏 所属分类:
JAVA