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, 
00null);

    AffineTransform at 
= new AffineTransform();

    
// scale image
    at.scale(2.02.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.00.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 阅读(434) 评论(0)  编辑  收藏 所属分类: Computer

只有注册用户登录后才能发表评论。


网站导航:
 

<2024年5月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

常用链接

留言簿(1)

随笔分类(13)

随笔档案(13)

文章分类(4)

文章档案(5)

相册

Developer

Favorite blogs

搜索

  •  

积分与排名

  • 积分 - 21884
  • 排名 - 1632

最新评论

阅读排行榜

评论排行榜