简体   繁体   English

如何在Java图形中正确渲染颜色

[英]How to render colours properly in Java Graphics

I was using this code placed here to generate bar-charts for my datasets. 我使用此代码放在这里产生酒吧图表为我的数据集。 However, the colours were all the same (red in the code), so I decided to generate a colour ramp for this. 但是,颜色都是相同的(代码中为红色),因此我决定为此生成一个色带。 I wrote the following code: 我写了以下代码:

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

It is here, where the problem starts. 问题就从这里开始。 Only the first colour (pretty light blue) get rendered properly. 仅第一种颜色(淡蓝色)可以正确渲染。 Other colours are rendered as black! 其他颜色呈现为黑色! You can see that I have put System.out.println to verify the colours generated (commented in the code posted here). 您可以看到我已经放入System.out.println来验证生成的颜色(在此处发布的代码中有注释)。 I saw that colours were generated as perfect RGB combinations. 我看到颜色是作为完美的RGB组合生成的。

The modified barchart function is posted here: 修改后的条形图功能发布在这里:

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

I wish to know, what mistake I am making! 我想知道,我正在犯什么错误!

You're probably going to hit yourself on the head now. 您现在可能会撞上自己。 The reason it fails is that you forget to increase the variable count after setting the first colour, so you're constantly overwriting the first element of the Color array, and leaving all the other values in the array as their initial default ( null ). 失败的原因是您在设置第一种颜色后忘记增加变量count ,因此您将不断覆盖Color数组的第一个元素,并将数组中的所有其他值保留为其初始默认值( null )。

Fixed code: 固定代码:

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

(Notice the colours[count++] ) (注意colours[count++]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM