Java - 在参数后调整大小,旋转和绘制图像的方法

时间:2014-10-14 23:00:29

标签: java image rotation resize draw

我试图在java中创建一个方法,该方法将调整旋转并在给定参数后绘制图像。 它不起作用。 Idk怎么样?顺便说一句,用普通的Graphic类而不是Graphic2D可以做到这一点吗?谢谢!

    public void drawResizedAndRotatedImage(Image image, Graphics g, int x, int y, int sizeX, int sizeY, double degrees){
        //Resize
        BufferedImage im = new BufferedImage(sizeX, sizeY, BufferedImage.TYPE_4BYTE_ABGR);
        @SuppressWarnings("unused")
        Graphics g2 = (Graphics)im.getGraphics();

        //Rotate
        ImageIcon imageIcon = new ImageIcon(image);
        BufferedImage bufferedImage = new BufferedImage(imageIcon.getIconWidth(), imageIcon.getIconHeight(), BufferedImage.TYPE_4BYTE_ABGR);
        Graphics2D g2d = (Graphics2D)bufferedImage.getGraphics();
        g2d.rotate(Math.toRadians(degrees), imageIcon.getIconWidth() / 2, imageIcon.getIconHeight() / 2);

        //Draw
        g.drawImage(image, x, y, sizeX, sizeY, null);
    }

1 个答案:

答案 0 :(得分:0)

好的,所以你创建一个BufferedImage并抓住它的Graphics上下文......

BufferedImage im = new BufferedImage(sizeX, sizeY, BufferedImage.TYPE_4BYTE_ABGR);
Graphics g2 = (Graphics)im.getGraphics();

并且不做任何事......

然后,您创建第二个BufferedImage,抓住它的Graphics上下文,设置它的旋转上下文并不为它绘制任何内容......

BufferedImage bufferedImage = new BufferedImage(imageIcon.getIconWidth(), imageIcon.getIconHeight(), BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D g2d = (Graphics2D)bufferedImage.getGraphics();
g2d.rotate(Math.toRadians(degrees), imageIcon.getIconWidth() / 2, imageIcon.getIconHeight() / 2);

然后,您将原始图像绘制到提供的Graphics上下文...

g.drawImage(image, x, y, sizeX, sizeY, null);

一无所获......

我认为您需要首先阅读Transforming Shapes, Text, and Images以了解基础知识......

转换正在复合,它们也只会影响应用它们之后绘制到Graphics上下文的任何内容。

最简单的方法实际上是使用AffineTransform

double scaleWidth = (double)sizeX / (double)image.getWidth(this);
double scaleHeight = (double)sizeY / (double)image.getHeight(this);
BufferedImage img = new BufferedImage(sizeX, sizeY, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = img.createGraphics();
AffineTransform at = new AffineTransform();
at.scale(scaleWidth, scaleHeight);
at.rotate(Math.toRadians(degrees), image.getWidth(this) / 2d, image.getHeight(this) / 2d);
g2d.setTransform(at);
g2d.drawImage(image, sizeX / 2, sizeY / 2, null);
g2d.dispose();

// And finally, the result...
g.drawImage(img, x, y, null);

例如......

Pony

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestImage {

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

    public TestImage() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private BufferedImage original;

        public TestPane() {
            try {
                original = ImageIO.read(new File("Your image here"));
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

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

        public Image scaleAndRotate(Image image, int sizeX, int sizeY, double degrees) {

            double scaleWidth = (double)sizeX / (double)image.getWidth(this);
            double scaleHeight = (double)sizeY / (double)image.getHeight(this);
            BufferedImage img = new BufferedImage(sizeX, sizeY, BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2d = img.createGraphics();
            AffineTransform at = new AffineTransform();
            at.scale(scaleWidth, scaleHeight);
            at.rotate(Math.toRadians(degrees), image.getWidth(this) / 2d, image.getHeight(this) / 2d);
            g2d.setTransform(at);
            g2d.drawImage(image, sizeX / 2, sizeY / 2, null);
            g2d.dispose();

            return img;

        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            int scaleWidth = getWidth() / 2;
            int scaleHeight = getHeight() / 2;

            Image img = scaleAndRotate(original, scaleWidth, scaleHeight, 45.0);
            int x = (getWidth() - img.getWidth(this)) / 2;
            int y = (getHeight()- img.getHeight(this)) / 2;

            g2d.drawImage(img, x, y, this);
            g2d.dispose();
        }

    }

}

您可能还想阅读...

关于缩放算法和技术的讨论

您还应该知道旋转时图像的大小也会发生变化,请查看(我最喜欢的答案之一)Rotate an image in java,了解有关如何计算结果图像大小的详细信息旋转的图像...