加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 在overriden paintComponent(…)方法中旋转图像

发布时间:2020-12-15 08:49:36 所属栏目:Java 来源:网络整理
导读:我只是想知道如何使用JLabel组件的paintComponent()方法旋转矩形图像并正确设置其新的宽度和高度? 我试图旋转(见附图)并且图像比例变得更大但是JLabel比例保持相同的原因使图像超出JLabel界限或者S:所以我的问题是如何动态地将图像新的宽度和高度设置为组
我只是想知道如何使用JLabel组件的paintComponent()方法旋转矩形图像并正确设置其新的宽度和高度?

我试图旋转(见附图)并且图像比例变得更大但是JLabel比例保持相同的原因使图像超出JLabel界限或者S:所以我的问题是如何动态地将图像新的宽度和高度设置为组件更优化的方式?

解决方法

1给MadProgrammers评论和链接.

使用link中的方法(略微编辑以省略GraphicsConfiguration的使用,drawRenderImage(..)和更改的变量名称):

//https://stackoverflow.com/questions/4156518/rotate-an-image-in-java
public static BufferedImage createTransformedImage(BufferedImage image,double angle) {
    double sin = Math.abs(Math.sin(angle));
    double cos = Math.abs(Math.cos(angle));
    int originalWidth = image.getWidth();
    int originalHeight = image.getHeight();
    int newWidth = (int) Math.floor(originalWidth * cos + originalHeight * sin);
    int newHeight = (int) Math.floor(originalHeight * cos + originalWidth * sin);
    BufferedImage rotatedBI = new BufferedImage(newWidth,newHeight,BufferedImage.TRANSLUCENT);
    Graphics2D g2d = rotatedBI.createGraphics();
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.translate((newWidth - originalWidth) / 2,(newHeight - originalHeight) / 2);
    g2d.rotate(angle,originalWidth / 2,originalHeight / 2);
    g2d.drawImage(image,null);
    g2d.dispose();
    return rotatedBI;
}

这是一个例子:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class RotateImage {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new MyRotatableImage(createImage(),-45));
                frame.pack();
                frame.setVisible(true);
            }
        });
    }

    public static BufferedImage createImage() {
        BufferedImage img = new BufferedImage(100,50,BufferedImage.TRANSLUCENT);
        Graphics2D g2d = img.createGraphics();
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setColor(Color.WHITE);
        g2d.fillRect(0,img.getWidth(),img.getHeight());
        g2d.setColor(Color.BLACK);
        g2d.setFont(new Font("Calibri",Font.BOLD,20));
        FontMetrics fm = g2d.getFontMetrics();
        String text = "Hello world";
        int textWidth = fm.stringWidth(text);
        g2d.drawString(text,(img.getWidth() / 2) - textWidth / 2,img.getHeight() / 2);
        g2d.dispose();
        return img;
    }
}

class MyRotatableImage extends JPanel {

    private BufferedImage transformedImage;

    public MyRotatableImage(BufferedImage img,int angle) {
        transformedImage = createTransformedImage(img,angle);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.drawImage(transformedImage,null);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(transformedImage.getWidth(),transformedImage.getHeight());
    }

    //https://stackoverflow.com/questions/4156518/rotate-an-image-in-java
    public static BufferedImage createTransformedImage(BufferedImage image,double angle) {
        double sin = Math.abs(Math.sin(angle));
        double cos = Math.abs(Math.cos(angle));
        int originalWidth = image.getWidth();
        int originalHeight = image.getHeight();
        int newWidth = (int) Math.floor(originalWidth * cos + originalHeight * sin);
        int newHeight = (int) Math.floor(originalHeight * cos + originalWidth * sin);
        BufferedImage rotatedBI = new BufferedImage(newWidth,BufferedImage.TRANSLUCENT);
        Graphics2D g2d = rotatedBI.createGraphics();
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.translate((newWidth - originalWidth) / 2,(newHeight - originalHeight) / 2);
        g2d.rotate(angle,originalHeight / 2);
        g2d.drawImage(image,null);
        g2d.dispose();
        return rotatedBI;
    }
}

参考:

> Rotate an image in java

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读