问题描述:

So I have an animation class, and I'd like to include the ability to rotate the animation. Not every animation I draw needs to rotate, so if I could make it an optional parameter that would be great. At the moment, I'll settle for just drawing a rotated frame.

Sorry if this question is ugly, I'm new to both Java and this forum, but I would really appreciate any advice on the matter. Java seems to take a much less direct route when performing simple tasks like this compared to C#, but I'm sure thats for good reason.

Any advice on either this piece of code or writing games in java in general would be taken to heart, thanks for your time!

public class Animation {

public BufferedImage animImage;

private int frameWidth;

private int frameHeight;

private int numberOfFrames;

private long frameTime;

private long startingFrameTime;

private long timeForNextFrame;

public int currentFrameNumber;

private boolean loop;

public int x;

public int y;

private int startingXOfFrameInImage;

private int endingXOfFrameInImage;

public boolean active;

private long showDelay;

private long timeOfAnimationCreation;

public int segmentStart;

public int segmentEnd;

public Animation(BufferedImage animImage, int frameWidth, int frameHeight, int numberOfFrames, long frameTime, boolean loop, int x, int y, long showDelay, int startFrame, int endFrame)

{

this.animImage = animImage;

this.frameWidth = frameWidth;

this.frameHeight = frameHeight;

this.numberOfFrames = numberOfFrames;

this.frameTime = frameTime;

this.loop = loop;

this.segmentStart = startFrame;

this.segmentEnd = endFrame;

this.x = x;

this.y = y;

this.showDelay = showDelay;

timeOfAnimationCreation = System.currentTimeMillis();

startingXOfFrameInImage = 0;

endingXOfFrameInImage = frameWidth;

startingFrameTime = System.currentTimeMillis() + showDelay;

timeForNextFrame = startingFrameTime + this.frameTime;

currentFrameNumber = segmentStart;

active = true;

}

public void Update(){

if(timeForNextFrame <= System.currentTimeMillis()){

currentFrameNumber++;

if(currentFrameNumber < segmentStart){

currentFrameNumber = segmentStart;

}

if(currentFrameNumber > segmentEnd){

if(!loop){

active = false;

currentFrameNumber = segmentEnd;

}

else{

currentFrameNumber = segmentStart;

}

}

startingXOfFrameInImage = currentFrameNumber * frameWidth;

endingXOfFrameInImage = startingXOfFrameInImage + frameWidth;

startingFrameTime = System.currentTimeMillis();

timeForNextFrame = startingFrameTime + frameTime;

}

}

public void Draw(Graphics2D g2d){

// Checks if show delay is over.

if(this.timeOfAnimationCreation + this.showDelay <= System.currentTimeMillis())

g2d.drawImage(animImage, x, y, x + frameWidth, y + frameHeight, startingXOfFrameInImage, 0, endingXOfFrameInImage, frameHeight, null);

}

网友答案:

You can just create a method that takes the angle of the image that you are wanting to rotate it by, and then call the rotate method that comes with the Graphics class

 public void rotateImage(Graphics2D g2, BufferedImage image) 
 {
     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);
 }

code inside the method came from this location: rotateImage

hope this helps

相关阅读:
Top