Java 旋转图片功能
Java Rotate Image function
我怎样才能让这段代码工作?
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 或图像类型,但我无法真正成功。
我在这个问题上有点偏离主题。
有帮助吗?
改变
if (spiral == null){
spiral = getImage("spiral.jpg");
spiral.rotateImage(45, this);
}
到
if (spiral == null){
spiral = getImage("spiral.jpg");
rotateImage(45, this);
}
因为 rotateImage
是您自己的方法而不是 Image
方法
我怎样才能让这段代码工作?
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 或图像类型,但我无法真正成功。 我在这个问题上有点偏离主题。 有帮助吗?
改变
if (spiral == null){
spiral = getImage("spiral.jpg");
spiral.rotateImage(45, this);
}
到
if (spiral == null){
spiral = getImage("spiral.jpg");
rotateImage(45, this);
}
因为 rotateImage
是您自己的方法而不是 Image
方法