1

My code rotates the image by 0.4 degrees every "update", the image rotates in range [-10,+10] continuously. The problem is that the buffered image gets cut off at the edges when it rotates, seems like the rotation changes the size that the bufferedImage needs but the size never updates, any ideas how i can get it to work?

    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);
}
Gilad Baruchian
  • 930
  • 3
  • 14
  • 30
  • If you're interested in the maths, you can have a look at this [example](http://stackoverflow.com/questions/20275424/rotating-image-with-affinetransform/20280225#20280225) – MadProgrammer Nov 28 '15 at 20:11

1 Answers1

2

seems like the rotation changes the size that the bufferedImage

Yes the size will change based on the angle of rotation.

You may find it easier to use the Rotated Icon. It does the rotation for you and recalculates the size (if necessary). There is no need to create new BufferedImages, you just set the degrees of rotation.

Simple example. Use the slilder bar to rotate the icon:

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);
                }
            }
        });
    }
}
camickr
  • 321,443
  • 19
  • 166
  • 288