jpanel中图片怎样放大缩小呢? 如果用java做一个类似photoshop的程序,首先载入一副图片,然后,其中一个功能是图形的放大缩小,这应该怎么实现呢,那么是否应该把图像的jpanel放到ScrollPane中,以便出现滚动条?有什么好方法吗? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 如果想实现图像处理,可以用sun开发的JAI如果仅仅想实现放大缩小,那么比较简单这是一个示例,你可以找找灵感public void CreateThumbnail(Picture oldpicture,Picture newpicture,int maxwidth,int maxheight) throws Exception { try { File F = new File(oldpicture.getImgDir(),oldpicture.getImgfile()); if (!F.isFile()) return; getImagSize(oldpicture); double Ratio=getRadio(oldpicture,maxwidth,maxheight); BufferedImage Bi = ImageIO.read(F); AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(Ratio, Ratio), null); Image Itemp = op.filter(Bi, null); File ThF = new File(newpicture.getImgDir(),newpicture.getImgfile()); ImageIO.write((BufferedImage)Itemp, ext, ThF); }catch (Exception ex) { throw new Exception(" ImageIo.write error in CreatThum.: "+ex.getMessage()); } } public double getRadio(Picture picture,int maxwidth,int maxheight){ double Ratio=1.0; if (picture.getWidth()>picture.getHeight()){ Ratio = (new Integer(maxwidth)).doubleValue()/picture.getWidth(); }else if (picture.getWidth()<picture.getHeight()){ Ratio=(new Integer(maxheight)).doubleValue()/picture.getHeight(); }else if (picture.getWidth()==picture.getHeight()){ if (picture.getWidth()>maxwidth){ Ratio = (new Integer(maxwidth)).doubleValue()/picture.getWidth(); } } return Ratio; } public void getImagSize(Picture picture) throws Exception{ double Ratio=1.0; try{ File F = new File(picture.getImgDir(),picture.getImgfile()); if (!F.isFile()) throw new Exception(F+" is not image file error !"); BufferedImage Bi = ImageIO.read(F);// Image Itemp = Bi.getScaledInstance (maxwidth,maxheight,Bi.SCALE_SMOOTH); picture.setWidth(Bi.getWidth()); picture.setHeight(Bi.getHeight()); }catch (Exception ex) { throw new Exception(" getImagSize: "+ex.getMessage()); } } java的继承问题 NetBeans 如何向panel里动态添加控件? sql server数据库连接报错End of stream was detected on a read 很小很小一个问题.......麻烦指点一下.... 【急】用JAVA做的扫雷,但是程序有问题,无法实现扫雷功能 大物理学家霍金 受迫害解密 jbuilder6和weblogic6.1同用时出了问题? 关于--违例差错控制--的问题! 一个关于I/O的问题 关于路径问题? jawin中调用dll文件中函数是不是一定要用invoke函数?(知道答案的请进来,小弟十万火急) 发送UDP时的问题
如果仅仅想实现放大缩小,那么比较简单
这是一个示例,你可以找找灵感
public void CreateThumbnail(Picture oldpicture,Picture newpicture,int maxwidth,int maxheight) throws Exception
{
try {
File F = new File(oldpicture.getImgDir(),oldpicture.getImgfile());
if (!F.isFile())
return; getImagSize(oldpicture);
double Ratio=getRadio(oldpicture,maxwidth,maxheight); BufferedImage Bi = ImageIO.read(F);
AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(Ratio, Ratio), null);
Image Itemp = op.filter(Bi, null); File ThF = new File(newpicture.getImgDir(),newpicture.getImgfile());
ImageIO.write((BufferedImage)Itemp, ext, ThF); }catch (Exception ex) {
throw new Exception(" ImageIo.write error in CreatThum.: "+ex.getMessage());
} } public double getRadio(Picture picture,int maxwidth,int maxheight){
double Ratio=1.0; if (picture.getWidth()>picture.getHeight()){
Ratio = (new Integer(maxwidth)).doubleValue()/picture.getWidth();
}else if (picture.getWidth()<picture.getHeight()){
Ratio=(new Integer(maxheight)).doubleValue()/picture.getHeight();
}else if (picture.getWidth()==picture.getHeight()){
if (picture.getWidth()>maxwidth){
Ratio = (new Integer(maxwidth)).doubleValue()/picture.getWidth();
} }
return Ratio;
} public void getImagSize(Picture picture) throws Exception{ double Ratio=1.0; try{
File F = new File(picture.getImgDir(),picture.getImgfile());
if (!F.isFile())
throw new Exception(F+" is not image file error !"); BufferedImage Bi = ImageIO.read(F);
// Image Itemp = Bi.getScaledInstance (maxwidth,maxheight,Bi.SCALE_SMOOTH); picture.setWidth(Bi.getWidth());
picture.setHeight(Bi.getHeight());
}catch (Exception ex) {
throw new Exception(" getImagSize: "+ex.getMessage());
} }