java图片查看器_JAVA写的图片查看器

这是一个使用Java实现的简单图像查看器,具备打开、缩放、翻转、移动和边缘检测等功能。用户可以通过输入框指定缩放因子和翻转角度,程序会实时更新图像。此外,还支持鼠标拖动进行图像移动,并能进行边缘检测操作。
摘要由CSDN通过智能技术生成

运行前在c:\放一张名称为aa.jpg的图片

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

import javax.swing.event.*;

import javax.swing.filechooser.FileFilter;

import java.awt.image.*;

import java.awt.geom.AffineTransform;

import java.io.*;

import java.util.ArrayList;

import java.awt.image.ConvolveOp;

import java.awt.Point;

import java.awt.geom.*;

public class ImageViewer extends JFrame

{

private Image img;

private JButton button1,button2,button3,button4,button5,button6,button7,button8;

private JPanel panel;

Container c;

DrawPanel dp;

public boolean start=false;

public int flag=0;

String name="c:\\aa.jpg";

public ImageViewer()

{

super("ImageViewer");

c=getContentPane();

panel=new JPanel();

dp=new DrawPanel(name);

c.add(dp,BorderLayout.CENTER);

final MouseHandler handler=new MouseHandler(dp);

dp.addMouseMotionListener(handler);

dp.addMouseListener(handler);

button1=new JButton("打开");

panel.add(button1);

button1.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

JFileChooser chooser=new JFileChooser();

chooser.setCurrentDirectory(new File("."));

final ExtensionFileFilter filter=new ExtensionFileFilter();

filter.addExtension("jpg");

filter.addExtension("jpeg");

filter.addExtension("gif");

filter.setdescription("Image Files");

chooser.setFileFilter(filter);

int result=chooser.showOpenDialog(dp);

if(result==JFileChooser.APPROVE_OPTION)

{

name=chooser.getSelectedFile().getPath();

getContentPane().remove(dp);

removeMouseListener(handler);

dp = new DrawPanel(name);

getContentPane().add(dp, BorderLayout.CENTER);

MouseHandler handler=new MouseHandler(dp);

dp.addMouseMotionListener(handler);

dp.addMouseListener(handler);

dp.revalidate();

}

}

});

button2=new JButton("缩放");

panel.add(button2);

button2.addActionListener(new Handler2());

button3=new JButton("翻转");

panel.add(button3);

button3.addActionListener(new Handler3());

button4=new JButton("移动");

panel.add(button4);

button4.addActionListener(new Handler4());

button5=new JButton("还原");

panel.add(button5);

button5.addActionListener(new Handler5());

button6=new JButton("边缘");

panel.add(button6);

button6.addActionListener(new Handler6());

button7=new JButton("画图");

panel.add(button7);

button7.addActionListener(new Handler7());

button8=new JButton("退出");

panel.add(button8);

button8.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

System.exit(0);

}

});

c.add(panel,BorderLayout.SOUTH);

setSize(550,350);

show();

}

private class Handler2 implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

String str=JOptionPane.showInputDialog(null,"Please input zoom factor","Message",1);

System.out.println(str);

if((str==null)||(str.length()==0))

JOptionPane.showMessageDialog(null,"The data cannot be null","Message",1);

else

dp.zoom(Double.parseDouble(str));

}

}

private class Handler3 implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

String str=JOptionPane.showInputDialog(null,"Please input flip angle","Message",1);

if((str==null)||(str.length()==0))

JOptionPane.showMessageDialog(null,"The data cannot be null","Message",1);

else

dp.rotate(Integer.parseInt(str));

}

}

private class Handler4 implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

dp.flag=3;

}

}

private class Handler5 implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

dp.OriginalImage();

}

}

private class Handler6 implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

dp.Edge();

}

}

private class Handler7 implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

dp.flag=5;

}

}

private static  class ExtensionFileFilter extends FileFilter

{

private String description="";

private ArrayList extensions=new ArrayList();

public void addExtension(String extension)

{

if(!extension.startsWith("."))

extension="."+extension;

extensions.add(extension.toLowerCase());

}

public void setdescription(String aDescription)

{

description=aDescription;

}

public String getDescription()

{

return description;

}

public boolean accept(File f)

{

if(f.isDirectory())

return true;

String name=f.getName().toLowerCase();

for(int i=0;iif(name.endsWith((String)extensions.get(i)))

return true;

return false;

}

}

public static void main(String[] args)

{

ImageViewer app=new ImageViewer();

app.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

}

}

class MouseHandler extends MouseInputAdapter

{

private Point currentPos,lastPos;

private DrawPanel drawpanel;

public  MouseHandler(DrawPanel panel)

{

this.drawpanel=panel;

}

public void mousePressed(MouseEvent e)

{

currentPos=e.getPoint();

lastPos=e.getPoint();

drawpanel.drawIt(lastPos,currentPos);

}

public void mouseDragged(MouseEvent e)

{

currentPos=e.getPoint();

drawpanel.drawIt(lastPos,currentPos);

lastPos=currentPos;

}

}

class DrawPanel extends JPanel

{

public Image img;

BufferedImage bi,bi1,bi2,biEdge,bimg,buffer;

int newX=400,newY=400;

int flag=0;

int x, y;

private Point currentPos,lastPos;

MouseHandler mousehandler;

Graphics2D bufferG;

public DrawPanel(String name)

{

img=Toolkit.getDefaultToolkit().getImage(name);

this.addMouseMotionListener(new MouserMotionHandler());

MediaTracker mt=new MediaTracker(this);

mt.addImage(img,1);

try

{

mt.waitForAll();

}catch(Exception e)

{

System.out.println("Exception While Loading.");

}

if(img.getWidth(this)==-1)

{

System.out.println("***make sure you have the textture image"+"*.jpg file in the same directory.***");

System.exit(0);

}

if(img.getWidth(this)!=0 && img.getHeight(this)!=0)

bi=new BufferedImage(img.getWidth(this),img.getHeight(this),BufferedImage.TYPE_INT_ARGB);

Graphics2D big=bi.createGraphics();

big.drawImage(img,0,0,this);

bi1=bi;

bi2=bi;

biEdge=bi;

}

private class MouserMotionHandler extends MouseMotionAdapter

{

public void mouseDragged(MouseEvent e)

{

x=e.getX();

y=e.getY();

repaint();

}

}

public void zoom(double factor)

{

int w=img.getWidth(this);

int h=img.getHeight(this);

newX=(int)(w*factor);

newY=(int)(h*factor);

bi1=new BufferedImage(newX,newY,BufferedImage.TYPE_INT_ARGB);

Graphics2D big=bi1.createGraphics();

big.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);

big.drawImage(img,0,0,newX,newY,this);

repaint();

flag=1;

}

public void rotate(int degree)

{

int angle=degree%360;

int w=img.getWidth(this);

int h=img.getHeight(this);

if(img.getWidth(this)!=0 && img.getHeight(this)!=0)

bi2=new BufferedImage(bi.getWidth(this),bi.getHeight(this),BufferedImage.TYPE_INT_ARGB);

Graphics2D big1=bi2.createGraphics();

AffineTransform. at=new AffineTransform();

at.translate(w/10,h/10);

at.rotate(Math.toRadians(angle));

big1.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);

big1.drawImage(img,at,this);

repaint();

flag=2;

}

public void OriginalImage()

{

flag=0;

repaint();

}

public void drawIt(Point lastPos,Point currentPos)

{

this.currentPos=currentPos;

this.lastPos=lastPos;

repaint();

}

public void Edge()

{

float elements[]={0.0f,-1.0f,0.0f,-1.0f,4.0f,-1.0f,0.0f,-1.0f,0.0f};

int w=img.getWidth(this);

int h=img.getHeight(this);

if(img.getWidth(this)!=0 && img.getHeight(this)!=0)

biEdge=new BufferedImage(w,h,BufferedImage.TYPE_INT_ARGB);

Graphics2D bigEdge=biEdge.createGraphics();

bigEdge.drawImage(img,0,0,this);

BufferedImageOp biop=null;

AffineTransform. at=new AffineTransform();

if(img.getWidth(this)!=0 && img.getHeight(this)!=0)

bimg=new BufferedImage(w,h,BufferedImage.TYPE_INT_ARGB);

Kernel kernel=new Kernel(3,3,elements);

ConvolveOp cop=new ConvolveOp(kernel,ConvolveOp.EDGE_NO_OP,null);

cop.filter(biEdge,bimg);

biop=new AffineTransformOp(at,AffineTransformOp.TYPE_NEAREST_NEIGHBOR);

repaint();

flag=4;

}

public void paintComponent(Graphics g)

{

super.paintComponent(g);

Graphics2D g2D=(Graphics2D)g;

if(flag==0)

g2D.drawImage(bi,0,0,this);

else if(flag==1)

g2D.drawImage(bi1,0,0,this);

else if(flag==2)

g2D.drawImage(bi2,0,0,this);

else if(flag==3)

g2D.drawImage(bi,x,y,this);

else if(flag==4)

g2D.drawImage(bimg,0,0,this);

else if(flag==5)

{

if(buffer==null)

{

buffer=(BufferedImage)this.createImage(this.getWidth(),this.getHeight());

bufferG=buffer.createGraphics();

}

if(currentPos!=null && lastPos!=null)

bufferG.draw(new Line2D.Double(lastPos,currentPos));

g2D.drawImage(buffer,0,0,this);

}

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值