Monday 12 April 2010

Using Thread in Java to Create Animation, Example 3

Now, we are at a point to create animation on our canvas without using an image; doing it just by painting. Still we use the tick-tack technique between run() method and the other() method. Here we have to add a paint() method to create the desired object for us, we put a small red blob, an oval shape on the canvas, moving from left to right. The canvas is a java swing panel.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package Forward;
 
/**
 *
 * author Peter Jones
 */
public class Ex3Thread extends javax.swing.JFrame implements java.lang.Runnable{
 
    /** Creates new form Ex3Thread */
    public Ex3Thread() {
        super("Example 3!"); // A title for the frame
        aPanel = new javax.swing.JPanel();
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setPreferredSize(new java.awt.Dimension(200, 100));
        add(aPanel);
        pack();
        start();
    }
 
    /** Start when browser is loaded or button pushed. **/
    public void start() {
        if (fThread == null){
            fThread = new java.lang.Thread (this);
            fThread.start();
        }
    } // start
 
    /** The thread loops to draw each frame.**/
    public void run() {
        j=0;
        // Loop through animation frames
        while ( fThread != null){
            // Sleep between frames
            try{
                Thread.sleep (100);
                x+=3;
                paint();
            }
            catch (InterruptedException e) { }
            // Now go to other frame
            other();
            isDone();
        //if (this.isDone()) fThread = null;
        }
    }// run ends
 
    // Your other frame comes here.
    public void other(){
        //
        ++j;
        // Sleep between frames
        try{
            Thread.sleep (200);
            aPanel.repaint(); // meanwhile repaint the panel.
        }
        catch (InterruptedException e) { }
    }
 
    /* Boolean allows you to terminate this thread, if you like, and go to other tasks.
     * I used it to provide a flag for counting.
     */
    public boolean isDone() {
        boolean temp=false;
        if(j==COUNTS) {
            temp=true;
            j=0;
            x=10;
        }
    return temp;
    }
     
    //Create some graphic
    public void paint() {
         
        java.awt.Graphics g=aPanel.getGraphics();
        g.setColor(java.awt.Color.red);
        g.fillOval(x, 25, 5, 5);
    }
 
    public static void main(String args[]) {
        new Ex3Thread().setVisible(true);
    }
 
/* Variables declaration - do not modify
* For simple GUI
*/
private javax.swing.JPanel aPanel;
 
/* Variables declaration - do not modify
* For thread animation
*/
int j=0; //counter
int x=10; // X-coordinate of a point
Thread fThread;
final int COUNTS=50;
// End of variables declaration
}

Click here to see the application. If you save the "jar" file in your computer you can use "7z" decompression utility to extract source file and image files.

No comments:

Post a Comment