📄 imageview.java
字号:
package my_cpt;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Calendar;
import super_cpt.*;
import javax.imageio.ImageIO;
import data2.Global;
public class ImageView extends SuperFrame implements ActionListener{
/**
* 图像浏览器
*/
private BufferedImage myImage;
//protected Scrollbar scroll=new Scrollbar(Scrollbar.HORIZONTAL);//水平滚动条
public final int mode;
public static final int SYSDISPOSE=1;
public static final int UNVISIBLE=2;
private Rectangle imgbounds=new Rectangle();
private StateBtn savebtn=new StateBtn("保存图像");
private StateBtn clsbtn=new StateBtn("关闭图像");
public ImageView(int windowmode)
{
super("图像浏览器");
setSize(400,300);
setLocation(312,184);
mode=windowmode;
add(savebtn);
add(clsbtn);
savebtn.setSize(70,20);
clsbtn.setSize(70,20);
savebtn.setLocation(getWidth()/2-70,getHeight()-25);
clsbtn.setLocation(getWidth()/2,getHeight()-25);
savebtn.addActionListener(this);
clsbtn.addActionListener(this);
}
public void FrameExit()
{
switch(mode)
{
case SYSDISPOSE:
dispose();
break;
case UNVISIBLE:
setVisible(false);
break;
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==savebtn)
{
if(myImage==null)return;
File file=new File(Global.OUTPUT_DIR);
if(!file.exists())
file.mkdir();
Calendar date=Calendar.getInstance();
String filename=Global.OUTPUT_DIR+"\\"+date.get(Calendar.HOUR_OF_DAY)
+date.get(Calendar.MINUTE)+date.get(Calendar.MINUTE)
+Global.IMAGEFORM;
try
{
ImageIO.write(myImage,"PNG",new File(filename));
this.setTitle("图像浏览器-"+filename+"已保存.");
repaint(0,0,getWidth(),25);
}catch(Exception ex){}
}
if(e.getSource()==clsbtn)
{
FrameExit();
}
}
public void setImage(final BufferedImage img)
{
myImage=img;
if(img==null)return;
double x=0,y=0,w=0,h=0;
double iw=img.getWidth(),ih=img.getHeight();
int rangeW=getWidth()-10,rangeH=getHeight()-50,rx=5,ry=22;
if(iw<rangeW && ih<rangeH)
{
x=rx+rangeW/2-iw/2;
y=ry+rangeH/2-ih/2;
w=iw;h=ih;
imgbounds.setBounds((int)x,(int)y,(int)w,(int)h);
return;
}
double rate2=(double)(getWidth()-10)/(getHeight()-50);
if(iw/ih>rate2)
{
w=rangeW;
h=ih*w/iw;
}else
{
h=rangeH;
w=iw*h/ih;
}
x=rx+rangeW/2-w/2;
y=ry+rangeH/2-h/2;
imgbounds.setBounds((int)x,(int)y,(int)w,(int)h);
repaint();
}
public void setVisible(boolean bool)
{
if(bool)
setTitle("图像浏览器");
super.setVisible(bool);
if(bool)
repaint();
}
public void myPaint(Graphics2D g2)
{
if(myImage!=null)
g2.drawImage(myImage,imgbounds.x,imgbounds.y,imgbounds.width,imgbounds.height,this);
else
g2.drawString("目前没有有效的图片",getWidth()/2-58,getHeight()/2-12);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -