簡體   English   中英

如何在Java圖形中正確渲染顏色

[英]How to render colours properly in Java Graphics

我使用此代碼放在這里產生酒吧圖表為我的數據集。 但是,顏色都是相同的(代碼中為紅色),因此我決定為此生成一個色帶。 我寫了以下代碼:

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

(注意colours[count++]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM