In this tutorial, we will create a java animation program that has the image fading effects.
So, now let's start this tutorial!
1. Open JCreator or NetBeans and make a java program with a file name of
ImageFading.java.
2. Import the following packages:
import java.awt.*; //used to access AlphaComposite, Graphics, Graphics2D and Image class
import java.awt.event.*; //used to access ActionListener and Action events
import javax.swing.*; //used to have the ImageIcon, JFrame, JPanel, and Timer
3. The ImageFading class must extends the JPanel and implements the ActionListener for the ticking of time. Have also the following variables below:
Image img
= new ImageIcon("ImageFading.png").
getImage(); //get the image file, the file must be in the same folder Timer timer
= new Timer(50,
this); // initializes the timer into 50 ms
private float alphaImg = 1f; // the variable for the image fading effects
4. Create the paint method for the image to be in 2D with the fading effects of AlphaComposite.
super.paint(g); //paint graphics
g2d.drawImage(img, 10, 10, null); //draw the image file in a 2D form
}
5. Create the ActionEvent when the time starts.
public void actionPerformed
(ActionEvent e
) { //performs when the time starts alphaImg += -0.01f;
if (alphaImg <= 0) {
alphaImg = 0; // as the timer starts, the image will slowly fade because of its alpha value
timer.stop(); // the timer stops after the alphaImg will be equal to 0
}
repaint();
}
6. Create a constructor that will start the timer.
public ImageFading() { //constructor
timer.start(); //starts the time
}
7. In your Main, create a JFrame component that will hold all other components. This will set the size, location, title, and visibility.
public static void main
(String[] args
) { frame.getContentPane().add(new ImageFading());
frame.
setDefaultCloseOperation(JFrame.
EXIT_ON_CLOSE); frame.setSize(550, 250);
frame.setVisible(true);
}
Output:
Here's the full code of this tutorial:
import java.awt.*; //used to access AlphaComposite, Graphics, Graphics2D and Image class
import java.awt.event.*; //used to access ActionListener and Action events
import javax.swing.*; //used to have the ImageIcon, JFrame, JPanel, and Timer
Image img
= new ImageIcon("ImageFading.png").
getImage(); //get the image file, the file must be in the same folder Timer timer
= new Timer(50,
this); // initializes the timer into 50 ms
private float alphaImg = 1f; // the variable for the image fading effects
public ImageFading() { //constructor
timer.start(); //starts the time
}
super.paint(g); //paint graphics
g2d.drawImage(img, 10, 10, null); //draw the image file in a 2D form
}
public void actionPerformed
(ActionEvent e
) { //performs when the time starts alphaImg += -0.01f;
if (alphaImg <= 0) {
alphaImg = 0; // as the timer starts, the image will slowly fade because of its alpha value
timer.stop(); // the timer stops after the alphaImg will be equal to 0
}
repaint();
}
public static void main
(String[] args
) { frame.getContentPane().add(new ImageFading());
frame.
setDefaultCloseOperation(JFrame.
EXIT_ON_CLOSE); frame.setSize(550, 250);
frame.setVisible(true);
}
}
Best Regards,
Engr. Lyndon Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer
If you have some queries, feel free to contact the number or e-mail below.
Mobile: 09488225971
Landline: 826-9296
E-mail:
[email protected]
Add and Follow me on Facebook:
https://www.facebook.com/donzzsky
Visit and like my page on Facebook at:
https://www.facebook.com/BermzISware