📄 changeimg.java
字号:
package cn.dxm.util;
import java.awt.Dimension;
import java.awt.FileDialog;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import cn.dxm.frame.ImageFrame;
public class ChangeIMG {
private ImageIcon iicon;
private ImageUtil iu;
private String imgURL = null;
private BufferedImage img;
private BufferedImage img2;
// 将图片缩放到最恰当的大小
public BufferedImage ChangeIMGToProbleSize(String IMGsrc, JFrame jFrame) {
iicon = new ImageIcon(IMGsrc);
iu = new ImageUtil();
imgURL = IMGsrc;
img = ImageUtil.loadImage(imgURL);
int imgH = iicon.getIconHeight();
int imgW = iicon.getIconWidth();
int newW = 0;
int newH = 0;
Dimension dm = Toolkit.getDefaultToolkit().getScreenSize();
jFrame.setSize(dm.width, dm.height - 30);
int width = dm.width;
int hight = dm.height;
// 如果图片尺寸大于屏幕大小,就将其进行等比比例放缩
if (imgH > (hight - 20) || imgW > (width - 20)) {
double h = imgH;
double w = imgW;
double wDivh3 = h / w;
newW = dm.width - 100;
newH = (int) (newW * wDivh3);
// 如果进行图片缩放以后图片的高仍然大于800,就进行如下操作,将高设为700
if (newH >= (dm.height - 100)) {
double c = w / h;
newH = dm.height - 100;
newW = (int) (c * newH);
}
img2 = ImageUtil.resize(img, newW, newH);
return img2;
} else {
return img;
}
}
// 将图片放大
public BufferedImage enlargeIMG(BufferedImage img,BufferedImage srcImg) {
ImageIcon iicon = new ImageIcon(img);
int height = iicon.getIconHeight();
int width = iicon.getIconWidth();
int h ;
int w;
if((height>ImageFrame.frame.getHeight())||(width>ImageFrame.frame.getWidth())){
h=height;
w=width;
}else{
double wDivh1 = height;
double wDivh2 = width;
// 图片的高宽比值
double wDivh3 = wDivh1 / wDivh2;
double newW = width * (10.0 / 9.0);
double newH = (int) (newW * wDivh3);
h = (int) newH;
w = (int) newW;
}
BufferedImage bimage=null;
bimage=ImageUtil.resize(srcImg, w, h);
return bimage;
}
// 将图片缩小
public BufferedImage narrowIMG(BufferedImage img,BufferedImage srcImg) {
ImageIcon iicon = new ImageIcon(img);
int height = iicon.getIconHeight();
int width = iicon.getIconWidth();
int h ;
int w;
//当图片宽小于是10,高小于8时,图片大小将不发生变化.
if(width<10||height<8){
w=width;
h=height;
}else{
double wDivh1 = height;
double wDivh2 = width;
// h/w
double wDivh3 = wDivh1 / wDivh2;
double newW = width * (9.0 / 10.0);
double newH = (int) (newW * wDivh3);
w = (int) newW;
h = (int) newH;
}
BufferedImage bimage=null;
bimage=ImageUtil .resize(srcImg, w, h);
return bimage;
}
// 删除图片
public boolean deleteIMG(String src) {
File imgFile = new File(src);
return imgFile.delete();
}
// 保存图片.
public void saveImage(JFrame frame) {
FileDialog save_dialog = null;
File saveFile = null;
String saveDir = null;
String saveName = null;
String saveURL = null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -