java - 如何在 Java Graphics 中正确渲染颜色

标签 java swing graphics paintcomponent

我正在使用此代码放置 here为我的数据集生成条形图。然而,颜色都是相同的(代码中的红色),所以我决定为此生成一个色带。我写了以下代码:

Color[] getColorRamp(int numColours)
{
    Color[] colours = new Color[numColours];
    int red_1 = 255; 
    int green_1 = 0; 
    int blue_1 = 0; 
    int red_2 = 0; 
    int green_2 = 0; 
    int blue_2 = 255; 

    int count = 0; 

    for (float t=0.0f;t<1.0f;t+=1.0/(float)numColours) {
        colours[count] = new Color((int)(t*red_2 + (1-t)*red_1),
                (int)(t*green_2 + (1-t)*green_1),
                (int)(t*blue_2 + (1-t)*blue_1),34); 

        //System.out.print((int)(t*red_2 + (1-t)*red_1) +",");
        //System.out.print((int)(t*green_2 + (1-t)*green_1) +",");
        //System.out.println((int)(t*blue_2 + (1-t)*blue_1));
    }
    return colours; 
}

问题就是从这里开始的。只有第一种颜色(浅蓝色)才能正确渲染。其他颜色渲染为黑色!您可以看到我已经放置了 System.out.println 来验证生成的颜色(在此处发布的代码中进行了注释)。我看到颜色是作为完美的 RGB 组合生成的。

修改后的条形图函数发布在此处:

void drawBarChart(Graphics g, double[] values, String[] names, String title)
{ 

    if (values == null || values.length == 0)
        return;
    double minValue = 0;
    double maxValue = 0;
    for (int i = 0; i < values.length; i++) {
        if (minValue > values[i])
            minValue = values[i];
        if (maxValue < values[i])
            maxValue = values[i];
    }

    //Graphics2D g = (Graphics2D)gg; 

    Dimension d = getSize();
    int clientWidth = d.width;
    int clientHeight = d.height;
    int barWidth = clientWidth / values.length;

    Font titleFont = new Font("SansSerif", Font.BOLD, 20);
    FontMetrics titleFontMetrics = g.getFontMetrics(titleFont);
    Font labelFont = new Font("SansSerif", Font.PLAIN, 10);
    FontMetrics labelFontMetrics = g.getFontMetrics(labelFont);

    int titleWidth = titleFontMetrics.stringWidth(title);
    int y = titleFontMetrics.getAscent();
    int x = (clientWidth - titleWidth) / 2;
    g.setFont(titleFont);
    g.drawString(title, x, y);

    int top = titleFontMetrics.getHeight();
    int bottom = labelFontMetrics.getHeight();
    if (maxValue == minValue)
        return;
    double scale = (clientHeight - top - bottom) / (maxValue - minValue);
    y = clientHeight - labelFontMetrics.getDescent();
    g.setFont(labelFont);

    Color[] colours = getColorRamp(values.length);

    for (int i = 0; i < values.length; i++) {
        int valueX = i * barWidth + 1;
        int valueY = top;
        int height = (int) (values[i] * scale);
        if (values[i] >= 0)
            valueY += (int) ((maxValue - values[i]) * scale);
        else {
            valueY += (int) (maxValue * scale);
            height = -height;
        }

        g.setColor(colours[i]);
        g.fillRect(valueX, valueY, barWidth - 2, height);
        g.setColor(Color.black);
        g.drawRect(valueX, valueY, barWidth - 2, height);
        int labelWidth = labelFontMetrics.stringWidth(names[i]);
        x = i * barWidth + (barWidth - labelWidth) / 2;
        g.drawString(names[i], x, y);
    }       
    //paintComponent(g);
}

我想知道我犯了什么错误!

最佳答案

你现在可能会打自己的头。它失败的原因是您在设置第一个颜色后忘记增加变量 count ,因此您不断地覆盖 Color 数组的第一个元素,并将数组中的所有其他值保留为它们的初始默认值 (null)。

固定代码:

for (float t=0.0f;t<1.0f;t+=1.0/(float)numColours) {
    colours[count++] = new Color((int)(t*red_2 + (1-t)*red_1),
            (int)(t*green_2 + (1-t)*green_1),
            (int)(t*blue_2 + (1-t)*blue_1),34); 
}

(注意颜色[count++])

关于java - 如何在 Java Graphics 中正确渲染颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22266216/

相关文章:

java - Maven 阴影复制 WEB-INF

java - Linux 上 Android Studio 的 JDK 路径

java - 多个 if then else 在监听器类上

c# - Marshal.Copy/UnlockBits 挂起

java - 如何在java中使用两个paint方法?或者在基本的油漆方法之外

java - Apache Mina Core 不兼容的类更改错误

java - 如何在 JPanel 的 gridLayout 中绘制随机形状

java - 选择 JRadioButton 时如何刷新 JPanel?

java - 如何从 jOptionPane 在 JFrame 上显示输入?

java - 如何在 JFrames 中使用 2 类绘制通过 Netbeans 中的 Palette 生成代码?