import java.awt.*; import javax.swing.*; import java.util.Random; public class Ex10_1 extends JPanel implements Runnable { int x, y, wid, hei; // 図形(楕円)の (x,y)座標, 幅wid, 高さhei int rr, gg, bb; // 色設定用変数 RGB Random rnd = new Random(); Thread thd = null; // スレッド public Ex10_1() { // コンストラクタ x = 10; y = 10; // 初期位置(x,y) wid = 50; hei = 50; // 初期幅 wid, 高さ hei thd = new Thread(this); thd.start(); // スレッド開始 } public void paintComponent(Graphics g) { g.setColor(new Color(rr,gg,bb)); // 画面に色指定 if (rnd.nextInt(2) == 0) { g.fillOval(x,y,wid,hei); // 楕円描画 } else { g.drawOval(x,y,wid,hei); // 楕円描画(スケルトン) } } public static void main(String[] args) { JFrame app = new JFrame(); app.add(new Ex10_1()); app.setTitle("楕円無限描画アニメ"); app.setSize(400, 400); app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); app.setVisible(true); } public void run() { while (true) { // 無限ループ x = rnd.nextInt(301); // x座標: 0..300 の乱数 y = rnd.nextInt(301); // y座標: 0..300 の乱数 wid = rnd.nextInt(51)+50; // 幅: 50..100 の乱数 hei = rnd.nextInt(51)+50; // 高: 50..100 の乱数 rr = rnd.nextInt(256); // 赤色光:0..255 の乱数 gg = rnd.nextInt(256); // 緑色光:0..255 の乱数 bb = rnd.nextInt(256); // 青色光:0..255 の乱数 repaint(); // 再描画 try { thd.sleep(250); // スレッド一時停止:停止時間=250ミリ秒 } catch(InterruptedException e) { e.printStackTrace(); // スタックトレース(スタック領域に積まれた呼出済みのメソッド一覧)を出力する } } } }