从网站绘制数据

时间:2015-06-17 08:29:40

标签: java html loops graph

我正在创建一个程序,使用直接从国家气象服务网站检索的数据绘制特定城市的每日最高温度。我有数据,但是当我尝试创建实际图形时程序错误。我的错误说我需要一个返回类型但是如果你查看我的代码,我就有一个。它采用OOP格式,因此我将向您展示的代码是实现类的一部分。有人能告诉我如何解决我的错误吗?

我的错误:

invalid method declaration; return type required

我的代码:

public class createGraph
{
    Picture canvas = null;
    Graphics g = null;
    Graphics2D g2 = null;

    DrawingGraph(int length, int height, Color color)
    {
        canvas = new Picture(length, height);
        canvas.setAllPixelsToAColor(color);
        g = canvas.getGraphics();
        g2 = (Graphics2D)g;
        g2.setColor(color);    
    }

    public void drawARectangle(Color color, int x1, int y1, int width, int height)
    {
        g2.setColor(color);
        g2.drawRect(x1, y1, width, height);
    }

    public Picture getGraph()
    {
        return canvas;
    }

    g.drawString("Maximum Temperature Readings", 196, 65);
    g.drawString("Pensacola, FL - August, 2009", 205, 80);

    int xPos = 97;
    for(int day = 1; day <= 31; day++)
    {
        xPos = xPos + 13;
        g.drawLine(xPos, 500, xPos, 510);

    }

    for(int yPos = 100; yPos <= 500; yPos +=40)
    {
        g.drawLine(93,yPos, 100, yPos);

    }

    g.drawString("100", 70, 105); 
    g.drawString("90", 75, 145);   
    g.drawString("80", 75, 185);
    g.drawString("70", 75, 225);
}

最终的图表应该看起来像这样:

graph

3 个答案:

答案 0 :(得分:1)

这段代码:

DrawingGraph(int length, int height, Color color)
{
    canvas = new Picture(length, height);
    canvas.setAllPixelsToAColor(color);
    g = canvas.getGraphics();
    g2 = (Graphics2D)g;
    g2.setColor(color);    
}

这应该是一个方法还是构造函数?如果它是一种常规方法,则需要void,或者如果它是构造函数,则它应该与该类名称相同。

你也有许多代码根本不在方法中。

答案 1 :(得分:0)

DrawingGraph不是函数也不是构造函数,如果它是一个函数,它需要返回类型,如果它应该是一个构造函数,它必须与类同名。

此外,在getGraph()之后,类的最后一部分必须位于函数中,但直接位于类主体中。 只需声明一个新函数并将其移入调用它。

答案 2 :(得分:0)

类名以lowerCase开头。 你是否真的不知道你做了什么交换&#34; lowerCase&#34;和&#34; DrawingGraph&#34; ?