jpanel中图片怎样放大缩小呢?

able0001 2006-04-22 10:40:10
如果用java做一个类似photoshop的程序,首先载入一副图片,然后,其中一个功能是图形的放大缩小,这应该怎么实现呢,那么是否应该把图像的jpanel放到ScrollPane中,以便出现滚动条?有什么好方法吗?
...全文
206 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
zclgod 2006-05-11
  • 打赏
  • 举报
回复
学习
zouxinfox 2006-05-02
  • 打赏
  • 举报
回复
如果想实现图像处理,可以用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());
}

}
yuzl32 2006-04-22
  • 打赏
  • 举报
回复
AffineTransformOp

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧