Thursday, 5 August 2010

I Needed more Software for Working with PDF Files

Perhaps you are here for PDF software:

  • Clicking here or here (This version is slower but smaller download, 1.9MB. Latest stable beta_5_4_5; with features for "page numbering" and "re-size" and "zoom", and "rotate" based on iText : rev.24/March/11. Fully deprecated, now, 04/11/2014).
  • For a more advanced "in-progress" stable version click here or here (This version is faster but larger download, 4.5MB; Latest stable beta_6_7_2; includes all features of Beta_5 versions with additional features for "Signature Manager," and "Bookmark Manager" based on iText : rev.25/March/11. No more will be revised and replaced, now, 04/11/2014)
  • Stable PDF file Splitter free software now available from Messiah's download page at, here. Stable beta_6, 1.9 MB; new build, based on iText (21/March/11).

  • Please click on the "Question Mark" at the bottom right of the software to learn how to use it.
    I was scanning my old writings to pdf files. They are my handwritten diaries, or lecture notes and some of them are typed with old type-writers. When I was arranging them, consolidating files on some supposed order, I noticed that I had forgotten to scan some pages. I could scan those individual pages to pdf and then I could split the previously scanned files put the new scanned pages between them at the right places and merge them. Instead of that I created a software to inject or rather insert the skipped pages into their places in my volumes of scanned material. You can download that software free if you, too, have that requirement. All software have a visual help here.
    (Update, November 2014 : Dysprosium (Dolce edition) has been released (please click) based on iText from mid of April 2014 and smoothly on a nightly basis added more features and now I have deprecated many scattered pieces of PDF utilities I already released. There are so many features that I cannot write complete instructions for all of them. From other previous pieces only Signature manager has been remained to be integrated. I sorted out PDF viewer problem somehow satisfactorily.)

    Sunday, 13 June 2010

    A Free Software to Merge (Attaching) PDF Files Together

    (For the latest PDF files Merger Software please have a look at the top of the left margine, inside the red box.) Frequently in academic places you might need to consolidate or technically speaking you have to concatenate two or more pdf files together. It is due to the fact that you are creating or downloading a work, such as a series of lectures during a period of time then you would like to make them in one big volume. I have created a software to do that job for me. I used iText software. I put it here for your usage. It is in RC1 phase so far. It has no risk using it. No other software is bundled with this. It does not install anything on your computer. It does not do anything with your pdf files. In the worst case scenario that never happens (since that iText is very noble a software) the concatenation might not happen. I have tested it and am using it for a long time with different files I have downloaded or scanned from the books with no problem. It has no limitation on the size and number of files or usages of software on different platforms-Windows, Unix, or Mac. It is a pity not to make it available for others. I have used iText 2.7 for implementation. I am planning to use iText 5 now with all details of open source and other fusses to become available later. Meanwhile, please enjoy these humble versions, by
    click  here   or  here (Series 5 versions are slower but smaller download, 1.9MB. Latest stable Series 5 version 5.4.5; with features for "page numbering"  and  "re-size" and "zoom", and "rotate" based on iText : rev.24/March/11; finalised and no newer version will be released. Fully deprecated, now, 04/11/2014).
    For a more advanced  stable version click  here  or   here  (Series 6 versions are faster but larger download, 4.5MB. Latest stable Series 6 version 6.7.2;  includes all features of Series 5 versions with additional features for "Signature Manager," and "Bookmark Manager" based on iText :   rev.25/March/11; actively being tested and debugged - still becomes more improved).
    For simpler  versions please go here (Latest stable Release R2 safe to use and stable, works similar to Adobe combine feature, finalised based on iText : 22/March/11; finalised and no newer version will be released. Fully deprecated, now, 04/11/2014).
    This place includes a help and Visual Help, and encountered issues page, as well.
    The first free software for Insertion pdf file 23/March/11 here.

    Stable pdf Splitter free software now available from Messiah's download page at, here. (Stable beta_6, 3.85 MB; new build, improved bookmark split based on iText, 21/March/11).
    Please click on the "Question Mark" at the bottom right of the software to learn how to use it.
    New 07/07/2014 : Modern version of Dysprosium D6.xx includes more features for Merge, Split and Inject.  
    All older versions removed.
    For Merge software:
    You should have all the pieces of pdf files in one place, perhaps in a folder of their own, somewhere next to each other. (Still, do not worry; software won't include any non PDF files around there.) When application opens, click for "brows" and take the "file-chooser" to where you have your pdf files and then select one of them and click "OK" on the "file-chooser." It pops up for a name to choose for your consolidated file. The default consolidated pdf file name I have chosen starts with "ZZ" such that later you can recognize it easily among other pdf files. Still, you can write your own choice of name in the red highlighted text field but do not add pdf extension. Software adds it automatically. After clicking "OK" on the "name-chooser" pop up, all the pdf files in the directory comes in the "List Area" of the application. You can remove those you do not need from the list and arrange files in the order that you prefer to consolidate. Then click on the "Merge" button. It brings progress GUI for you and after few seconds or few minutes, depending on the size and number of files, you have your consolidated pdf file in the same directory as the others. Next "dialogue" box offers you more concatenation or exit, whatever you choose to do.
    Please check for improvements. You might like to use "subscribe feeds."
    Please note, Pdf calculations and regenerations take resources from your computer, sometimes for more than a minute, in particular, if the size of files are greater than 50 MB. Allow enough time to the software to crank and to crunch the job. All the default created files begins with "ZZ" to make it easier to find them.
    Please check this place for newer versions and newer software for PDF.
    Update, May 2016 : Dysprosium (Dolce edition) has been released (please click) based on iText from mid of April 2014 and smoothly on a nightly basis added more features and now I have deprecated many scattered pieces of PDF utilities I already released. There are so many features that I cannot write complete instructions for all of them. From other previous pieces only Signature manager has been remained to be integrated. I sorted out PDF viewer problem somehow satisfactorily.)
    (updated : 26/May/2016) I have had over 60,000 downloads from 187 countries. All around the world, only 1 country in South America (French Guiana) and 1 country in Asia (Democratic North Korea ) have not used Dysprosium Software, yet. The remaining countries not downloaded to this date (less than 10) are from the central Africa and perhaps some very small Island countries.
    ==================
    If you are merging large files, receiving "Map Failed" exception, then please increase your java heap as explained (please click) here.

    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.

    Friday, 16 April 2010

    Using Thread in Java to Create Animation, Example 4

    In the previous example we introduced a simple graphic work. We introduced a paint() method in a cosy corner to create the image we need without interfering with other parts of application. Then we call it back and forth in our thread tick-tacks. To render that graphic to realize the graphical object on our canvas1 we need an instance of java.awt.Graphics which is always present there from deep down the system and easily we catch it from the things that native methods create for us. It is like palette of painters with color pastes on it and different brushes. Once you catch an instance of that you can use it everywhere. The best is to take it from the container that you are working on it. Here I took it from the javax swing panel that I had on my Frame. Once you put the paint() on your canvas to remove it you have to repaint() your canvas; other wise its effect remains there and in the next tick-tack you have still there. In this way you have a moving brush that creates some other type of animation if you are interested. It is like a trajectory you are watching. In this example I have created that effect. Nevertheless, I have to repaint() my panel at each renewal of the thread when the trajectory reaches to the end of canvas, somewhere in isDone() method. You can de-comment and re-comment related instructions to watch its effect. This Blog is the simplest explanation you ever could find anywhere for java threading and painting and animation. Please, have a look at Example 4.

      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
    package Forward;
    
    /**
     *
     * author Peter Jones
     */
    public class Ex4Thread extends javax.swing.JFrame implements java.lang.Runnable{
    
        /** Creates new form Ex4Thread */
        public Ex4Thread() {
            super("Example 4!"); // 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(350, 120));
            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();
            //if (this.isDone()) fThread = null;
            }
        }// run ends
    
        // Your other frame comes here.
        public void other(){
            //
            ++j;
            // Sleep between frames
            try{
                Thread.sleep (1);
                //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;
                T0=System.currentTimeMillis();
                x=X0;
                y=Y0;
                aPanel.repaint();
            }
        return temp;
        }
    
        //Create some graphic
        public void paint() {
    
            java.awt.Graphics g=aPanel.getGraphics();
            g.setColor(java.awt.Color.red);
            g.fillOval(x, Y0, 2, 2); //Object one (aircraft)
            g.fillOval(x, y, 2, 2); //Object two (released bomb)
           // g.setColor(java.awt.Color.gray);
            //g.fill3DRect(300, 80, 15, 30, true);
    
        }
    
        public static void main(String args[]) {
            new Ex4Thread().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
         *
         */
        int j=0; //counter
        final int COUNTS=3500;
    
        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=10; // pixels
    
        final int V0=25; // pixels/second
        final double G=0.98; //pixels/second/second
        // End of variables declaration
    }// End of class
    
    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.
    The first trajectory is simple and similar to previous one. It hs a constnt speed of V0=25. Second trajectory follows some rule of mathematics as y=(1/2)g.t2 It is an aircraft bomber with speed of 25 pixels per second and releases a bomb. I assumed the gravity should be one tenth of the earth gravity. It is 0.98 pixels per second per second. x-ccordinate of the bomb remains the same as x-coordiate of the bomber aircraft, that is x=V0*t,  but its y-coordinate changes according to law of gravity.
    It is important that you make a formula such that it could be cosistent with the dimensions of canvas. You also should consider for how long your frame remains in the sight. For that it is necessary, too, to know how long the effect of an exposure remains in the eye. It is 20 milli second.

    ==================================
    1. I mean canvas of painting. I do not mean Java AWT Canvas.

    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.

    Wednesday, 7 April 2010

    Using Thread in Java to Create Animation, Example 2

    We could run the thread by tick-tacking between the run() method and an auxiliary other() method, each take the thread to a certain sleep duration. You can test if you can remove the sleep from the other() method. Still you have tick tack but not smooth to create animation. The high of the clock pulse is equal to run() duration say 100 ms but the low (I measured using system time) is 3 to 4 milliseconds. you can add yet another anOther() method and a third image in your resources and get a better effect. you also can play with sleep duration to reach more smoothness in terms of visual subjective effects. you can consider the duration of effect when human eyes switches between two scenes. Now I make a more complex animation. I make these like the old neon lamps rather than using maths calculation and rendering graphics. If I use painting then my attention would be diverted to force correctness of java codes when running in hand in hand with the native operating system. That frequently needs adjustments which is not related to the job of animation. I should be confident that my animations work and encourage me to step forward rather than becoming disappointed. Then I can add other elements to them. This example put pieces of a whole picture consecutively and creates animation similar to the book-flicking through animation that children make using corner of pages of a book.

    package Forward;
    
    /**
    *
    * author Peter Jones
    */
    public class Ex2Thread extends javax.swing.JFrame implements java.lang.Runnable {
        /** Creates new form Ex2Thread */
        public Ex2Thread() {
            aPanel = new javax.swing.JPanel();
            aLabel = new javax.swing.JLabel();
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setPreferredSize(new java.awt.Dimension(50, 110));
            add(aPanel);
            aPanel.add(aLabel);
            pack();
            start();
        }
        /** Start when browser is loaded or button pushed. **/
        private 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){
                s = mString(j);
                // Sleep 100msecs between frames
                try{
                    Thread.sleep (100);
                    aLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource(s)));
                }
                catch (InterruptedException e) { }
                // Now go to other frame
                //++j;//
                other();
                isDone();
                //if (this.isDone()) fThread = null;
            }
        }// run ends
        // Your other frame comes here.
        public void other(){
            //
            ++j;
            // Sleep 100msecs between frames
            try{
                Thread.sleep (100);
            }
            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;
            }
        return temp;
        }
    
        // This method creates strings "Sun0.gif", "Sun1.gif", ..., to "Sun7.gif"
        public String mString(int n){
            String aSt = "";
            aSt = "Sun".concat(java.lang.String.valueOf(n)).concat(".gif");
        return aSt;
        }
    
        /**
         * Next main
         */
        public static void main(String args[]) {
            new Ex2Thread().setVisible(true);
        }
        // Variables declaration - do not modify
        // For simple GUI
        //
        private javax.swing.JLabel aLabel;
        private javax.swing.JPanel aPanel;
        // Variables declaration - do not modify
        // For thread animation
        int j = 0;
        String s = "";
        Thread fThread;
        final int COUNTS = 8;
        // 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.
    I took small image of "Helios" (Sun personified) in form of an icon. I used the Windows(R) Paint utility and sliced the icon in desired forms. In this way I got eight icons; from "Sun0.gif" to "Sun7.gif". I could make an array of images but I preferred to create a method mString(int n) to fetch the icons one after the other. Then I adjusted my counter from zero to seven to bring images one after another andput them on the java swing label. I could terminate the animation here and go to other jobs by calling isDone() method within the "if" statement and killing the thread; instead of that, I reset my counter back to zero to start it again. I used the isDone() method in a void type fashion. You know in java many boolens can work as void as well without their return value becomes utilized. Hence, I commented the "if" to prevent killing the thread and keep the tick-tack going on and on. If you un-comment this line and comment "j=0; statement" in the isDone() method, then the animation runs only once.
    You can also put the "++j" before call to the other() method inside the run() method and then make the call to other() method commented and see the effect. It works but not as smooth as before. Best is to give your clock one tick and one tack; one run() and one other(). Then go and adjust the desired timings inside each state of the run() and the other().

    Sunday, 4 April 2010

    Using Thread in Java to Create Animation, Example 1

    I liked to write a short guide for threads in java. It is because I could not find the existing materials much helpful. None of them dealt with this in a pedagogically clear way. They confuse the reader with materials such as "monitor" and native operating system or by mixing the thread usage with other pieces of a complicate program that deviate the attention to those pieces. Many of them want only to demonstrate an animated program is possible without explaining the thread by itself. Here I use a thread for moving from one image to another image. It is like a "Clock Pulse" or a "tick-tack" When it ticks we have one image when it tacks it shows another image. When the clock pulse is up it shows one image. When the pulse is down it shows the other image. When you use a thread in a java class you should implement java.lang.Runnable() interface. This interface contains the signature for the run() method that you write  for your class. You also need a start method start()  to initiate a thread. As a counterpart  to tick of run() method we need a reset() method for tack of our clock. We need a method that, in case, takes us out of  the tick-tack into a place that we become in control of doing other tasks. This is normally a boolean flag that tests something we predicted in a corner of the tick or in a corner of the tack. I do it simple by counting the tick-tacks and after certain number of loops I stop the animation, through that counter or through the flag. The quartet of start() -  run() - reset() - flag() is all I need. start(): starts tick-tack; run() and reset(): make the tick-tack runs and runs for ever; flag(): stops the tick-tack to go to other jobs if you like to stop it (it inhibits the clock like the hardware inhibit signal). I cannot change names of the run() and start() but I can change names of the other two. I call my reset() as other() to emphasis its job, to prevent confusing it with inhibit. I call my flag() method as isDone() in fashion of java boolean classes. I do not move anything on the screen to demonstrate animation. I only switch between two still pictures to learn how the thread is working between two states. This facilitate my future progress. I had an icon image, "Bella.gif" I used Windows 7 Microsoft Picture Manager and flipped it horizontally and saved the flipped image as "bellb.gif" Then I switched back and forth between these two images. You can see that, tick and tack are both within a never-ending while loop, as far as the thread is not killed; that is, is not null. We call our flag method isDone() to find a way, or rather to arrange, to terminate that loop, for example, if the bell has done fifty tolls. At that time we put the toll counting back to zero for the next round. Here, I embed my code,

      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
    package Forward;
    
    /**
     *
     * author Peter Jones
     */
    // I do not extend java.lang.Thread, I'd rather to implement java.lang.Runnable to be more straight forward
    public class Ex1aThread extends javax.swing.JFrame implements java.lang.Runnable{
    
        /** Creates new "form" Ex1bThread */
        public Ex1aThread() {
            aPanel = new javax.swing.JPanel();
            aLabel = new javax.swing.JLabel();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            setPreferredSize(new java.awt.Dimension(50, 110));
            add(aPanel);
            aPanel.add(aLabel);
            pack();
            // I start a thread just after packing.
            // I could start it from a button or a mouse click
            // or any other event
            start();
        }
    
    
        /** Start when browser is loaded or button pushed. **/
        public final void start() {
            // if thread is not active start it
            if (fThread == null){
                fThread = new java.lang.Thread (this);
                fThread.start();
            }
        } // start
    
        /** The thread loops to draw each frame.**/
        public void run() {
            //  Loop through animation frames
            while ( fThread != null){
                //  Sleep 100msecs between frames
                // Change this value for smoothness
                try{
                    Thread.sleep (100);
                    // I am counting number of loops
                    ++bellTolls;
                    // in each loop I "refresh" image icon of the label
                    aLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("Bella.gif")));
                }
                catch  (InterruptedException e) { }
                //  Now go to other frame
                other();
                //  I check if bell tolls fifty times I kill fThread by making it null
                if (this.isDone())  fThread = null;
            }
        }// run ends
     
        //  Your other frame comes here.
        public void other(){
            //  Sleep 100msecs between frames
            // Change this value for smoothness
            try{
                Thread.sleep (100);
                // in each loop I "refresh" image icon of the label to other image
                aLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("Bellb.gif")));
            }
            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. Bell stops after fifty tolls.
         *  The true flag kills the thread, returns the counter to zero to be ready
         */
        public boolean isDone() {
            boolean temp=false;
            //COUNTS is assumed fifty times.
            if(bellTolls==COUNTS) {
                temp=true;
                // I reset the toll counting
                bellTolls=0;
            }
            // returning true kills the fThread
            return temp;
        }
    
        //
        //  Next main
        //
        public static void main(String args[]) {
            new Ex1aThread().setVisible(true);
        }
    
        //  Variables declaration - do not modify
        //  For simple GUI
        //
        private javax.swing.JLabel aLabel;
        private javax.swing.JPanel aPanel;
    
        //  Variables declaration - do not modify
        //  For thread animation
        //
        Thread fThread;
        int bellTolls=0; // this is toll counter
        final int COUNTS=50;
        //  End of variables declaration
    }
    
    Click here to see the application (please click on Open/Run and look at top-left corner ). Ifyou save the "jar" file in your computer you can use "7z" decompression utility to extract source file and image file.
    I have created a Java Swing Ex1Thread.java class to put my animation over it. That Swing comes in the constructor "public Ex1Thread { bloh, bloh, bloh, ...}" up to the line that we see start(). start() method is invoked after packing the Swing frame by pack() method. Up to this point we do not concentrate on given codes. They are already part of our knowledge. From start() the thread task starts. By putting the start() in the constructor we have started our thread from the point that the class becomes created. (That is the reason for modifying start() as a final or as private method. Had we called it somewhere out of the constructor it simply could override the interface. Ignore these details here.) It is easy to start it from other points in the program, for instance, from action of a button. Other tutorials have done it from a button, but it is confusing since we encounter with event dispatching, then. To prove my control over the threads, in the next example I interrupt between tick-tacks with a third thread such that I have a tick-talk-tack. In this fashion I can manage as many threads in my hand. Here, I embed my code,

      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
    package Forward;
    
    /**
     *
     * author Peter Jones
     */
    public class Ex1bThread extends javax.swing.JFrame implements java.lang.Runnable{
    
        /** Creates new form Ex1bThread */
        public Ex1bThread() {
    
            aPanel = new javax.swing.JPanel();
            aLabel = new javax.swing.JLabel();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            setPreferredSize(new java.awt.Dimension(50, 110));
            add(aPanel);
            aPanel.add(aLabel);
            pack();
            start();
        }
    
        /** Start when browser is loaded or button pushed. **/
        public final void start() {
            if (fThread == null){
                fThread = new java.lang.Thread (this);
                fThread.start();
            }
        } // start
    
        /** The thread loops to draw each frame.**/
        public void run() {
            //  Loop through animation frames
            while ( fThread != null){
                //  Sleep 100msecs between frames
                // Change its value to adjust its smoothness.
                try{
                    Thread.sleep (100);
                    ++bellTolls;
                    aLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("Bella.gif")));
                }
                catch  (InterruptedException e) { }
                //  Now go to other frame
                other();
                //To appreciate that you can invoke many threads as easy as one
                //create a third thread to interrupt between tick and tack
                anOther();
                if (this.isDone())  fThread = null;
            }
        }// run ends
     
        //  Your other frame comes here.
        public void other(){
            //  Sleep 100msecs between frames
            // Change its value to adjust its smoothness.
            try{
                Thread.sleep (100);
                aLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("Bellb.gif")));
            }
            catch  (InterruptedException e) { }
    
         }
    
        //  Your yet another frame comes here.
        // It is t he third thread comes interrupting inthe middle of tick tack
        public void anOther(){
            //  Sleep 100msecs between frames
            // Change its value to adjust its smoothness.
            try{
                Thread.sleep (100);
                aLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("Sun.gif")));
            }
            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. Bell stops after fifty tolls.
         *  The true flag kills the thread, returns the counter to zero to be ready
         */
        public boolean isDone() {
            boolean temp=false;
            if(bellTolls==COUNTS) {
                temp=true;
                bellTolls=0;
            }
            return temp;
        }
        
        //
        //  Next main
        //
        public static void main(String args[]) {
            new Ex1bThread().setVisible(true);
        }
    
        //  Variables declaration - do not modify
        //  For simple GUI
        //
        private javax.swing.JLabel aLabel;
        private javax.swing.JPanel aPanel;
    
        //  Variables declaration - do not modify
        //  For thread animation
        //
        Thread fThread;
        int bellTolls=0;
        final int COUNTS=50;
        //  End of variables declaration
    
    }
    
    Click here to see the application (please click on Open/Run and look at top-left corner ). I put a Helios (personified Sun) between bell tolls. It is done by invoking the method anOther()