Friday, 30 April 2010

Using Thread in Java to Create Animation, Example 5

I said we use the paint() method to have a corner to creat my objects. Further I can have a class for my objects and create images there and bring them on my frame. I need only to pass the graphic of canvas to that class. Then I can even create more than one similar objects on the canvas and put them next together. I can have them resized and rotated and deformed. Now, I use some simple drawings and my aircraft bomber looks more real. I create it in a small sub-class to my canvas. Then I can call it from my paint() method. From this point you can use formalities of object-oriented languages for data encapsulations. But I prefer to experience with animations and put details of software engineering aside until I become fully fluent and in command of more basic details.

package Forward;

/**
 *
 * author Peter Jones
 */
public class Ex5Thread extends javax.swing.JFrame implements java.lang.Runnable{

    /** Creates new form Ex5Thread */
    public Ex5Thread() {
        super("Example 5!"); // A title for the frame
        T0=System.currentTimeMillis();
        x=X0;
        y=Y0;
        aPanel = new javax.swing.JPanel();
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setPreferredSize(new java.awt.Dimension(300, 150));
        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 (1);
                t=System.currentTimeMillis()-T0;
                t=t/1000; //convert to seconds
                x=(int)(V0*t);
                y=Y0+(int)(G*t*t/2);
                paint();
            }
            catch (InterruptedException e) {break; } //break stops the loop
            // Now go to other frame
            other();
            isDone();
        }
    }// run ends

    // Your other frame comes here.
    public void other(){
        //
        // Sleep between frames
        try{
            Thread.sleep (25);
            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(x==300) {//if reaches end of panel
            temp=true;
            T0=System.currentTimeMillis();//reset time origin
            x=X0; //initial x
            y=Y0; //initial x
            aPanel.repaint(); //reset the panel
        }
    return temp;
    }

    //Create some graphic
    public void paint() {
        java.awt.Graphics g=aPanel.getGraphics();
//        new airCraft().paint(g,x, y, Y0);
    }

    public static void main(String args[]) {
        new Ex5Thread().setVisible(true);
    }

    /* Variables declaration - do not modify
     * For simple GUI
     * For thread animation
     */
    private javax.swing.JPanel aPanel;
    private Thread fThread;

    // Variables declaration - do not modify

    double t=0; //seconds
    double T0=0; //seconds

    int x=0; // X-coordinate of a point
    final int X0=10; // pixels

    int y=0; // Y-coordinate of a point
    final int Y0=30; // pixels

    final int V0=25; // pixels/second
    final double G=0.98; //pixels/second/second
    // End of variables declaration

//sub-class creates some graphical object
class airCraft{

    airCraft(){//Costructor

    }

    protected void paint(java.awt.Graphics gr, int x, int y, int Y){
 
        gr.setColor(java.awt.Color.green);
        gr.fillOval(x, Y+10, 100, 20);//fuselage

        gr.setColor(java.awt.Color.red);
        gr.fillArc(x-20, Y+5, 40, 20, 0, 100);//tail
        gr.fillArc(x+10, Y+3, 60, 15, 0, 135);//far wing
        gr.fillArc(x+35, Y+10, 35, 20, 0, -135);//front wing

        gr.setColor(java.awt.Color.gray);
        gr.fillArc(x+55, Y+10, 40, 10, 0, 100);//cockpit
        
        gr.fillOval(x+30, Y+y, 10, 5);//bomb

   }
}

}
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.

0 comments:

Post a Comment