Tuesday, March 8, 2011

Best Bang For The Buck Camcorder

custom icon in the Animation Window Title

Animation Window Icon

We have already seen how we could animate the title of a window, now we'll see how we can make our window icon , also has movement.

icon change need not always, when the program can encourage us something or going to notify every few minutes, depending on each application that we will give.

Images




Code

Master Class

Classes package;

import javax.swing.JFrame;

public class Principal extends JFrame{
moverIcono mi;
public Principal(){
mi=new moverIcono(this);
mi.start();
}
public static void main(String arg[]){
Home p = new Principal ();
p.setBounds (0, 0, 300, 200);
p.setLocationRelativeTo (null);
p.setVisible (true);
}

}

Class moverIcono

package
Classes ;

import java.util.ArrayList;

import javax.swing.ImageIcon;

public class moverIcono extends Thread {
ArrayList < ImageIcon> iconos;
Principal p;
int indice=9;
public moverIcono(Principal prin){
p=prin;
iconos=new ArrayList ();
for(int i=0;i < 10;i++){
iconos.add(new ImageIcon(this.getClass().getResource("../lib/imagennumero"+i+".jpg")));
}
}
public void run(){
while(true){
p.setIconImage (iconos.get (index). GetImage ());
if (index> 0) {
index -;
} else {
index = 9;
try {
Thread.sleep (2000);
} catch (InterruptedException e) {
/ / TODO Auto-generated catch block
e.printStackTrace ();
}
}
try {
Thread.sleep (800);
} catch ( InterruptedException e) {
/ / TODO Auto-generated catch block
e.printStackTrace ();
}
}
}}

The main class creates a moverIcono, this is going to do all the animation, this class inherits from Thread, so it's a thread in the constructor initializes a list which contains ImageIcon the icon. Then we implement the run () method of the Thread class, and assign an icon to the window, then decreased the index and look forward to 0.8 seconds and change the icon, and so on.

The index starts at 9 and will be declining until it reaches 0, then the index takes again the value of 9 and wait 2 seconds before resuming the animation.

0 comments:

Post a Comment