【问题标题】:Java 3 Color GradientJava 3 颜色渐变
【发布时间】:2012-12-07 21:52:13
【问题描述】:

我有一个JPanel,我想在其中绘制一个渐变。我有下面的代码,但只绘制了 2 色渐变。我想添加第三个,但不知道如何。我想要的是让面板的左上角为白色,右上角为红色,两个底角为黑色。我必须做些什么来实现这一点,看起来像这样:

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



编辑:可能的解决方案

我能够想出使用 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);
    }

【问题讨论】:

    标签: java image swing gradient


    【解决方案1】:

    这样的?

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

    把它变成工厂方法

    ..因为它更漂亮。

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

    【讨论】:

      【解决方案2】:

      看看LinearGradientPaint,它允许你指定n颜色的数量和它们的权重。

      更新 1

      由于需求的“小”变化,LinearGradientPaintGradientPant 中的任何一个是否会对性能产生任何显着影响是值得商榷的。

      我强烈建议您查看Harmonic Code。这家伙做了一些非常有趣的帖子,还有一些关于渐变的帖子。 ;)

      更新 2

      知道我在Bilinear color interpolation 之前看到过类似的东西。

      【讨论】:

      • 我想出了使用 2 GradientPaint 的一个垂直和一个水平。它有效,但比 LinearGradientPaint 慢吗?
      • 很难说。这将取决于梯度的复杂性
      • 据我所知,我认为您想要的最好按照您的方式实现,而无需编写自己的...
      猜你喜欢
      • 1970-01-01
      • 2020-06-09
      • 1970-01-01
      • 1970-01-01
      • 2020-05-25
      • 1970-01-01
      • 1970-01-01
      • 2011-04-26
      • 2011-03-06
      相关资源
      最近更新 更多