简体   繁体   English

如何绘制一个简单的图形?

[英]How to draw a simple graph?

So here's the thing, I'm supposed to read a document and import some data. 这就是问题,我应该阅读文档并导入一些数据。 A line in the document looks like this "A 0 2 5 20 23 25." 文档中的一行如下所示:“ A 0 2 5 20 23 25”。 (.txt). (。文本)。

I can write a method that finds this line and the values but my problem is to draw the actual graph whith the values. 我可以编写一种找到这条线和值的方法,但是我的问题是绘制带有值的实际图形。 X of the graph will be fixed for 10 between each value and the values in the file corresponds to Y. I want to come up with a method that takes the first Y(0), and Draws a straight line to the next Y(2) and from 2-5 etc. I fail to do this, my method only Draws a line from 0-2 and 5-20 and 23-25. 图的X将在每个值之间固定为10,并且文件中的值对应于Y。我想提出一种方法,该方法采用第一个Y(0),并向下一个Y(2)画一条直线)和2-5等。我无法执行此操作,我的方法仅从0-2和5-20和23-25绘制一条线。 Book hasn't talked about array's yet so I should do this without arrays, I try to do it as I read the file but.. 本书还没有讨论数组的问题,所以我应该在没有数组的情况下执行此操作,但是我在读取文件时会尝试这样做。

Here is a bit of my code anyway, any tips?? 无论如何,这是我的一些代码吗? Much appreciated 非常感激

public static void drawGraph(String letter, Scanner input, Graphics g){
   while (input.hasNextLine()){
     int x = 1;
     String text = input.nextLine();
     Scanner data = new Scanner(text);
     String foundLetter = data.next();       
     if(foundLetter.equalsIgnoreCase(letter)){
        while(data.hasNextInt()){                 
           int count = data.nextInt();
            //This is where I get Stuck

            g.drawLine((x-1)*10, y1, 10*x);
            x++;

          }

     }
  }

} }

I have made an template drawingpanel for the plot divided into sections of 10's 我为该情节制作了一个模板绘图面板,分为10个部分

Thanks, 谢谢,

You have to memorize the last y value, then draw a line from (x-1, last y) to (x, y) . 您必须记住最后一个y值,然后从(x-1,last y)(x,y)画一条线。

int lastY = data.nextInt();
while (data.hasNextInt()) {                 
    int nextY = data.nextInt();
    g.drawLine((x-1)*10, lastY, 10*x, nextY);
    x++;
    lastY = nextY;
}

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

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