2010-04-22 61 views
3

我可以旋轉已添加到JLabel的圖像。唯一的問題是,如果高度和寬度不相等,旋轉的圖像將不再出現在JLabel的原點(0,0)處。如何使用Java/Swing旋轉圖像,然後將其原點設置爲0,0?

這是我正在做的。我也嘗試使用AffineTransform並旋轉圖像本身,但結果相同。

Graphics2D g2d = (Graphics2D)g; 
g2d.rotate(Math.toRadians(90), image.getWidth()/2, image.getHeight()/2); 
super.paintComponent(g2d); 

如果我有一個圖像,其寬度大於其高度,用這個方法旋轉的圖像,然後畫它會導致在圖像中垂直地畫的點0,0的上方,且水平向右點0,0。

回答

4

使用g2d.transform()將圖像移回需要的位置。我只是想象一下計算可能是,但我認爲是這樣的:

int diff = (image.getWidth() - image.getHeight())/2; 
g2.transform(-diff, diff); 

順便說一句,你可能有標籤報告其首選大小的一個問題 - 你可能需要重寫的getPreferredSize()和帳戶用於交換圖像的寬度和高度如果旋轉。

3
AffineTransform trans = new AffineTransform(0.0, 1.0, -1.0, 0.0, image.getHeight(), 0.0); 
g2d.transform(trans); 
g2d.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null); 
2

下面的函數將旋轉一個緩衝圖像,如果它是一個完美的正方形,它將不確定。

public BufferedImage rotate(BufferedImage image) 
{ 
    /* 
    * Affline transform only works with perfect squares. The following 
    * code is used to take any rectangle image and rotate it correctly. 
    * To do this it chooses a center point that is half the greater 
    * length and tricks the library to think the image is a perfect 
    * square, then it does the rotation and tells the library where 
    * to find the correct top left point. The special cases in each 
    * orientation happen when the extra image that doesn't exist is 
    * either on the left or on top of the image being rotated. In 
    * both cases the point is adjusted by the difference in the 
    * longer side and the shorter side to get the point at the 
    * correct top left corner of the image. NOTE: the x and y 
    * axes also rotate with the image so where width > height 
    * the adjustments always happen on the y axis and where 
    * the height > width the adjustments happen on the x axis. 
    * 
    */ 
    AffineTransform xform = new AffineTransform(); 

    if (image.getWidth() > image.getHeight()) 
    { 
    xform.setToTranslation(0.5 * image.getWidth(), 0.5 * image.getWidth()); 
    xform.rotate(_theta); 

    int diff = image.getWidth() - image.getHeight(); 

    switch (_thetaInDegrees) 
    { 
    case 90: 
     xform.translate(-0.5 * image.getWidth(), -0.5 * image.getWidth() + diff); 
     break; 
    case 180: 
     xform.translate(-0.5 * image.getWidth(), -0.5 * image.getWidth() + diff); 
     break; 
    default: 
     xform.translate(-0.5 * image.getWidth(), -0.5 * image.getWidth()); 
     break; 
    } 
    } 
    else if (image.getHeight() > image.getWidth()) 
    { 
    xform.setToTranslation(0.5 * image.getHeight(), 0.5 * image.getHeight()); 
    xform.rotate(_theta); 

    int diff = image.getHeight() - image.getWidth(); 

    switch (_thetaInDegrees) 
    { 
    case 180: 
     xform.translate(-0.5 * image.getHeight() + diff, -0.5 * image.getHeight()); 
     break; 
    case 270: 
     xform.translate(-0.5 * image.getHeight() + diff, -0.5 * image.getHeight()); 
     break; 
    default: 
     xform.translate(-0.5 * image.getHeight(), -0.5 * image.getHeight()); 
     break; 
    } 
    } 
    else 
    { 
    xform.setToTranslation(0.5 * image.getWidth(), 0.5 * image.getHeight()); 
    xform.rotate(_theta); 
    xform.translate(-0.5 * image.getHeight(), -0.5 * image.getWidth()); 
    } 

    AffineTransformOp op = new AffineTransformOp(xform, AffineTransformOp.TYPE_BILINEAR); 

    BufferedImage newImage =new BufferedImage(image.getHeight(), image.getWidth(), image.getType()); 
    return op.filter(image, newImage); 
} 
+0

奇妙的代碼!感謝您成爲唯一一家嚴格迴應圖片旋轉的答案,而不是將其鎖定到Graphics2D。 – AnthonyW 2014-12-03 13:44:29

相關問題