图片放大缩小程序


<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->  1  import  java.awt. * ;
 2  import  java.awt.event.ActionEvent;
 3  import  java.awt.event.ActionListener;
 4  import  javax.swing. * ;
 5  public   class  Main  extends  JFrame  implements  ActionListener
 6  {
 7       private  ImagePanel imgPane  =   null ;
 8       private  JScrollPane scrollPane  =   null ;
 9       private  JButton zoomin  =   null  ;
10       private  JButton zoomout  =   null ;
11       public  Main ()
12      {
13           super  ( " JScrollPane Demo " );
14          imgPane  =   new  ImagePanel ( " /1.jpg " );
15          imgPane.setPreferredSize ( new  Dimension ( 600 400 ));
16          scrollPane  =   new  JScrollPane (imgPane);
17          scrollPane.setVerticalScrollBarPolicy (JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
18          scrollPane.setHorizontalScrollBarPolicy (JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
19          zoomin  =   new  JButton ( " 放大 " );
20          zoomout  =   new  JButton ( " 缩小 " );
21          zoomin.setBounds ( 0 , 10 , 60 , 30 );
22          zoomout.setBounds ( 80 , 10 , 60 , 30 );
23          zoomin.addActionListener ( this );
24          zoomout.addActionListener ( this );
25          imgPane.add (zoomout,JLayeredPane.DRAG_LAYER);
26          imgPane.add (zoomin,JLayeredPane.DRAG_LAYER);
27           this .add (scrollPane);
28          setSize ( 600 400 );
29          setDefaultCloseOperation (EXIT_ON_CLOSE);
30          setVisible ( true );
31      }
32       public   static   void  main ( String[] args )
33      {
34           new  Main ();
35      }
36       public   void  actionPerformed ( ActionEvent e )
37      {
38           if  ((JButton) (e.getSource ())  ==  zoomin)
39          {
40              imgPane.enlarge ();
41              imgPane.setPreferredSize (imgPane.getPreferredSize ());
42              scrollPane.validate ();
43          }
44           else   if  ((JButton) (e.getSource ())  ==  zoomout)
45          {
46              imgPane.ensmall ();
47              imgPane.setPreferredSize (imgPane.getPreferredSize ());
48              scrollPane.validate ();
49          }
50      }
51  }
52 
53  class  ImagePanel  extends  JLayeredPane
54  {
55       private  Dimension theSize  =   new  Dimension ( 600 400 );
56       private  ImageIcon img  =   null ;
57       public  ImagePanel (String imgpath )
58      {
59           super  ();
60          setLayout ( null );
61           this .img  =   new  ImageIcon (getClass ().getResource (imgpath));
62      }
63       public   void  paintComponent ( Graphics g )
64      {
65          g.clearRect ( 0 0 1024 768 );
66          g.drawImage (img.getImage (),  0 0 , theSize.width, theSize.height, null );
67      }
68       public   void  enlarge ( )
69      {
70          theSize.width  =  (theSize.width  *   101 /   100 ;
71          theSize.height  =  (theSize.height  *   101 /   100 ;
72          setSize (theSize);
73      }
74       public   void  ensmall ( )
75      {
76          theSize.width  =  (theSize.width  *   100 /   101 ;
77          theSize.height  =  (theSize.height  *   100 /   101 ;
78          setSize (theSize);
79      }
80       public  Dimension getPreferredSize ( )
81      {
82           return   this .theSize;
83      }
84  }
85 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值