2015-01-15 92 views
-2

如何讓此代碼工作?Java旋轉圖像功能

public class Tutorial extends Applet{ 

/////////////////DRAWING IMAGES TO THE SCREEN/////////////////////// 
private Image spiral = null; 

public void paint (Graphics g){ 
    this.setSize(640, 480); 

    if (spiral == null){ 
     spiral = getImage("spiral.jpg"); 
     spiral.rotateImage(45, this); //It says that rotateImage is undefined for the Image 
    } 

    Graphics2D g2 = (Graphics2D)g; 
    g2.drawImage(spiral, 25, 50, this); 
} 

public Image getImage(String path){ 
    Image tempImage = null; 
    try 
    { 
     URL imageURL = Tutorial.class.getResource(path); 
     tempImage = Toolkit.getDefaultToolkit().getImage(imageURL); 
    } 
    catch (Exception e) 
    { 
     System.out.println("An error occured - " + e.getMessage()); 
    } 
    return tempImage; 
} 

//////////////////////////////////////////////////////////////////// 

///////////////////IMAGE ROTATION ///////////////////////////////// 

public void rotateImage(double degrees, ImageObserver o){ 
    ImageIcon icon = new ImageIcon(this.spiral); 
    BufferedImage blankCanvas = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB); 
    Graphics2D g2 = (Graphics2D)blankCanvas.getGraphics(); 
    g2.rotate(Math.toRadians(degrees), icon.getIconWidth()/2, icon.getIconHeight()/2); 
    g2.drawImage(this.spiral, 0, 0, o); 
    this.spiral = blankCanvas; 
} 
///////////////////////////////////////////////////////////////// 
} 

我知道我必須改變方法rotateImage或圖像類型,但我不能真正成功。 我在這個問題上有點偏離小船。 有什麼幫助嗎?

+1

我相信你不必調用'spiral.rotateImage'只是'rotateImage '因爲你自己的方法不是'圖像'方法 – gtgaxiola 2015-01-15 15:25:59

+5

嗨Aleksander,歡迎來到SO。請閱讀[問]因爲你的問題可能會被關閉,否則。 – 2015-01-15 15:26:02

+0

謝謝,第一個建議解決了我的問題 – 2015-01-15 15:29:40

回答

1

變化

if (spiral == null){ 
    spiral = getImage("spiral.jpg"); 
    spiral.rotateImage(45, this); 
} 

if (spiral == null){ 
    spiral = getImage("spiral.jpg"); 
    rotateImage(45, this); 
} 

因爲rotateImage是你自己的方法不是Image方法