Java Animation: Color Fading Tutorial

In this tutorial, we will create a java animation program that has the fading color effects. So, now let's start this tutorial! 1. Open JCreator or NetBeans and make a java program with a file name of ColorFading.java. 2. Import the following packages:
  1. import java.awt.*; //this will be used to access graphics,color, and alpha composite
  2. import java.awt.geom.Rectangle2D;// used to create a 2-Dimensional Rectangle
  3. import javax.swing.*;//used to have JPanel and JFrame
3. Instantiate a variable for 2D Rectangle and the alpha/fade rectangle.
  1. private Rectangle2D rect = new Rectangle2D.Float(40f, 20f, 80f, 50f);
  2. private float fade_rectangle = 1f;
4. Create the paint method for the graphics to create the 2D Rectangle and fill it with color and the fading effects.
  1. public void paint(Graphics g) {
  2. super.paint(g); //paint the frame
  3. Graphics2D g2d = (Graphics2D) g; //create a 2D graphics
  4. g2d.setColor(new Color(100, 50, 150)); //initialize a color
  5. g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, fade_rectangle)); //fading effects of the rectangle
  6. g2d.fill(rect);//fill the rectangle
  7. }
5. Create the run method for the fading effects with the use of the thread.
  1. public void run() {
  2. while (fade_rectangle >= 0) {
  3. repaint();
  4. fade_rectangle += -0.01f;
  5.  
  6. if (fade_rectangle < 0) {
  7. fade_rectangle = 0;
  8. }
  9. try {
  10. Thread.sleep(50); //50ms the time it fades
  11. } catch (Exception e) {
  12. }
  13. }
  14. }
6. Create another class named RectRunnable that implements Runnable to call the thread to run.
  1. class RectRunnable implements Runnable {
  2. private Thread runner;
  3.  
  4. public RectRunnable() {
  5. runner = new Thread(this);
  6. runner.start(); //starts the fading
  7. }
7. Create a constructor and have it an instantiation of the class RectRunnable to run the fading.
  1. public ColorFading() {
  2. new RectRunnable();
  3. }
8. 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("Color fading animation");
  3. frame.getContentPane().add(new ColorFading());
  4. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  5. frame.setSize(250, 150);
  6. frame.setVisible(true);
  7. }
Output: Color FadingHere's the full code of this tutorial:
  1. import java.awt.*; //this will be used to access graphics,color, and alpha composite
  2. import java.awt.geom.Rectangle2D;// used to create a 2-Dimensional Rectangle
  3. import javax.swing.*;//used to have JPanel and JFrame
  4.  
  5.  
  6. public class ColorFading extends JPanel {
  7.  
  8. private Rectangle2D rect = new Rectangle2D.Float(40f, 20f, 80f, 50f);
  9. private float fade_rectangle = 1f;
  10.  
  11. public ColorFading() {
  12. new RectRunnable();
  13. }
  14.  
  15. public void paint(Graphics g) {
  16. super.paint(g); //paint the frame
  17. Graphics2D g2d = (Graphics2D) g; //create a 2D graphics
  18. g2d.setColor(new Color(100, 50, 150)); //initialize a color
  19. g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, fade_rectangle)); //fading effects of the rectangle
  20. g2d.fill(rect);//fill the rectangle
  21. }
  22.  
  23. public static void main(String[] args) {
  24. JFrame frame = new JFrame("Color fading animation");
  25. frame.getContentPane().add(new ColorFading());
  26. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  27. frame.setSize(250, 150);
  28. frame.setVisible(true);
  29. }
  30.  
  31. class RectRunnable implements Runnable {
  32. private Thread runner;
  33.  
  34. public RectRunnable() {
  35. runner = new Thread(this);
  36. runner.start();
  37. }
  38.  
  39. public void run() {
  40. while (fade_rectangle >= 0) {
  41. repaint();
  42. fade_rectangle += -0.01f;
  43.  
  44. if (fade_rectangle < 0) {
  45. fade_rectangle = 0;
  46. }
  47. try {
  48. Thread.sleep(50);
  49. } catch (Exception e) {
  50. }
  51. }
  52. }
  53. }
  54. }
Hope this helps! 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

Comments

Submitted byowuweweweuniet… (not verified)on Sat, 10/22/2022 - 19:31

hello

Add new comment