Java Graphics Draw Gradient linearGradient(final JComponent comp, final Color col, final int step, final int sleepTime)

Here you can find the source of linearGradient(final JComponent comp, final Color col, final int step, final int sleepTime)

Description

linear Gradient

License

Open Source License

Declaration

public static SwingWorker<Void, Void> linearGradient(final JComponent comp, final Color col, final int step,
            final int sleepTime) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.awt.Color;

import javax.swing.JComponent;
import javax.swing.SwingWorker;

public class Main {
    public static SwingWorker<Void, Void> linearGradient(final JComponent comp, final Color col, final int step,
            final int sleepTime) {
        final Color cCol = comp.getBackground();
        SwingWorker<Void, Void> colorThread = new SwingWorker<Void, Void>() {

            @Override//ww  w  .  java  2  s  .co m
            protected Void doInBackground() throws Exception {
                int cR = cCol.getRed();
                int cG = cCol.getGreen();
                int cB = cCol.getBlue();

                int R = col.getRed();
                int G = col.getGreen();
                int B = col.getBlue();

                int stepR = (R - cR) / step;
                int stepG = (G - cG) / step;
                int stepB = (B - cB) / step;

                for (int i = 0; i < step; i++) {
                    cR += stepR;
                    cG += stepG;
                    cB += stepB;

                    if (i < step / 2)
                        Thread.sleep(sleepTime);
                    else
                        Thread.sleep(sleepTime / 2);

                    comp.setBackground(new Color(cR, cG, cB));
                }

                return null;
            }
        };

        return colorThread;
    }
}

Related

  1. DrawGradient(Graphics g, int x, int y, int width, int height, Color color1, Color color2)
  2. paintGradient(Graphics g, JComponent comp, Color color1, Color color2)
  3. paintGradientSelection(Color topLineColor, Color bottomLineColor, Color topGradientColor, Color bottomGradientColor, Graphics2D graphics2D, int width, int height)
  4. paintHorizontalGradient2D(Graphics2D g, int x, int y, int w, int h, float g1, float g2, Color c1, Color c2, Color c3, int[][] mask)
  5. paintListGradientSelection(Graphics2D graphics2D, Component component, int width, int height)