开发者

How can I make a counter-clockwise rotation of the image in java?

I have an image and I want it to move in a counter-clockwise direction and that is my problem. I have a code but it seems my code doesn't work properly. You can check my code if where I've made my mistakes. Please help me regarding this matter...

This is my code:

public void move(long dt)
{

    double dt_s = dt / 1e9;
    double dx_m = speed * dt_s;
    double dy_m = speed * dt_s;

    double width = board.x1_world;
    double height = board.y1_world;

    double min_height = 0.0;
    double max_height = height;
    double min_width = 0.0;
    double max_width = width;

    x += dx_m;
    if (x >= max_width)
    {
        x = max_width;

        if (y >= min_height)
        {
            y += dy_m;
        }
    }
    if (y >= max_height)
    {
        y = max_height;
        if (x >= min_width)
        {
            dx_m *= -1;
        }
    }
    if (x <= min_width)
    {
        x = min_width;
        if (y <= max_height)
        {
            y -= dy_m;
        }
    }
    if (y <= min_height)
    {
        y = min_height;
        if (x <= max_width)
        {
            dx_m *= -1;
        }
    }


}


@Override
public void render(Graphics2D g2d)
{
    AffineTransform t = g2d.getTransform();

    double height = 0.3;//meter
    double width = 0.3;//meter

    double bird_footy = height;
    double bird_footx = width / 2;


    int xx = bo开发者_JS百科ard.convertToPixelX(x - bird_footx);
    int yy = board.convertToPixelY(y + bird_footy);

    g2d.translate(xx, yy);

    double x_expected_pixels = width * board.meter;
    double y_expected_pixels = height * board.meter;

    double x_s = x_expected_pixels / ((ToolkitImage) birdImage).getWidth();

    double y_s = y_expected_pixels / ((ToolkitImage) birdImage).getHeight();

    g2d.scale(x_s, y_s);
    g2d.drawImage(getImage(), 0, 0, this);

    g2d.setColor(Color.BLACK);

    g2d.setTransform(t);
}


there you go:

import java.io.File;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.image.AffineTransformOp;
import java.awt.geom.AffineTransform;

class RT {
    public static void main(String[] args) throws java.io.IOException {
        BufferedImage img = ImageIO.read(new File("input-image.png"));

        BufferedImage rotated = new AffineTransformOp(
                AffineTransform.getQuadrantRotateInstance(
                    3, img.getWidth() / 2, img.getHeight() / 2),
                AffineTransformOp.TYPE_BILINEAR).filter(img, null);

        ImageIO.write(rotated, "PNG", new File("output-image.png"));
    }
}
  • load your image as BufferedImage through ImageIO helper methods
  • create an AffineTransformOp object, and give it a rotate instance transformation, preferably an QuadrantRotateInstance as you're interested in rotating by 90° exactly. 3 there means 3 quadrants, so three times rotation of 90° each; that equals to one counter-clockwise rotation.
  • pass your prefered quality factor, usually chosen is BILINEAR method, which is semi-fast and semi-good-quality.
  • filter your transform; that is, apply it to the image. A filtering returns a new BufferedImage or it stores the new image in the second argument given to filter (null above - we store the image in a new BufferredImage object)
  • write or save your image to a file.

Notes:
This is a very quick example on how you can do an efficient 90° transform. It's not the only way ofcourse. Take this code and fit it to yours. Make sure to correctly handle exceptions (unlike my example).
Read more on the javadoc about each of those objects if you want to know more.


Woops

yeah, sooo, I see that - after reading the code of OP - that this is unrelated actually, cause the OP seems to want a visual rotation, not just to rotate an image and present it.
I'll leave this here, if anyone bumps into this question and is looking for this.
For the rest, ignore it please.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜