import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.awt.Image; public class Hina13 extends JFrame { public Hina13() { setSize(500, 500); //最初のフレームサイズ setTitle("Java Example"); //タイトル setDefaultCloseOperation(EXIT_ON_CLOSE); //コンテナを取得してパネルを貼る MyJPanel myJPanel = new MyJPanel(); Container c = getContentPane(); //コンテナの取得 c.add(myJPanel); //パネルを貼る setVisible(true); } /***************** main ****************************/ public static void main(String[] args) { new Hina13(); } /******************************************************** MyJPanel:パネル *********************************************************/ public class MyJPanel extends JPanel implements ActionListener { Timer timer; int x=0; /************ コンストラクタ ****************/ public MyJPanel() { timer = new Timer(100,this); timer.start(); } /*************** paintComponent *******************/ public void paintComponent(Graphics g) { super.paintComponent(g); //画面クリアする。 g.fillOval(x,250,10,10); } /************ タイムアップしたら ***********************/ public void actionPerformed(ActionEvent e) { repaint(); x = x + 2; if (x>500) timer.stop(); } } }