我已經開發了一個小的Swing應用程序,其中我使用單獨的類component
將廣場添加到了我的JFrame
中。現在我想在它的中心旋轉這個Square,但是我只看到一個靜態Square,它根本不旋轉。廣場不在我的搖擺應用程序中旋轉
這是我的代碼...
public class Rotation extends JFrame {
Rotation() {
super("Animation of rotation about center");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400,400);
add(new component());
setVisible(true);
}
public static void main(String args[]){
SwingUtilities.invokeLater(new Runnable(){public void run(){new Rotation();}});
}
}
class component extends JPanel implements ActionListener {
Timer timer;
Rectangle.Double r=new Rectangle.Double(100,100,50,50);
int theta=0;
component() {
timer=new Timer(10,this);
timer.start();
}
public void actionPerformed(ActionEvent e) {
if (theta==360){
theta=0;
theta++;
}
repaint();
}
public void paint(Graphics g){
Graphics2D g2=(Graphics2D)g;
g2.setColor(Color.GRAY);
g2.rotate(theta);
g2.fill(r);
}
}
可能有人請幫我找出並解決問題。
2是** **錯了,請參閱@MikleGarin的回答 – kleopatra
雅的子彈4雖然我們應該使用的paintComponent(),但我引用w.r.t我上面的代碼,它也完美的作品的作品是好的良好 –
@Naveen不是萬能的。遲早,通過這種開發方法,您將會遇到很多性能問題。 –