【问题标题】:How to render colours properly in Java Graphics如何在 Java Graphics 中正确渲染颜色
【发布时间】:2014-03-08 06:57:39
【问题描述】:

我使用此代码放置在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);
}

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

【问题讨论】:

  • 这可能是字节不足/溢出问题
  • 为了尽快获得更好的帮助,请发布MCTaRE(经过测试和可读的最小完整示例)。

标签: java swing graphics paintcomponent


【解决方案1】:

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

固定代码:

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

(注意colours[count++]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-08
    • 2013-06-26
    • 2019-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-07
    • 2015-10-18
    相关资源
    最近更新 更多