Java Animation: Image Fading
Submitted by donbermoy on Monday, November 10, 2014 - 13:16.
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:
3. The ImageFading class must extends the JPanel and implements the ActionListener for the ticking of time. Have also the following variables below:
5. Create the ActionEvent when the time starts.
6. Create a constructor that will start the timer.
7. In your Main, create a JFrame component that will hold all other components. This will set the size, location, title, and visibility.
Here's the full code of this tutorial:
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
- 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
4. Create the paint method for the image to be in 2D with the fading effects of AlphaComposite.
- super.paint(g); //paint graphics
- g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alphaImg)); //this code is for the fading effects on the image
- g2d.drawImage(img, 10, 10, null); //draw the image file in a 2D form
- }
- 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 ImageFading() { //constructor
- timer.start(); //starts the time
- }
Output:

- 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
- 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.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alphaImg)); //this code is for the fading effects on the image
- g2d.drawImage(img, 10, 10, null); //draw the image file in a 2D form
- }
- 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();
- }
- frame.getContentPane().add(new ImageFading());
- frame.setSize(550, 250);
- frame.setVisible(true);
- }
- }
Add new comment
- 399 views