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 } |
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().
No comments:
Post a Comment