【问题标题】:How to create a colour gradient without Javafx/AWT?如何在没有 Javafx/AWT 的情况下创建颜色渐变?
【发布时间】:2017-02-27 00:07:34
【问题描述】:

所以我有一个小问题,我从几个小时开始就尝试解决它。 我有一个 BufferedImage,我想以流畅的方式更改颜色 f.e.从红色到白色。 我的主要:

public static void main(String[] args) {
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

    for (int x = 0; x != width; x++) {
        for (int y = 0; y != height; y++) {
            image.setRGB(x, y, color12(x, y));



    try {
        ,,,

我改变颜色的方法:

static int color12(int x, int y) {      
    int size = 100;
    if (Math.abs(width / 2 - x) < size / 2 && Math.abs(height / 2 - y) < size / 2)
        return new Color(255, 0, 0).getRGB();
    else
        return new Color(200, 200, 255).getRGB();
}

}

所以我玩弄了这个方法,但我无法完成。 我最好的“解决方案”是这样的:

int r = 0 ;     
    int b = 0;
    int g = 0;
    for (int i = 1; i < 255; i++) 
r++; 

否则返回新颜色(r,g,b).getRGB();

谁能帮帮我?

【问题讨论】:

    标签: java eclipse colors linear-gradients


    【解决方案1】:

    我不确定你想要怎样的渐变(例如水平、垂直或对角线),但这里有一个使用线性插值的示例,用于水平或垂直渐变。

    import java.awt.Color;
    import java.awt.image.BufferedImage;
    import javax.swing.BoxLayout;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.SwingUtilities;
    
    public class ExampleFrame extends JFrame {
    
        private static enum GradientOrientation {  
            HORIZONTAL, VERTICAL
        };
    
        private static BufferedImage createGradientImg(int width, int height, Color startColor, Color endColor, GradientOrientation o) {
            BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            for (int x = 0; x < width; x++) {
                for (int y = 0; y < height; y++) {
                    int pos = o.equals(GradientOrientation.HORIZONTAL) ? x : y;
                    int size = o.equals(GradientOrientation.HORIZONTAL) ? width : height;
                    image.setRGB(x, y, getGradientRGB(startColor, endColor, pos, size));
                }
            }
            return image;
        }
    
        private static int getGradientRGB(Color startColor, Color endColor, int pos, int size) {
            double perc = (double) pos / size;
            int newRed = (int) (endColor.getRed() * perc + startColor.getRed() * (1 - perc));
            int newGreen = (int) (endColor.getGreen() * perc + startColor.getGreen() * (1 - perc));
            int newBlue = (int) (endColor.getBlue() * perc + startColor.getBlue() * (1 - perc));
            return new Color(newRed, newGreen, newBlue).getRGB();
        }
    
        public void createAndShow() {
            BufferedImage img1 = createGradientImg(200, 100, Color.RED,
                    Color.WHITE, GradientOrientation.HORIZONTAL);
            BufferedImage img2 = createGradientImg(200, 100, Color.BLUE,
                    Color.YELLOW, GradientOrientation.HORIZONTAL);
            BufferedImage img3 = createGradientImg(200, 100, Color.WHITE,
                    Color.YELLOW, GradientOrientation.VERTICAL);
            BufferedImage img4 = createGradientImg(200, 100, Color.BLACK,
                    Color.WHITE, GradientOrientation.VERTICAL);
    
            BoxLayout layout = new BoxLayout(getContentPane(), BoxLayout.Y_AXIS);
            getContentPane().setLayout(layout);
            getContentPane().add(new JLabel(new ImageIcon(img1)));
            getContentPane().add(new JLabel(new ImageIcon(img2)));
            getContentPane().add(new JLabel(new ImageIcon(img3)));
            getContentPane().add(new JLabel(new ImageIcon(img4)));
            pack();
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setVisible(true);
        }
    
        public static void main(String[] args) {
    
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    ExampleFrame ef = new ExampleFrame();
                    ef.createAndShow();
                }
            });
        }
    }
    

    【讨论】:

    • 但我不想使用 swing 或 awt :) 是否可以仅通过编辑静态 int 颜色方法来完成任务?
    • 我不完全确定您要达到的目标,但您可以将您的 color12() 方法更改为我的 getGradientRGB() 方法;只需将正确的参数传递给它。在我的示例中,用于创建渐变图像的唯一类是 BufferedImageColor 根据您的原始问题;这两个都是awt 包的一部分。其余的纯粹是为了演示,即作为示例显示,为了将其显示为 GUI,您需要使用 awt/Swing/JavaFX/swt/etc。除非你想从头开始构建一个库。
    • 嗨。所以我想让例如我的颜色以红色开始,它从 (255,0,0) 到 (255,255,255) 逐步变化.. 以流畅的方式从红色变为白色。我的 color12 方法已经在创建一种颜色,但只有 1 种,不多。我想要很多颜色(从红色到白色)。我玩弄了主要方法和方法,但无法以正确的方式完成。我想我只需要在 color12 中添加一个 for 或 while 方法,但我不确定..也许你现在理解我了(我知道这很难,因为我的英语不流利)
    猜你喜欢
    • 2018-05-15
    • 1970-01-01
    • 1970-01-01
    • 2019-05-14
    • 1970-01-01
    • 2014-02-20
    • 2019-06-14
    • 2014-10-29
    • 1970-01-01
    相关资源
    最近更新 更多