Pudgy's World
          posts - 13,  comments - 16,  trackbacks - 0

          ?Rotate Image 45 Degrees

          ?


          /*

          Java?Media?APIs:?Cross-Platform?Imaging,?Media?and?Visualization
          Alejandro?Terrazas
          Sams,?Published?November?2002,?
          ISBN?0672320940
          */


          import?java.awt.Graphics;
          import?java.awt.Graphics2D;
          import?java.awt.Image;
          import?java.awt.Insets;
          import?java.awt.MediaTracker;
          import?java.awt.Toolkit;
          import?java.awt.geom.AffineTransform;
          import?java.awt.geom.Point2D;
          import?java.awt.image.AffineTransformOp;
          import?java.awt.image.BufferedImage;
          import?java.awt.image.BufferedImageOp;

          import?javax.swing.JFrame;

          /**
          ?*?RotateImage45Degrees.java?-?1.?scales?an?image's?dimensions?by?a?factor?of
          ?*?two?2.?rotates?it?45?degrees?around?the?image?center?3.?displays?the
          ?*?processed?image
          ?
          */
          public?class?RotateImage45Degrees?extends?JFrame?{
          ??
          private?Image?inputImage;

          ??
          private?BufferedImage?sourceBI;

          ??
          private?BufferedImage?destinationBI?=?null;

          ??
          private?Insets?frameInsets;

          ??
          private?boolean?sizeSet?=?false;

          ??
          public?RotateImage45Degrees(String?imageFile)?{
          ????addNotify();
          ????frameInsets?
          =?getInsets();
          ????inputImage?
          =?Toolkit.getDefaultToolkit().getImage(imageFile);

          ????MediaTracker?mt?
          =?new?MediaTracker(this);
          ????mt.addImage(inputImage,?
          0);
          ????
          try?{
          ??????mt.waitForID(
          0);
          ????}?
          catch?(InterruptedException?ie)?{
          ????}

          ????sourceBI?
          =?new?BufferedImage(inputImage.getWidth(null),?inputImage
          ????????.getHeight(
          null),?BufferedImage.TYPE_INT_ARGB);

          ????Graphics2D?g?
          =?(Graphics2D)?sourceBI.getGraphics();
          ????g.drawImage(inputImage,?
          0,?0,?null);

          ????AffineTransform?at?
          =?new?AffineTransform();

          ????
          //?scale?image
          ????at.scale(2.0,?2.0);

          ????
          //?rotate?45?degrees?around?image?center
          ????at.rotate(45.0?*?Math.PI?/?180.0,?sourceBI.getWidth()?/?2.0,?sourceBI
          ????????.getHeight()?
          /?2.0);

          ????
          /*
          ?????*?translate?to?make?sure?the?rotation?doesn't?cut?off?any?image?data
          ?????
          */
          ????AffineTransform?translationTransform;
          ????translationTransform?
          =?findTranslation(at,?sourceBI);
          ????at.preConcatenate(translationTransform);

          ????
          //?instantiate?and?apply?affine?transformation?filter
          ????BufferedImageOp?bio;
          ????bio?
          =?new?AffineTransformOp(at,?AffineTransformOp.TYPE_BILINEAR);

          ????destinationBI?
          =?bio.filter(sourceBI,?null);

          ????
          int?frameInsetsHorizontal?=?frameInsets.right?+?frameInsets.left;
          ????
          int?frameInsetsVertical?=?frameInsets.top?+?frameInsets.bottom;
          ????setSize(destinationBI.getWidth()?
          +?frameInsetsHorizontal,?destinationBI
          ????????.getHeight()
          ????????
          +?frameInsetsVertical);
          ????show();
          ??}

          ??
          /*
          ???*?find?proper?translations?to?keep?rotated?image?correctly?displayed
          ???
          */
          ??
          private?AffineTransform?findTranslation(AffineTransform?at,?BufferedImage?bi)?{
          ????Point2D?p2din,?p2dout;

          ????p2din?
          =?new?Point2D.Double(0.0,?0.0);
          ????p2dout?
          =?at.transform(p2din,?null);
          ????
          double?ytrans?=?p2dout.getY();

          ????p2din?
          =?new?Point2D.Double(0,?bi.getHeight());
          ????p2dout?
          =?at.transform(p2din,?null);
          ????
          double?xtrans?=?p2dout.getX();

          ????AffineTransform?tat?
          =?new?AffineTransform();
          ????tat.translate(
          -xtrans,?-ytrans);
          ????
          return?tat;
          ??}

          ??
          public?void?paint(Graphics?g)?{
          ????
          if?(destinationBI?!=?null)
          ??????g.drawImage(destinationBI,?frameInsets.left,?frameInsets.top,?
          this);
          ??}

          ??
          public?static?void?main(String[]?args)?{
          ????
          new?RotateImage45Degrees("fruits.png");
          ??}

          }

          ?

          posted on 2005-09-22 07:23 Pudgy's World 閱讀(456) 評論(0)  編輯  收藏 所屬分類: Computer

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           

          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          常用鏈接

          留言簿(1)

          隨筆分類(13)

          隨筆檔案(13)

          文章分類(4)

          文章檔案(5)

          相冊

          Developer

          Favorite blogs

          搜索

          •  

          積分與排名

          • 積分 - 22527
          • 排名 - 1626

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 漯河市| 曲阜市| 赤壁市| 谢通门县| 南华县| 邢台县| 桂阳县| 富宁县| 包头市| 平湖市| 黄石市| 鱼台县| 宁国市| 浏阳市| 伊川县| 理塘县| 宁远县| 建平县| 泗阳县| 康乐县| 德保县| 恩施市| 涿州市| 通海县| 桐城市| 沭阳县| 凤山县| 古交市| 乐昌市| 松桃| 全南县| 色达县| 通道| 阿巴嘎旗| 龙川县| 淮阳县| 福泉市| 措美县| 丰宁| 若羌县| 安国市|