简体   繁体   English

Java旋转图像

[英]Java Rotate Image

   @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        Graphics2D g2=(Graphics2D) g.create();

        //rotation of player
         if(player.newDirection)
         {
             int rotationX=player.getImage().getWidth(null)/2;
             int rotationY=player.getImage().getHeight(null)/2;

             AffineTransform at=new AffineTransform();




                if(player.direction==LEFT)
                {
                    //graphics.rotate(Math.toRadians(90),bufferedImage.getWidth()/2,bufferedImage.getHeight()/2);
                    //graphics.drawImage(player.getImage(), player.getX(), player.getY(), null);
                    at.setToRotation(Math.toRadians(45), rotationX, rotationY);
                    g2.setTransform(at);
                    g2.drawImage(player.getImage(),player.getX(),player.getY(),null);
                    g2.dispose();
                    System.out.println("sola");
                }

I am trying to rotate the image of player but it does not work. 我正在尝试旋转播放器的图像,但是它不起作用。 Where is the problem? 问题出在哪儿?

I think I understand your problem. 我想我明白你的问题。

Rotation are made around the (0, 0) point, so if you print your image in the middle of the screen, it will display very far away from where you'd think. 旋转是围绕(0,0)点进行的,因此,如果您在屏幕中间打印图像,它将显示在与您想像的位置相距很远的地方。

In order to have things work as expected, draw your image so that the center of the image will be at (0, 0), then translate it. 为了使事情按预期工作,请绘制图像,使图像的中心位于(0,0),然后对其进行平移。

So you might have to do the transform as so: - Translate so that image center is (0, 0) - Rotate your image - Translate your image back to the required point 因此,您可能必须这样进行转换:-进行转换,以使图像中心为(0,0)-旋转图像-将图像转换回所需的点

you might consider two other things when using the affirm transform... 使用肯定转换时,您可能会考虑另外两件事...

when you code g2.setTransform(at); 当您编码g2.setTransform(at); you must be very careful, because you set the affirmTransform for your entire graphics now! 您必须非常小心,因为您现在为整个图形设置了affirmTransform! (see http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics2D.html#setTransform%28java.awt.geom.AffineTransform%29 : it says: "This method should never be used to apply a new coordinate transform on top of an existing transform..." ) so don't make any mistakes there... (请参阅http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics2D.html#setTransform%28java.awt.geom.AffineTransform%29 :它表示:“ 永远不要使用此方法在现有的变换之上应用新的坐标变换...“),所以不要在此犯任何错误...

better use g2.drawImage(Image, at, ImageObserver) as noticed on http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics2D.html 最好使用http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics2D.html上注意到的g2.drawImage(Image, at, ImageObserver)

Another common mistake is to create a 'wrong' startOf-AffirmeTransform... i suggest you try to get one by AffineTransform at = AffineTransform.getRotateInstance(theta, cx,cy); 另一个常见的错误是创建一个“错误的” startOf-AffirmeTransform ...我建议您尝试通过AffineTransform at = AffineTransform.getRotateInstance(theta, cx,cy);获取一个错误的代码AffineTransform at = AffineTransform.getRotateInstance(theta, cx,cy); and maybe concate another translate instance at.concatenate(AffineTransform.getTranslateInstance(dx, dy); or simply use at.translate(dx, dy) ... but remember: it's important in what order oyu performe transformations on your affineTransformation (its a differt if you [rotate and then translate] or [translat and then rotate] - just as a hint)... 并可能在at.concatenate at.concatenate(AffineTransform.getTranslateInstance(dx, dy);或仅使用at.translate(dx, dy)情况下连接另一个翻译实例...但请记住:这对oyu在affineTransformation上执行转换的顺序很重要(它是如果您[先旋转然后平移]或[先翻译然后旋转]则不同-只是提示)...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM