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.


  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
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package animation;

/**
 *
 * 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
        x = X0;
        y = Y0;
        aPanel = new javax.swing.JPanel();
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setPreferredSize(new java.awt.Dimension(840, 400));
        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(10);
                //build the first image
                java.awt.Graphics gr = getGraphics();
                paint(gr);
                //next, move coordinates
                //such that incoming image won't overlap
                t += 1;
                x = +(int) (V0 * t);
                y = Y0 + (int) (G * t * t / 2);//y-coordinate of bomb
            } catch (InterruptedException e) {
                break;
            } //break stops the loop
            // Now go to other frame
            other();
        }
    }// run ends

    // Your other frame comes here.
    public void other() {
        // check for the end of panel
        isDone();
        // Sleep between frames
        try {
            Thread.sleep(490);
            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 > 840) {//if reaches end of panel
            temp = true;
            t = 0; //initial t
            x = X0; //initial x
            y = Y0; //initial y
            aPanel.repaint(); //reset the panel
        }
        return temp;
    }

    //Create some graphic
    public void paint(java.awt.Graphics gr) {
        //Next, create object from sub-class!
        new airCraft().paint(gr, x, y, Y0);
        /* gr.setColor(java.awt.Color.green);
         gr.fillOval(x, Y0+10, 100, 20);//fuselage

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

         gr.setColor(java.awt.Color.gray);
         gr.fillArc(x+55, Y0+10, 40, 10, 0, 100);//cockpit

         gr.setColor(java.awt.Color.red);        
         gr.fillOval(x+30, Y0+y, 10, 5);//bomb*/
    }

    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
     *
     */
    long t = 0; //seconds

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

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

    final int V0 = 60; // pixels/second
    final double G = 2.45; //pixels/second/second
    // End of variables declaration

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

        airCraft() {//Constructor

        }

        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.setColor(java.awt.Color.red);
            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.

As you see this is not a smooth animation. To make it smooth, you need to calculate different timings such as effect of a frame on human eyes, rendering a nice artistic image with software, or use pictures instead, using Java Beans to dispatch property changes, using JavaFX and create scenes. When you become familiar with basics that I put here, you can discover further.

No comments:

Post a Comment