一个简单的window照片查看器

 

调试一下午,可以对了,这是一个照片查看器的雏形

图片的路径可以更改,图片的名字为1.jpg 2.jpg等,也

可以随时更改!

Code:
  1. import java.awt.BorderLayout;   
  2. import java.awt.event.ActionEvent;   
  3. import java.awt.event.ActionListener;   
  4. import java.awt.geom.AffineTransform;   
  5. import java.awt.image.AffineTransformOp;   
  6. import java.awt.image.BufferedImage;   
  7. import java.io.FileInputStream;   
  8. import java.io.FileNotFoundException;   
  9. import java.io.IOException;   
  10. import javax.imageio.ImageIO;   
  11. import javax.swing.*;   
  12.   
  13. public class A extends JFrame implements ActionListener{   
  14.     static int i=1;   
  15.     String path=null;   
  16.     JTabbedPane tabbedPane = new JTabbedPane();   
  17.     JPanel jp = new JPanel();   
  18.     BufferedImage sourceImage;   
  19.       JButton jb1=new JButton("上一张");   
  20.       JButton jb2=new JButton("下一张");   
  21.     public A(int i) {   
  22.            
  23.         jb1.addActionListener(this);   
  24.         jb2.addActionListener(this);   
  25.         this.setTitle("Java版照片查看器");   
  26.         this.setBounds(300,250,700500);   
  27.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
  28.         try {   
  29.             a(true,i);   
  30.         } catch (FileNotFoundException e) {   
  31.             JOptionPane.showMessageDialog(null,"图片路径有误/n当前路径为:"+path,"错误!",JOptionPane.ERROR_MESSAGE);   
  32.             e.printStackTrace();   
  33.         } catch (IOException e) {   
  34.             e.printStackTrace();   
  35.         }   
  36.         this.setVisible(true);   
  37.     }   
  38.   
  39.     public static void main(String[] args) {   
  40.         A a = new A(i);   
  41.     }   
  42.   
  43.     public void a(boolean flag,int i) throws FileNotFoundException, IOException {   
  44.         if(!flag){   
  45.             this.getContentPane().remove(tabbedPane);   
  46.             this.getContentPane().remove(jp);   
  47.         }   
  48.         //获取当前用户的路径   
  49.         path = System.getProperty("user.dir");   
  50.         //sourceImage = ImageIO.read(new FileInputStream(path   
  51.         //      + "//src//car//code//"+i+".jpg"));//照片的相对路径   
  52.         sourceImage = ImageIO.read(new FileInputStream(path+"/"+i+".jpg"));//照片的相对路径   
  53.         BufferedImage dstImage = null;   
  54.         BufferedImage highImage = null;   
  55.         BufferedImage levelImage = null;   
  56.         AffineTransform transform = new AffineTransform(-1001,   
  57.                 sourceImage.getWidth() - 10);// 水平翻转   
  58.         AffineTransform transform1 = new AffineTransform(100, -10,   
  59.                 sourceImage.getHeight() - 1);// 垂直翻转   
  60.         AffineTransform transform2 = new AffineTransform(-100, -1,   
  61.                 sourceImage.getWidth() - 1, sourceImage.getHeight() - 1);// 旋转180度   
  62.   
  63.         AffineTransformOp op = new AffineTransformOp(transform,   
  64.                 AffineTransformOp.TYPE_BILINEAR);   
  65.         AffineTransformOp op1 = new AffineTransformOp(transform1,   
  66.                 AffineTransformOp.TYPE_BILINEAR);   
  67.         AffineTransformOp op2 = new AffineTransformOp(transform2,   
  68.                 AffineTransformOp.TYPE_BILINEAR);   
  69.   
  70.         dstImage = op.filter(sourceImage, null);   
  71.         highImage = op1.filter(sourceImage, null);   
  72.         levelImage = op2.filter(sourceImage, null);   
  73.         tabbedPane.add("照片原图"new JLabel(new ImageIcon(sourceImage)));   
  74.         tabbedPane.add("水平翻转图"new JLabel(new ImageIcon(dstImage)));   
  75.         tabbedPane.add("垂直翻转图"new JLabel(new ImageIcon(highImage)));   
  76.         tabbedPane.add("旋转180度图"new JLabel(new ImageIcon(levelImage)));   
  77.         this.getContentPane().add(tabbedPane, BorderLayout.NORTH);   
  78.            jp.add(jb1);   
  79.            jp.add(jb2);   
  80.            this.getContentPane().add(jp);   
  81.     }   
  82.   
  83.     @Override  
  84.     public void actionPerformed(ActionEvent e) {   
  85.     if(e.getSource()==jb1){   
  86.         i=(--i)%6;   
  87.         if(i==0){   
  88.             i=5;   
  89.         }   
  90.         new A(i);   
  91.         this.dispose();   
  92.         try {   
  93.             a(false,i);   
  94.         } catch (FileNotFoundException e1) {   
  95.             JOptionPane.showMessageDialog(null,"图片路径有误/n当前路径为:"+path,"错误!",JOptionPane.ERROR_MESSAGE);   
  96.             e1.printStackTrace();   
  97.         } catch (IOException e1) {   
  98.             e1.printStackTrace();   
  99.         }   
  100.        
  101.     }else if(e.getSource()==jb2){   
  102.         i=(++i)%6;   
  103.         if(i==0){   
  104.             i=1;   
  105.         }   
  106.         new A(i);   
  107.         this.dispose();   
  108.         try {   
  109.             a(false,i);   
  110.         } catch (FileNotFoundException e1) {   
  111.             JOptionPane.showMessageDialog(null,"图片路径有误/n当前路径为:"+path,"错误!",JOptionPane.ERROR_MESSAGE);   
  112.             e1.printStackTrace();   
  113.         } catch (IOException e1) {   
  114.             e1.printStackTrace();   
  115.         }   
  116.     }   
  117.     }   
  118.   
  119. }   

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值