2013-08-23 94 views
0

我如何旋轉圖像,就像在Java中旋轉桌面時硬幣會旋轉一樣?像在Java中旋轉硬幣一樣旋轉圖像

正如this Gif。

我已經嘗試使用AffineTransform剪切。它不會給我我想要的輸出。

你可以從兩個圖像看到,剪樣通過使其更大,而不是僅僅旋轉圖像扭曲的圖像。

+1

後你已經嘗試 – prasanth

+0

我曾嘗試使用的AffineTransform 剪切這不是給我的輸出I雖然想要。 http://puu.sh/48IHR.png剪毛 http://puu.sh/48IJx.png原始 正如您從兩張圖像中看到的那樣,剪切類型會使圖像變大而不是扭曲圖像只是旋轉圖像。 – Bradsta

+0

你可以照顧發佈一些代碼嗎? –

回答

2

警告:你不是曾經打算讓你顯示同樣的結果,但你可以僞造它

基本上,這個例子簡單的尺度x軸從-1到1,然後再返回。 ..

enter image description here

import java.awt.BorderLayout; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import java.awt.geom.AffineTransform; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.imageio.ImageIO; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.Timer; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class ShearTest { 

    public static void main(String[] args) { 
     new ShearTest(); 
    } 

    public ShearTest() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(new TestPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class TestPane extends JPanel { 

     private BufferedImage img; 

     private float xScale = 1f; 
     private float xDelta = 0.05f; 

     public TestPane() { 

      try { 
       img = ImageIO.read(new File("C:\\hold\\thumbnails\\megatokyo.jpg")); 
      } catch (IOException ex) { 
       ex.printStackTrace(); 
      } 

      final Timer timer = new Timer(40, new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        xScale += xDelta; 
        if (xScale > 1) { 
         xDelta *= -1; 
        } else if (xScale < -1) { 
         xDelta *= -1; 
        } 
        repaint(); 
       } 
      }); 
      timer.start(); 
     } 

     @Override 
     public Dimension getPreferredSize() { 
      return img == null ? new Dimension(200, 200) : new Dimension(img.getWidth(), img.getHeight()); 
     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      if (img != null) { 
       Graphics2D g2d = (Graphics2D) g.create(); 

       int x = (int)((getWidth() - (img.getWidth() * xScale))/2); 
       int y = (getHeight() - img.getHeight())/2; 

       AffineTransform at = new AffineTransform(); 
       at.translate(x, y); 
       at.scale(xScale, 1); 

       g2d.setTransform(at); 
       g2d.drawImage(img, 0, 0, this); 

       g2d.dispose(); 
      } 
     } 
    } 
} 
+0

現在,** **是一個不錯的屏幕截圖,包含動畫和所有內容。這次你超過了你自己。 :) –

+1

@andrewthompson你不知道有多難創造:P - 我認爲它需要一點剪切,但可以等待 – MadProgrammer

+0

很酷的例子謝謝你! – brux