How to rotate image on it's center pivot and on the center of a jpanel ? I've tried to use this code, but the image only rotate on its center pivot on the top left of jpanel, but not on the center of the jpanel. Any help would be appreciated
public class aboutPanel extends javax.swing.JPanel {
Image img= new ImageIcon("ball.png").getImage();
Image ball[]=new Image[4];//result of cropping image
AffineTransform afft=new AffineTransform();
int degree=10;
public aboutPanel() {
initComponents();
for(int i=0;i<4;i++)//crop image to 4 ball
{
ball[i]=Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(img.getSource(),new CropImageFilter(0,i*78,80,78)));
}
if(thr.isAlive()==false)
{
thr.start();//start thread
}
}
Thread thr= new Thread(new Runnable() {
@Override
public void run() {
while(true)
{
repaint();
try {
Thread.sleep(20);
} catch (InterruptedException ex) {
Logger.getLogger(aboutPanel.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
});
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d=(Graphics2D)g;
afft.rotate(Math.toRadians(degree),ball[0].getWidth(this)/2,ball[0].getHeight(this)/2);
g2d.setTransform(afft);
g2d.drawImage(ball[0],0,0,this);
if(degree==360)
{
degree=0;
}
}