在overriden paintComponent(...)方法中旋转图像 [英] Rotate image in overriden paintComponent(...) method
问题描述
我试图旋转(看到附加的图像)和图像比例变得更大,但JLabel比例保持不变,使图像超出JLabel界限或什么:S所以我的问题是如何以更优化的方式动态地将图像新的宽度和高度设置为组件?
好的,我试了一下 David Kroukamp 给我看的示例。谢谢,大卫,你指出我正确的方向。我认为这是一个非常好的代码片,可以重新使用。
public static BufferedImage rotateImage(Image image,int angle)
{
double sin = Math.abs (Math.sin(角度));
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 rotateBI = 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();
返回rotateBI;
}
我测试了片段,结果图片及其组件的比例动态变化。
但是我得到的结果组件宽度一直比它的内部图像宽一点点:S
示例这里是我的版本的图像旋转45度角
...正如我所能找到的(在蓝色背景上)它的宽度并不完全适合环绕白色图像......其实我正在寻找某种标准Java图像处理旋转解决方案,但我找不到它:(所以我不得不深入研究这个问题,至少弄清楚如何解决'更广泛的效果和避免每次重新绘制新的BufferedImage对象 ...
好的,作为我研究的成果,我试着编写一些我的旋转代码适应。这里是:
>测试
public static void rotateImage(Graphics g,Image image,int tilt,JC元组件)
{
//创建转换,请注意转换发生
//以相反的顺序(所以向后检查)
AffineTransform at = new AffineTransform();
// 5。修改组件的比例...
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.将它翻译到组件的中心
at.translate(width / 2,height / 2);
// 3.做实际的旋转
at.rotate(Math.toRadians(tilt));
// 2.只是一个比例尺,因为这个图像很大
// at.scale(1,1);
// 1.翻译对象,以便围绕
中心旋转它(更简单:))
at.translate(-image.getWidth (null)/ 2,-image.getHeight(null)/ 2);
//绘制图片
((Graphics2D)g).drawImage(image,at,null);
$ b ...所以结果图像旋转在-30度倾斜看起来像这样
我非常希望小费能节省一天的费用:)
$ hr
谢谢大家的帮助
I am just wondering how to rotate a rectangle image with paintComponent() method of JLabel component and set its new width and height correctly?
I tried to rotate (see attached image) and the image scale becomes bigger but the JLabel scale keeps the same what makes image be out of JLabel bounds or something :S So my question is how to set image new width and height to component dynamically in a more optimal way?
解决方案 OK I tried the sample David Kroukamp showed me. Thanks, David, you pointed me to right direction. I think it is a really good snippet to lean back on.
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;
}
David Kroukamp's result image preview
I tested the snippet and the result image + its component's scale were really dynamic changed.
But the result component width I've got has been always a little bit wider than it's inner image was :S
For example here is my version of an image rotated in 45 degrees angle
... As I could find (on blue background) its width is not totally fit to surround white image ... Actually I was looking for some kind of standard Java Image Processing rotate solution but I couldn't find it :( so I had to dig deeper to the problem and at least figure out how to solve the 'wider effect' and avoid create new BufferedImage object every re-paint...
OK, as a reault of my research, I tried to write some kind of my rotate code adaptation. Here it is :
>tested
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);
}
... so the result image rotated on -30 degrees tilt looks like this
I dearly hope the tip saves ones day :)
Thank you all for help
这篇关于在overriden paintComponent(...)方法中旋转图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!