调试一下午,可以对了,这是一个照片查看器的雏形
图片的路径可以更改,图片的名字为1.jpg 2.jpg等,也
可以随时更改!
- import java.awt.BorderLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.geom.AffineTransform;
- import java.awt.image.AffineTransformOp;
- import java.awt.image.BufferedImage;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import javax.imageio.ImageIO;
- import javax.swing.*;
- public class A extends JFrame implements ActionListener{
- static int i=1;
- String path=null;
- JTabbedPane tabbedPane = new JTabbedPane();
- JPanel jp = new JPanel();
- BufferedImage sourceImage;
- JButton jb1=new JButton("上一张");
- JButton jb2=new JButton("下一张");
- public A(int i) {
- jb1.addActionListener(this);
- jb2.addActionListener(this);
- this.setTitle("Java版照片查看器");
- this.setBounds(300,250,700, 500);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- try {
- a(true,i);
- } catch (FileNotFoundException e) {
- JOptionPane.showMessageDialog(null,"图片路径有误/n当前路径为:"+path,"错误!",JOptionPane.ERROR_MESSAGE);
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- this.setVisible(true);
- }
- public static void main(String[] args) {
- A a = new A(i);
- }
- public void a(boolean flag,int i) throws FileNotFoundException, IOException {
- if(!flag){
- this.getContentPane().remove(tabbedPane);
- this.getContentPane().remove(jp);
- }
- //获取当前用户的路径
- path = System.getProperty("user.dir");
- //sourceImage = ImageIO.read(new FileInputStream(path
- // + "//src//car//code//"+i+".jpg"));//照片的相对路径
- sourceImage = ImageIO.read(new FileInputStream(path+"/"+i+".jpg"));//照片的相对路径
- BufferedImage dstImage = null;
- BufferedImage highImage = null;
- BufferedImage levelImage = null;
- AffineTransform transform = new AffineTransform(-1, 0, 0, 1,
- sourceImage.getWidth() - 1, 0);// 水平翻转
- AffineTransform transform1 = new AffineTransform(1, 0, 0, -1, 0,
- sourceImage.getHeight() - 1);// 垂直翻转
- AffineTransform transform2 = new AffineTransform(-1, 0, 0, -1,
- sourceImage.getWidth() - 1, sourceImage.getHeight() - 1);// 旋转180度
- AffineTransformOp op = new AffineTransformOp(transform,
- AffineTransformOp.TYPE_BILINEAR);
- AffineTransformOp op1 = new AffineTransformOp(transform1,
- AffineTransformOp.TYPE_BILINEAR);
- AffineTransformOp op2 = new AffineTransformOp(transform2,
- AffineTransformOp.TYPE_BILINEAR);
- dstImage = op.filter(sourceImage, null);
- highImage = op1.filter(sourceImage, null);
- levelImage = op2.filter(sourceImage, null);
- tabbedPane.add("照片原图", new JLabel(new ImageIcon(sourceImage)));
- tabbedPane.add("水平翻转图", new JLabel(new ImageIcon(dstImage)));
- tabbedPane.add("垂直翻转图", new JLabel(new ImageIcon(highImage)));
- tabbedPane.add("旋转180度图", new JLabel(new ImageIcon(levelImage)));
- this.getContentPane().add(tabbedPane, BorderLayout.NORTH);
- jp.add(jb1);
- jp.add(jb2);
- this.getContentPane().add(jp);
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- if(e.getSource()==jb1){
- i=(--i)%6;
- if(i==0){
- i=5;
- }
- new A(i);
- this.dispose();
- try {
- a(false,i);
- } catch (FileNotFoundException e1) {
- JOptionPane.showMessageDialog(null,"图片路径有误/n当前路径为:"+path,"错误!",JOptionPane.ERROR_MESSAGE);
- e1.printStackTrace();
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- }else if(e.getSource()==jb2){
- i=(++i)%6;
- if(i==0){
- i=1;
- }
- new A(i);
- this.dispose();
- try {
- a(false,i);
- } catch (FileNotFoundException e1) {
- JOptionPane.showMessageDialog(null,"图片路径有误/n当前路径为:"+path,"错误!",JOptionPane.ERROR_MESSAGE);
- e1.printStackTrace();
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- }
- }
- }