简体   繁体   English

Java 3 Color Gradient

[英]Java 3 Color Gradient

I have a JPanel , and I would like to paint a gradient within it. 我有一个JPanel ,我想在其中绘制一个渐变。 I have the code below, but that only paints a 2 color gradient. 我有下面的代码,但只绘制了2色渐变。 I would like to add a 3rd but don't know how. 我想添加第3个但不知道如何。 What I want is to have the top left of the panel as white, top right red, and both bottom corners black. 我想要的是让面板的左上角为白色,右上角为红色,两个底角为黑色。 What would I have to do to achieve this, something that looks like this: 我需要做些什么来实现这一点,看起来像这样:

示例渐变

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);
    }
}



Edit: Possible solution 编辑:可能的解决方案

I was able to come up with using 2 gradients one horizontal and one vertical, like this: 我能够想出使用2个渐变,一个水平和一个垂直,像这样:

    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);
    }

Something like this? 像这样的东西?

三向色彩渐变

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);
    }
}

Making it into a factory method 使其成为一种工厂方法

..because it is prettier. 因为它更漂亮。

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);
    }
}

Take a look at LinearGradientPaint , it allows you to specify n number of colours and their weights. 看看LinearGradientPaint ,它允许您指定n个颜色及其权重。

Update 1 更新1

With a "small" change in requirements, it is debatable if either LinearGradientPaint over GradientPant will have any significant effect in performance. 随着需求的“小”变化,如果GradientPant LinearGradientPaint对性能有任何显着影响,那么值得商榷。

I highly recommend that you take a look at Harmonic Code . 我强烈建议你看一下Harmonic Code This guy does some really interesting posts, and some on gradients. 这个人做了一些非常有趣的帖子,还有一些关于渐变。 ;) ;)

Update 2 更新2

Knew I'd seen something like it before Bilinear color interpolation . 知道我在Bilinear颜色插值之前看过类似的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM