gpt4 book ai didi

java - BufferedImage 旋转后被切断

转载 作者:行者123 更新时间:2023-11-30 03:09:15 24 4
gpt4 key购买 nike

我的代码每次“更新”都会将图像旋转0.4度,图像在[-10,+10]范围内连续旋转。问题是缓冲图像在旋转时在边缘被切断,似乎旋转改变了 bufferedImage 所需的大小,但大小永远不会更新,我有什么想法可以让它工作吗?

    protected double rotation = 0;
private double rotDir = 0.4;

private BufferedImage getRotatedSprite(){
if(Math.abs(rotation)>10)
rotDir=rotDir*(-1);
rotation+=rotDir;
ImageIcon icon = new ImageIcon(bImage);
BufferedImage bImg = new BufferedImage(icon.getIconWidth(),
icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d= (Graphics2D)bImg.getGraphics();
g2d.rotate(Math.toRadians(rotation), icon.getIconWidth()/2, icon.getIconHeight()/2);
g2d.drawImage(bImage, 0, 0, null);
return bImg;
}
public void drawSprite(Graphics g) {
BufferedImage image = getRotatedSprite();

if(customWidth != -1 && customHeight != -1){
g.drawImage(image, (int)locX, (int)locY, customWidth, customHeight, this);
}else
g.drawImage(image, (int)locX, (int)locY, this);
}

最佳答案

seems like the rotation changes the size that the bufferedImage

是的,尺寸会根据旋转角度而变化。

您可能会发现使用 Rotated Icon 更容易。它会为您进行旋转并重新计算尺寸(如有必要)。无需创建新的 BufferedImage,只需设置旋转角度即可。

简单的例子。使用 slider 旋转图标:

import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.*;

public class Rotation3 extends JPanel
{
private Icon icon;
private RotatedIcon rotated;
private int degrees;

public Rotation3(Image image)
{
icon = new ImageIcon( image );
rotated = new RotatedIcon(icon, 0);
// rotated.setCircularIcon( true );
setDegrees( 0 );
setPreferredSize( new Dimension(600, 600) );
}

@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);

// translate x/y so Icon rotated around a specific point (300, 300)

int x = 300 - (rotated.getIconWidth() / 2);
int y = 300 - (rotated.getIconHeight() / 2);
rotated.paintIcon(this, g, x, y);

g.setColor(Color.RED);
g.fillOval(295, 295, 10, 10);
}

public void setDegrees(int degrees)
{
this.degrees = degrees;
rotated.setDegrees(degrees);
repaint();
}

public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
// String path = "dukewavered.gif";
String path = "lunch75.jpg";
BufferedImage bi = ImageIO.read(Rotation3.class.getResourceAsStream(path));
final Rotation3 r = new Rotation3(bi);

final JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 360, 0);
slider.addChangeListener(new ChangeListener()
{
public void stateChanged(ChangeEvent e)
{
int value = slider.getValue();
r.setDegrees( value );
}
});

JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new JScrollPane(r));
f.add(slider, BorderLayout.SOUTH);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
catch(IOException e)
{
System.out.println(e);
}
}
});
}
}

关于java - BufferedImage 旋转后被切断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33974090/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com