如果用java做一个类似photoshop的程序,首先载入一副图片,然后,其中一个功能是图形的放大缩小,这应该怎么实现呢,那么是否应该把图像的jpanel放到ScrollPane中,以便出现滚动条?有什么好方法吗?

解决方案 »

  1.   

    如果想实现图像处理,可以用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());
        }  }