我只是想知道如何使用 JLabel 组件的 paintComponent() 方法旋转矩形图像并正确设置其新宽度和高度?
我试图旋转(见附图)并且图像比例变大但JLabel比例保持不变是什么使图像超出JLabel边界或其他东西:S所以我的问题是如何将图像新的宽度和高度设置为动态组件更优化的方式?
我只是想知道如何使用 JLabel 组件的 paintComponent() 方法旋转矩形图像并正确设置其新宽度和高度?
我试图旋转(见附图)并且图像比例变大但JLabel比例保持不变是什么使图像超出JLabel边界或其他东西:S所以我的问题是如何将图像新的宽度和高度设置为动态组件更优化的方式?
+1 对 MadProgrammers 的评论和链接。
使用链接中的方法(稍微编辑以省略使用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, 0, 0, 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, 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, 0, 0, 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, 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, 0, 0, null);
g2d.dispose();
return rotatedBI;
}
}
参考:
好的,所以您希望 JLabel 保持其尺寸并改为调整图像大小。
无论如何,您可能已经在使用仿射变换,因此您可以使用一些三角函数和最小值/最大值来找到旋转图像的 AABB。适当缩放(我假设您已经在使用仿射变换进行旋转)
编辑:
AABB = Axis Aligned Bounding Box,它的边对应于旋转图像的最小/最大 x,y 坐标。只是一种更简洁的表达方式。
好的,我尝试了David Kroukamp给我看的样本。谢谢,大卫,你为我指明了正确的方向。我认为这是一个非常好的片段。
public static BufferedImage rotateImage(Image image, int angle)
{
double sin = Math.abs(Math.sin(angle));
double cos = Math.abs(Math.cos(angle));
int originalWidth = image.getWidth(null);
int originalHeight = image.getHeight(null);
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, 0, 0, null);
g2d.dispose();
return rotatedBI;
}
我测试了片段,结果图像+它的组件的比例确实是动态变化的。 但是我得到的结果组件宽度总是比它的内部图像宽一点:S
例如,这是我旋转 45 度角的图像版本
...我发现(在蓝色背景上)它的宽度并不完全适合围绕白色图像...实际上我正在寻找某种标准的 Java 图像处理旋转解决方案,但我找不到它 :( 所以我必须深入挖掘问题,至少弄清楚如何解决“更广泛的效果”并避免每次重新绘制时创建新的 BufferedImage 对象......
好的,作为我研究的一部分,我尝试编写某种我的旋转代码改编。这里是 :
>经过测试
public static void rotateImage(Graphics g, Image image,int tilt,JComponent component)
{
// create the transform, note that the transformations happen
// in reversed order (so check them backwards)
AffineTransform at = new AffineTransform();
//5. modify component scale ...
double sin = Math.abs(Math.sin(Math.toRadians(tilt)));
double cos = Math.abs(Math.cos(Math.toRadians(tilt)));
int w=image.getWidth(null);
int h=image.getHeight(null);
int newW=(int) Math.floor(w * cos + h * sin);
int newH=(int) Math.floor(h * cos + w * sin);
component.setSize(newW, newH);
int width=component.getWidth();
int height=component.getHeight();
// 4. translate it to the center of the component
at.translate(width / 2, height / 2);
// 3. do the actual rotation
at.rotate(Math.toRadians(tilt));
// 2. just a scale because this image is big
// at.scale(1, 1);
// 1. translate the object so that you rotate it around the
// center (easier :))
at.translate(-image.getWidth(null)/2, -image.getHeight(null)/2);
// draw the image
((Graphics2D) g).drawImage(image, at, null);
}
...所以旋转 -30 度倾斜的结果图像看起来像这样
我非常希望小费可以节省一天:)
谢谢大家的帮助