📄 imageframe.java
字号:
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.Image;
import java.awt.Graphics2D;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.image.*;
import java.awt.event.*;
import java.awt.RenderingHints;
import java.awt.geom.*;
public class ImageFrame extends JFrame {
public ImageFrame(ImagePanel p) //构造函数
{
iPanel=p;
//初始化
Toolkit kit=Toolkit.getDefaultToolkit();
Dimension screenSize=kit.getScreenSize();
setBounds(screenSize.width/2-screenSize.width*3/8,screenSize.height/2-screenSize.height*3/8,screenSize.width*3/4,screenSize.height*3/4);
Container contentPane=getContentPane();
contentPane.setLayout(new BorderLayout());
l=new JLabel();
contentPane.add(new JScrollPane(l),BorderLayout.CENTER);
tBar=new JToolBar();
//设置工具栏
tBar.add(new ChangeImageAction(-1));
tBar.add(new ChangeImageAction(1));
tBar.add(new ZoomAction(2));
tBar.add(new ZoomAction(0));
tBar.add(new RotateAction(-1));
tBar.add(new RotateAction(1));
tBar.add(new BrowseAction());
contentPane.add(tBar,BorderLayout.NORTH);
super.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){ //响应关闭事件
setVisible(false);
}
});
}
public void setImage(BufferedImage bi,int i) //设置所显示图像
{
int w=bi.getWidth();
int h=bi.getHeight();
image=bi;
iNum=i;
imageShow=new BufferedImage(w,h,BufferedImage.TYPE_INT_ARGB);
imageShow.createGraphics().drawImage(bi,0,0,null);
l.setIcon(new ImageIcon(imageShow)); //显示该图像
l.setHorizontalAlignment(JLabel.CENTER);
}
public void setImage(BufferedImage bi) //类内部使用以更换当前显示图像
{
int w=bi.getWidth();
int h=bi.getHeight();
imageShow=new BufferedImage(w,h,BufferedImage.TYPE_INT_ARGB);
imageShow.createGraphics().drawImage(bi,0,0,null);
l.setIcon(new ImageIcon(imageShow)); //显示该图像
l.setHorizontalAlignment(JLabel.CENTER);
}
public void showMessage(Object o){ //弹出错误提示
JOptionPane.showMessageDialog(this,o);
}
class BrowseAction extends AbstractAction //返回浏览模式
{
public BrowseAction()
{
ImageIcon i=new ImageIcon(getClass().getResource("icon/browse.JPG"));
putValue(Action.NAME,"Browse");
putValue(Action.SHORT_DESCRIPTION,"浏览图像");
putValue(Action.SMALL_ICON,i);
}
public void actionPerformed(ActionEvent e)
{
setVisible(false);
}
}
class ZoomAction extends AbstractAction //对图像进行缩放处理
{
public ZoomAction(int s){
if(s==2)
{ //放大图标初始化
ImageIcon i=new ImageIcon(getClass().getResource("icon/zoom+.JPG"));
putValue(Action.NAME,"Zoom+");
putValue(Action.SHORT_DESCRIPTION,"放大");
putValue(Action.SMALL_ICON,i);
style=2;
}
else
{ //缩小图标初始化
ImageIcon i=new ImageIcon(getClass().getResource("icon/zoom-.JPG"));
putValue(Action.NAME,"Zoom-");
putValue(Action.SHORT_DESCRIPTION,"缩小");
putValue(Action.SMALL_ICON, i);
style=0.5;
}
}
public void actionPerformed(ActionEvent e)
{
int newW=(int)(imageShow.getWidth()*style); //计算改变后图像大小
int newH=(int)(imageShow.getHeight()*style);
try{
BufferedImage bi=new BufferedImage(newW,newH,BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D g2d=bi.createGraphics();
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2d.drawImage( imageShow,0,0,newW,newH,null); //画出图像
setImage(bi); //显示图像
bi.flush();
}catch(OutOfMemoryError er){
showMessage(er);
}
}
double style;
}
class RotateAction extends AbstractAction //对图像进行旋转处理
{
public RotateAction(int s)
{
style=s;
if(style==1)
{ //顺时针旋转图标初始化
ImageIcon i=new ImageIcon(getClass().getResource("icon/rotatec.JPG"));
putValue(Action.NAME,"rotate");
putValue(Action.SHORT_DESCRIPTION,"顺时针旋转");
putValue(Action.SMALL_ICON,i);
}
else
{ //逆时针旋转图标初始化
ImageIcon i=new ImageIcon(getClass().getResource("icon/rotateanti.JPG"));
putValue(Action.NAME,"rotateanti");
putValue(Action.SHORT_DESCRIPTION,"逆时针旋转");
putValue(Action.SMALL_ICON,i);
}
}
public void actionPerformed(ActionEvent e)
{
int w=imageShow.getWidth();
int h=imageShow.getHeight();
int angle;
if(style==1)
angle=90;
else
angle=270;
try{
AffineTransform at=AffineTransform.getRotateInstance( Math.toRadians(angle),w/2,h/2); //设置旋转参数
AffineTransformOp ato=new AffineTransformOp(at,AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
BufferedImage bi=new BufferedImage(h,w,BufferedImage.TYPE_INT_ARGB);
ato.filter(imageShow,bi); //转换图像
setImage(bi); //显示图像
bi.flush();
}catch(OutOfMemoryError er){
showMessage(er);
}
}
int style;
}
class ChangeImageAction extends AbstractAction //切换图像处理
{
public ChangeImageAction(int s)
{
style=s;
if(style==1)
{
ImageIcon i=new ImageIcon(getClass().getResource("icon/next.JPG"));
putValue(Action.NAME,"next");
putValue(Action.SHORT_DESCRIPTION,"下一个图像");
putValue(Action.SMALL_ICON,i);
}
else
{
ImageIcon i=new ImageIcon(getClass().getResource("icon/previous.JPG"));
putValue(Action.NAME,"previous");
putValue(Action.SHORT_DESCRIPTION,"上一个图像");
putValue(Action.SMALL_ICON,i);
}
}
public void actionPerformed(ActionEvent e)
{
if(style==1)
iPanel.setImage(iNum+1); //下一个图像
else
iPanel.setImage(iNum-1); //上一个图像
}
int style;
}
static BufferedImage image; //原始图像
static BufferedImage imageShow; //显示的图像
JLabel l;
JToolBar tBar;
ImagePanel iPanel;
int iNum; //当前显示图像的数组号
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -