【问题标题】:Java Rotate Image function [closed]Java旋转图像功能[关闭]
【发布时间】:2015-01-15 15:23:51
【问题描述】:

我怎样才能使这段代码工作?

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 或 Image 类型,但我不能真正成功。 我在这个主题上有点离题。 有什么帮助吗?

【问题讨论】:

  • 我相信你不必调用spiral.rotateImage 只需rotateImage 是你自己的方法而不是Image 方法
  • 嗨,Aleksander,欢迎来到 SO。请阅读How to Ask,否则您的问题可能会被关闭。
  • 谢谢,第一个建议解决了我的问题

标签: java rotation image-rotation


【解决方案1】:

改变

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

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

因为rotateImage 是您自己的方法而不是Image 方法

【讨论】:

    猜你喜欢
    • 2020-10-10
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 2012-08-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多