私は四角形を描いた小さなスイングアプリケーションを開発しました.今私はThreadを使ってこの四角形をその中心で回転させたいと思っています.私が抱えている問題は、rotateSquare()メソッドでその四角形への参照を取得する方法です. (実際には、可能であれば、コンテンツペイン全体を一掃してその位置に別の回転した正方形を描画するのではなく、同じ正方形を回転させる方法が必要です)。
これが私のコードです:
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class Rotation extends JFrame implements Runnable{
Thread t;
Rotation()
{
super("Animation of rotation about center");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400,400);
setContentPane(new Container(){
public void paint(Graphics g)
{
Graphics2D g2=(Graphics2D)g.create();
g2.setBackground(Color.WHITE);
g2.clearRect(0, 0, getWidth(), getHeight());
g2.setColor(Color.GRAY);
g2.fillRect(100, 100, 100, 100);
}
});
t=new Thread(this,"pr");
t.start();
setVisible(true);
//setOpacity(0.8f);
}
public void run()
{
try{
for(;;)
{
Thread.sleep(100);
SwingUtilities.invokeLater(new Runnable(){public void run(){
rotateSquare();
}});
}
}catch(InterruptedException e){System.out.println("Thread interrupted");}
}
public void rotateSquare();
{
}
public static void main(String args[])
{
//setDefaultLookAndFeelDecorated(true);
SwingUtilities.invokeLater(new Runnable(){public void run(){new Rotation();}});
}
}