Java:围绕图像中心旋转图像

oxf4rvwz  于 2022-12-17  发布在  Java
关注(0)|答案(1)|浏览(197)

我是Java编程的新手,我试图使用以下代码旋转图像,但似乎没有任何效果,我在网上搜索了很多,但没有任何帮助。我看到人们用BufferedImage做它,但不想使用它。这段代码是旋转整个2d对象,而不仅仅是我想旋转的图像。我发现这是通过显示矩形,因为图像没有在彼此的顶部对齐。谢谢你的帮助。

package package3;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Rotate extends JPanel {

    public static void main(String[] args) {
        new Rotate().go();
    }
    
    public void go() {
        JFrame frame = new JFrame("Rotate");
        JButton b = new JButton("click");
        
        MyDrawPanel p = new MyDrawPanel();
        frame.add(p);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(1000, 1000);
        frame.setVisible(true);
    }
    

    class MyDrawPanel extends JPanel{
        @Override
        protected void paintComponent(Graphics g) {
            Graphics2D g2d = (Graphics2D) g; 
            Image image = new ImageIcon(
                             getClass()
                            .getResource("wheel.png"))
                            .getImage();
            g2d.drawImage(image, 0, 0, 500, 500, this);
            
            int x = image.getHeight(this);
            int y = image.getWidth(this);
            g2d.rotate(1, x/2, y/2);
            g2d.setBackground(Color.black);
            g2d.drawImage(image, 0, 0, 500, 500, this); 
            g2d.setColor(Color.BLACK);
            g2d.fillRect(0, 0, this.getWidth(), this.getHeight());
        }
    }
}

here's what output looks like

rfbsl7qr

rfbsl7qr1#

首先,当使用Graphics上下文旋转图像时,旋转将发生在“锚”点(如果我记得的话,顶部/左侧是默认位置)。
因此,为了围绕图像的中心旋转图像,您需要将锚点设置为图像的中心(在其容器的上下文内)。
这意味着rotate调用应该类似于...
旋转(弧度,x偏移+(图像.获取宽度()/ 2),y偏移+(图像.获取高度()/ 2));
然后,当您在xOffset/yOffset处绘制图像时,图像将围绕锚点(或图像中心)旋转。
第二,变换是复合的。也就是说,当你变换一个图形上下文时,所有后续的绘制操作都将被变换。如果你再次变换它,新的变换将被添加到旧的变换中(所以如果你旋转45度,然后再次旋转45度,变换现在将是90度)。
通常最简单的方法是先复制Graphics状态,应用变换和绘制操作,然后复制dispose,这将使原始上下文保持其原始(变换)状态(应用所有绘制操作),这样您就不必花时间试图找出如何消除混乱

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main {
    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    JFrame frame = new JFrame();

                    frame.add(new MyDrawPanel());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

    class MyDrawPanel extends JPanel {

        private BufferedImage image;

        public MyDrawPanel() throws IOException {
            image = ImageIO.read(getClass().getResource("/images/MegaTokyo.png"));
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(1000, 1000);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (image == null) {
                return;
            }
            Graphics2D g2d = (Graphics2D) g.create();
            drawImageTopLeft(g2d);
            drawImageBottomRight(g2d);
            drawImageMiddle(g2d);

            g2d.rotate(Math.toRadians(45), getWidth() / 2, getHeight() / 2);
            g2d.setColor(Color.BLACK);
            g2d.drawRect(0, 0, this.getWidth(), this.getHeight());
            g2d.dispose();
        }

        protected void drawImageTopLeft(Graphics2D g2d) {
            g2d = (Graphics2D) g2d.create();
            int x = 0;
            int y = 0;

            g2d.rotate(Math.toRadians(135), image.getWidth() / 2, image.getHeight() / 2);
            g2d.drawImage(image, x, y, this);
            g2d.dispose();
        }

        protected void drawImageBottomRight(Graphics2D g2d) {
            g2d = (Graphics2D) g2d.create();
            int x = (getWidth() - image.getWidth());
            int y = (getHeight() - image.getHeight());

            g2d.rotate(Math.toRadians(-45), getWidth() - (image.getWidth() / 2), getHeight() - (image.getHeight() / 2));
            g2d.drawImage(image, x, y, this);
            g2d.dispose();
        }

        protected void drawImageMiddle(Graphics2D g2d) {
            g2d = (Graphics2D) g2d.create();
            int x = (getWidth() - image.getWidth()) / 2;
            int y = (getHeight() - image.getHeight()) / 2;

            g2d.rotate(Math.toRadians(45), getWidth() / 2, getHeight() / 2);
            g2d.drawImage(image, x, y, this);
            g2d.dispose();
        }
    }
}

相关问题