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

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值