All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.codename1.ui.LinearGradientPaint Maven / Gradle / Ivy

There is a newer version: 7.0.161
Show newest version
/*
 * Copyright (c) 2012, Codename One and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Codename One designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *  
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 * 
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 * 
 * Please contact Codename One through http://www.codenameone.com/ if you 
 * need additional information or have any questions.
 */
package com.codename1.ui;

import static com.codename1.ui.MultipleGradientPaint.CycleMethod.REFLECT;
import com.codename1.ui.geom.Rectangle2D;
import com.codename1.ui.geom.Shape;
import com.codename1.util.MathUtil;

/**
 * LinearGradientPaint provides a way to fill a {@link Shape} with a linear gradient.  
 * @author shannah
 * @since 7.0
 * @see Graphics#setColor(com.codename1.ui.Paint) 
 */
public class LinearGradientPaint extends MultipleGradientPaint {
    private double startX, startY, endX, endY;
    private Transform t = Transform.makeIdentity(), t2 = Transform.makeIdentity();
    
    /**
     * Creates a LinearGradientPaint with the specified settings.
     * @param startX The startX coordinate of the gradient in user space.
     * @param startY The startY coordinate of the gradient in user space.
     * @param endX The endX coordinate of the gradient in user space.
     * @param endY THe endY coordinate of the gradient in user space.
     * @param fractions Fractional positions of where gradient colors begin.  Each value should be between 0 and 1.
     * @param colors The colors to use in the gradient.  There should be the same number of colors as there are fractions.
     * @param cycleMethod The cycle method to use.
     * @param colorSpace The color space to use.
     * @param gradientTransform Transform to use for the gradient.  Not used right now.
     */
    public LinearGradientPaint(float startX, float startY, float endX, float endY, float[] fractions, int[] colors, MultipleGradientPaint.CycleMethod cycleMethod, MultipleGradientPaint.ColorSpaceType colorSpace, Transform gradientTransform) {
        super(fractions, colors, cycleMethod, colorSpace, gradientTransform);
        this.startX = startX;
        this.startY = startY;
        this.endX = endX;
        this.endY = endY;
    }
    
    /**
     * Creates a LinearGradientPaint with the specified settings.
     * @param startX The startX coordinate of the gradient in user space.
     * @param startY The startY coordinate of the gradient in user space.
     * @param endX The endX coordinate of the gradient in user space.
     * @param endY THe endY coordinate of the gradient in user space.
     * @param fractions Fractional positions of where gradient colors begin.  Each value should be between 0 and 1.
     * @param colors The colors to use in the gradient.  There should be the same number of colors as there are fractions.
     * @param cycleMethod The cycle method to use.
     * @param colorSpace The color space to use.
     * @param gradientTransform Transform to use for the gradient.  Not used right now.
     */
    public LinearGradientPaint(double startX, double startY, double endX, double endY, float[] fractions, int[] colors, MultipleGradientPaint.CycleMethod cycleMethod, MultipleGradientPaint.ColorSpaceType colorSpace, Transform gradientTransform) {
        super(fractions, colors, cycleMethod, colorSpace, gradientTransform);
        this.startX = startX;
        this.startY = startY;
        this.endX = endX;
        this.endY = endY;
    }

    /**
     * Paints linear gradient in the given bounds.
     * @param g
     * @param bounds 
     */
    @Override
    public final void paint(Graphics g, Rectangle2D bounds) {
        paint(g, bounds.getX(), bounds.getY(), bounds.getWidth(), bounds.getHeight());
    }

    
    private double insetStartLength(double x, double y, double w, double h) {
        
        
        return Math.max(w, h);
    }
    
    private double insetEndLength(double x, double y, double w, double h) {
        return Math.max(w, h);
    }
    
    private double length() {
        double x = endX-startX;
        double y = endY-startY;
        return Math.sqrt(x*x+y*y);
    }
    
    private double theta() {
        if (length() == 0) {
            return 0;
        }
        return thetaDirection() * MathUtil.acos((endX-startX)/length());
    }
    
    private int thetaDirection() {
        if (endY >= startY) {
            return 1;
        } else {
            return -1;
        }
    }
    
    private int[] reverseColors() {
        int[] colors = getColors();
        int len = colors.length;
        int[] out = new int[len];
        for (int i=0; i-->
            
    
            


© 2015 - 2024 Weber Informatics LLC | Privacy Policy