[英]Rotating image on the center of jpanel
如何在中心軸和jpanel中心旋轉圖像? 我嘗試使用此代碼,但是圖像僅在jpanel左上方的中心軸上旋轉,而不在jpanel的中心上旋轉。 任何幫助,將不勝感激
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;
}
}
g2d.drawImage(ball[0],0,0,this);
您正在位置(0,0)繪制圖像;
如果要在中心,則需要計算中心。 例如,寬度中心將類似於:
int widthCenter = (getWidth() - ball[0].getWidth(this)) / 2;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.