Java Animation: Image Fading

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:
  1. import java.awt.*; //used to access AlphaComposite, Graphics, Graphics2D and Image class
  2. import java.awt.event.*; //used to access ActionListener and Action events
  3. 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:
  1.   Image img = new ImageIcon("ImageFading.png").getImage(); //get the image file, the file must be in the same folder
  2.   Timer timer = new Timer(50, this); // initializes the timer into 50 ms
  3.  
  4.   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.
  1.   public void paint(Graphics g) {
  2.     super.paint(g); //paint graphics
  3.     Graphics2D g2d = (Graphics2D) g; //create a 2D graphics
  4.  
  5.     g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alphaImg)); //this code is for the fading effects on the image
  6.     g2d.drawImage(img, 10, 10, null); //draw the image file in a 2D form
  7.   }
5. Create the ActionEvent when the time starts.
  1.   public void actionPerformed(ActionEvent e) { //performs when the time starts
  2.     alphaImg += -0.01f;
  3.     if (alphaImg <= 0) {
  4.       alphaImg = 0; // as the timer starts, the image will slowly fade because of its alpha value
  5.       timer.stop(); // the timer stops after the alphaImg will be equal to 0
  6.     }
  7.     repaint();
  8.   }
6. Create a constructor that will start the timer.
  1.   public ImageFading() { //constructor
  2.     timer.start();  //starts the time
  3.   }
7. In your Main, create a JFrame component that will hold all other components. This will set the size, location, title, and visibility.
  1.   public static void main(String[] args) {
  2.     JFrame frame = new JFrame("Image Fade out");
  3.     frame.getContentPane().add(new ImageFading());
  4.     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  5.     frame.setSize(550, 250);
  6.     frame.setVisible(true);
  7.   }
Output: output Here's the full code of this tutorial:
  1. import java.awt.*; //used to access AlphaComposite, Graphics, Graphics2D and Image class
  2. import java.awt.event.*; //used to access ActionListener and Action events
  3. import javax.swing.*; //used to have the ImageIcon, JFrame, JPanel, and Timer
  4.  
  5. public class  ImageFading extends JPanel implements ActionListener {
  6.        
  7.   Image img = new ImageIcon("ImageFading.png").getImage(); //get the image file, the file must be in the same folder
  8.   Timer timer = new Timer(50, this); // initializes the timer into 50 ms
  9.  
  10.   private float alphaImg = 1f; // the variable for the image fading effects
  11.  
  12.   public ImageFading() { //constructor
  13.     timer.start();  //starts the time
  14.   }
  15.  
  16.   public void paint(Graphics g) {
  17.     super.paint(g); //paint graphics
  18.     Graphics2D g2d = (Graphics2D) g; //create a 2D graphics
  19.  
  20.     g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alphaImg)); //this code is for the fading effects on the image
  21.     g2d.drawImage(img, 10, 10, null); //draw the image file in a 2D form
  22.   }
  23.  
  24.   public void actionPerformed(ActionEvent e) { //performs when the time starts
  25.     alphaImg += -0.01f;
  26.     if (alphaImg <= 0) {
  27.       alphaImg = 0; // as the timer starts, the image will slowly fade because of its alpha value
  28.       timer.stop(); // the timer stops after the alphaImg will be equal to 0
  29.     }
  30.     repaint();
  31.   }
  32.  
  33.   public static void main(String[] args) {
  34.     JFrame frame = new JFrame("Image Fade out");
  35.     frame.getContentPane().add(new ImageFading());
  36.     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  37.     frame.setSize(550, 250);
  38.     frame.setVisible(true);
  39.   }
  40.  
  41. }
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

Add new comment