As the title said, java rotate image, this is my way to rotate image/2D graphic in java. Geez, I’m totally not good in writing introduction😀 . Ok then lets code, first we should make RotatePanel class which extend JPanel like this.

/**
*
* @author EtaYuy88 aka Meihta Dwiguna Saputra
*/
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;

public class RotatePanel extends JPanel {
    private Image image;
    private double currentAngle;

    public RotatePanel()
    {}

    public RotatePanel(Image image) {
        this.image = image;
        MediaTracker mt = new MediaTracker(this);
        mt.addImage(image, 0);
        try {
            mt.waitForID(0);
        }
        catch (Exception e) {
            e.printStackTrace(System.err);
        }
    }

    public void setImage(Image image)
    {
       this.image = image;
       MediaTracker mt = new MediaTracker(this);
       mt.addImage(image, 0);
       try {
           mt.waitForID(0);
       }
       catch (Exception e) {
           e.printStackTrace(System.err);
       }
    }

    public void rotate() {
        //rotate 2 degrees at a time
        currentAngle-=2.0;
        if (currentAngle >= 360.0) {
            currentAngle = 0;
        }
        repaint();
    }

    public void rotateWithParam(int rotateDegree) {
        //rotate base on rotateDegree parameter degrees at a time
        currentAngle = rotateDegree;
        if (currentAngle >= 360.0) {
            currentAngle = 0;
        }
        repaint();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D)g;
        AffineTransform origXform = g2d.getTransform();
        AffineTransform newXform = (AffineTransform)(origXform.clone());
        //center of rotation is center of the panel
        int xRot = this.getWidth()/2;
        int yRot = this.getHeight()/2;
        newXform.rotate(Math.toRadians(currentAngle), xRot, yRot);
        g2d.setTransform(newXform);
        //draw image centered in panel
        int x = (getWidth() - image.getWidth(this))/2;
        int y = (getHeight() - image.getHeight(this))/2;
        g2d.drawImage(image, x, y, this);
        g2d.setTransform(origXform);
    }

}//end of class

After that, use the custom panel (RotatePanel) inside your form like picture bellow (u can simply drag and drop it if you using NetBeans as IDE) , add a JSlider bellow our custom panel and a JLabel bellow JSlider.

Set JSlider properties and add the image inside the RotatePanel like this :

//my JSlider named jsRotate
//set current value to 0
jsRotate.setValue(0);
//set maximum value to 360
jsRotate.setMaximum(360);
//set minimum value to 0
jsRotate.setMinimum(0);

//I use mds.png that you can take in my blog and save it under the place you want to be, for example I simply place it in c:\
java.awt.Image image = Toolkit.getDefaultToolkit().getImage("c:/mds.png");
//add image to the rotatePanel
rotatePanel.setImage(image);

Finally this is how to rotate the image with JSlider as it trigger.

private void jsRotateStateChanged(javax.swing.event.ChangeEvent evt) {
jlAmount.setText(String.valueOf(jsRotate.getValue()) + "°");
rotatePanel.rotateWithParam(jsRotate.getValue());
}

And this is how it run😀

Easy isn’t? I hope my post help you😉 and for info about the newest post, don’t forget to