Java 3色渐变

8
我有一个JPanel,我想在它里面绘制一个渐变。我已经有了下面的代码,但是这只绘制了一个由两种颜色组成的渐变。我想添加第三种颜色,但不知道该怎么做。我想要的是将面板的左上角涂成白色,右上角涂成红色,两个底角都涂成黑色。要实现这个目标,我应该做些什么呢?类似这样的效果:

Example Gradient

package pocketshop.util;

import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;

public class ColorPicker extends JPanel{

    public ColorPicker(){
        repaint();
    }

    @Override
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D)g;
        int w = getWidth();
        int h = getHeight();

        GradientPaint gp = new GradientPaint(
                0, 0, Color.white,
                0, h, Color.black);

        g2d.setPaint(gp);
        g2d.fillRect(0, 0, w, h);
    }
}



编辑:可能的解决方案

我能想到的方法是使用两个渐变,一个水平的,一个垂直的,就像这样:

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D)g;
        int w = getWidth();
        int h = getHeight();

        // Vertical
        GradientPaint gp = new GradientPaint(
                0, 0, new Color(0,0,0,0),
                0, h, Color.black);

        // Horizontal
        GradientPaint gp2 = new GradientPaint(
                0, 0, Color.white,
                w, 0, Color.red, true);

        g2d.setPaint(gp2);
        g2d.fillRect(0, 0, w, h);
        g2d.setPaint(gp);
        g2d.fillRect(0, 0, w, h);
    }
2个回答

9

你希望类似这个样子吗?

三色渐变

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;

public class ThreeWayGradient {

    public static void main(String[] args) {
        final BufferedImage image = new BufferedImage(
                200, 200, BufferedImage.TYPE_INT_RGB);
        Runnable r = new Runnable() {
            @Override
            public void run() {
                Graphics2D g = image.createGraphics();
                GradientPaint primary = new GradientPaint(
                        0f, 0f, Color.WHITE, 200f, 0f, Color.ORANGE);
                GradientPaint shade = new GradientPaint(
                        0f, 0f, new Color(0, 0, 0, 0),
                        0f, 200f, new Color(0, 0, 0, 255));
                g.setPaint(primary);
                g.fillRect(0, 0, 200, 200);
                g.setPaint(shade);
                g.fillRect(0, 0, 200, 200);

                JLabel l = new JLabel(new ImageIcon(image));
                JOptionPane.showMessageDialog(null, l);
                File f = new File(System.getProperty("user.home"),
                        "ThreeWayGradient.png");
                try {
                    ImageIO.write(image, "png", f);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

将其转换为工厂方法

因为它更加美观。

将ThreeWayGradient作为工厂方法

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;

public class ThreeWayGradient {

    public static BufferedImage getThreeWayGradient(
            int size,
            Color primaryLeft,
            Color primaryRight,
            Color shadeColor) {
        BufferedImage image = new BufferedImage(
                size, size, BufferedImage.TYPE_INT_RGB);

        Graphics2D g = image.createGraphics();
        GradientPaint primary = new GradientPaint(
                0f, 0f, primaryLeft, size, 0f, primaryRight);
        int rC = shadeColor.getRed();
        int gC = shadeColor.getGreen();
        int bC = shadeColor.getBlue();
        GradientPaint shade = new GradientPaint(
                0f, 0f, new Color(rC, gC, bC, 0),
                0f, size, shadeColor);
        g.setPaint(primary);
        g.fillRect(0, 0, size, size);
        g.setPaint(shade);
        g.fillRect(0, 0, size, size);

        g.dispose();
        return image;
    }

    /**
     * Presumed to have a layout that shows multiple components.
     */
    public static void addGradient(
            JPanel p, int s, Color pL, Color pR, Color sh) {

        JLabel l = new JLabel(new ImageIcon(getThreeWayGradient(s, pL, pR, sh)));
        p.add(l);
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                JPanel gui = new JPanel(new GridLayout(2,4,1,1));
                addGradient(gui,100,Color.YELLOW,Color.RED,Color.GREEN);
                addGradient(gui,100,Color.GREEN,Color.YELLOW,Color.RED);
                addGradient(gui,100,Color.RED,Color.GREEN,Color.YELLOW);
                addGradient(gui,100,Color.BLUE,Color.MAGENTA,Color.PINK);
                addGradient(gui,100,Color.WHITE,Color.RED,Color.BLACK);
                addGradient(gui,100,Color.RED,Color.GREEN,Color.BLACK);
                addGradient(gui,100,Color.BLUE,Color.PINK,Color.BLACK);
                addGradient(gui,100,Color.BLUE,Color.CYAN,Color.BLACK);
                JOptionPane.showMessageDialog(null, gui);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

5
请看一下 LinearGradientPaint,它允许您指定 n 种颜色和它们的权重。

更新1

需求有了“小”变化,就很难说 LinearGradientPaintGradientPant 在性能上会有多大影响。

我强烈建议您浏览一下Harmonic Code。这个家伙写了一些非常有趣的帖子,其中一些是关于渐变的。;)

更新2

我知道我以前看到过类似的东西:双线性颜色插值


我想到了使用两个GradientPaint,一个垂直的和一个水平的。它可以工作,但是比LinearGradientPaint慢吗? - Get Off My Lawn
很难说。这取决于梯度的复杂程度。 - MadProgrammer
从我的理解来看,我认为你想要的最好的实现方式就是你现在所做的,不需要自己编写... - MadProgrammer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接