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 閱讀(455) 評論(0)  編輯  收藏 所屬分類: Computer

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


          網站導航:
           

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

          常用鏈接

          留言簿(1)

          隨筆分類(13)

          隨筆檔案(13)

          文章分類(4)

          文章檔案(5)

          相冊

          Developer

          Favorite blogs

          搜索

          •  

          積分與排名

          • 積分 - 22516
          • 排名 - 1626

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 宣恩县| 金秀| 丰都县| 江阴市| 嘉峪关市| 吕梁市| 台前县| 宜章县| 邢台县| 蓬溪县| 体育| 天峨县| 河津市| 阳信县| 南澳县| 镇远县| 墨竹工卡县| 淮北市| 农安县| 定襄县| 灌云县| 灯塔市| 调兵山市| 阜新市| 桃园市| 吉隆县| 若尔盖县| 卫辉市| 曲麻莱县| 碌曲县| 睢宁县| 高雄市| 衡阳县| 萍乡市| 普定县| 乌海市| 勃利县| 新密市| 宜章县| 曲水县| 哈巴河县|