アニメーション

 


【例】直径50の円を(1,150)から(200,150)へ移動する

import java.applet.*;
import java.awt.Graphics;
public class animation1 extends Applet implements Runnable {
    int x=0;
    private Thread thre = new Thread(this);;
    public void start() {  //Runnableでは最初にこの部分が実行される
        thre.start();
    }
    public void run() {  //この部分がスレッドとして実行される
        int i;
        for ( i=1 ; i<=200 ; ++i ) {
            x = x + 1;
            repaint();
            try {
                Thread.sleep(10);  //アニメーションの実行速度を調節
            }
            catch(InterruptedException e)  {
            }
        }
    }
    public void paint(Graphics g){
        g.drawOval(x,150,50,50);
    }
}


実行例



【問題】
長さ100の垂直な直線を(1,150)から(200,150)へ移動しなさい。


実行例


【問題】
三角形を(1,150)から(200,150)へ移動しなさい。


実行例


【例】直径50の円を(1,150)から(100,150)の間で左右に往復させる

import java.applet.*;
import java.awt.Graphics;

public class animation1_3 extends Applet implements Runnable {
    int x=0;
    int direct = 1;
    private Thread thre = new Thread(this);;
    public void start() {  //Runnableでは最初にこの部分が実行される
        thre.start();
    }
    public void run() {  //この部分がスレッドとして実行される
        int i;
        for ( i=1 ; i<500 ; ++i ) {
            if(x>=100) direct = -1;
            if(x<=1) direct = 1;
            if(direct == 1) x = x + 1;
            if(direct == -1) x = x - 1;
            repaint();
            try {
                Thread.sleep(10);  //アニメーションの実行速度を調節
            }
            catch(InterruptedException e)  {
            }
        }
    }
    public void paint(Graphics g){
        g.drawOval(x,100,50,50);
    }
}


実行例


【問題】
ボタンを2つ表示(右、左)し、ボタンで円が動く方向を変えなさい


実行例


【例】ボタンを2つ表示(速く、遅く)し、ボタンで円が動く速度を変える
import java.applet.*;
import java.awt.Graphics;
import java.awt.Button;
import java.awt.event.*;

public class animation1_5 extends Applet implements Runnable, ActionListener {
    int x;
    int direct = 1;
    int speed = 10;
    private Thread thre = new Thread(this);;
    private Button b1 = new Button("速く");
    private Button b2 = new Button("遅く");
    public void start() {  //Runnableでは最初にこの部分が実行される
        add(b1);
        add(b2);
        b1.addActionListener(this);
        b2.addActionListener(this);
        thre.start();
    }
    public void run() {  //この部分がスレッドとして実行される
        int i;
        for ( i=1 ; i<1000 ; ++i ) {
            if(x>=100) direct = -1;
            if(x<=1) direct = 1;
            if(direct == 1) x = x + 1;
            if(direct == -1) x = x - 1;
            repaint();
            try {
                Thread.sleep(speed);  //アニメーションの実行速度を調節
            }
            catch(InterruptedException e)  {
            }
        }
    }
    public void actionPerformed(ActionEvent e){
        String s;
        s = e.getActionCommand();
        if (s == "速く") {speed = speed - 1;}
        if (s == "遅く") {speed = speed + 1;}
        repaint();
    }
    public void paint(Graphics g){
        g.drawOval(x,100,50,50);
    }
}


実行例



【問題】
上の例に、ボタン(中断、再開)を追加し、実行の中断と再開ができるように変更しなさい。
中断は thre.suspend() 、再開は thre.resume() を用います。
また、コンパイルの際に、-Xlint を追加します。
【例】javac animation1.java -Xlint

実行例



【問題】
左右キー(← →)で円の移動方向を変更しなさい。

実行例



【問題】
左右キー(← →)でパドル(長方形)を左右に動かしなさい。

実行例



これを応用するとこのようなプログラムが作れます。