Java 3 Color Gradient

前端 未结 2 931
孤独总比滥情好
孤独总比滥情好 2021-01-12 02:50

I have a JPanel, and I would like to paint a gradient within it. I have the code below, but that only paints a 2 color gradient. I would like to add a 3rd but d

相关标签:
2条回答
  • 2021-01-12 03:38

    Something like this?

    Three way Color Gradient

    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 as factory method

    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);
        }
    }
    
    0 讨论(0)
  • 2021-01-12 03:50

    Take a look at LinearGradientPaint, it allows you to specify n number of colours and their weights.

    Update 1

    With a "small" change in requirements, it is debatable if either LinearGradientPaint over GradientPant will have any significant effect in performance.

    I highly recommend that you take a look at Harmonic Code. This guy does some really interesting posts, and some on gradients. ;)

    Update 2

    Knew I'd seen something like it before Bilinear color interpolation.

    0 讨论(0)
提交回复
热议问题