简体   繁体   English

Java-在txt文件中绘制线条阅读点

[英]Java - draw lines reading points in a txt file

I'm trying to understand how Java and in particular Swing work, so I made an application for drawing freehand lines on a JPanel. 我试图了解Java特别是Swing的工作原理,因此我制作了一个在JPanel上绘制徒手线条的应用程序。 Then with the Save button I can memorize some informations (when user began and ended drawing the line, which points make the line etc.). 然后,使用“保存”按钮,我可以记住一些信息(当用户开始和结束绘制线条时,这些点构成了线条等)。

I save informations this way: 我这样保存信息:

EDIT I remomed useless stuff and simply put a space between different informations.. 编辑我删除了无用的东西,只是在不同的信息之间放置了一个空格。

line 04:54:34:365 04:54:35:167 [java.awt.Point[x=249,y=114], java.awt.Point[x=249,y=114], java.awt.Point[x=207,y=87], java.awt.Point[x=207,y=87]]

This is an example of a line; 这是一条线的例子; can you suggest me how to parse the file to draw the lines back to the JPanel? 您能建议我如何解析文件以将线条画回到JPanel吗? I think it should work like "for every line, go after 'points' word and take the data and draw it". 我认为它的工作方式应像是“对于每一行,紧跟“要点”一词并获取数据并绘制出来”。 Is it the correct way to do it? 这是正确的方法吗? I've already managed to read and write from/to the file, so the problem resides in taking only that part of the line and give it as an input to the drawing function. 我已经设法读写文件,所以问题出在仅占用那部分线并将其作为绘图功能的输入。

This is the way I read from the file (.txt): 这是我从文件(.txt)中读取的方式:

List<String> lines = Files.readAllLines(Paths.get(file.getPath()), Charset.forName("UTF-8"));

Thank you! 谢谢!

I would do something similar to AnubianNoob's suggestion but with each drawn "line", even made up of many points, on each line in your text file. 我会做一些类似于AnubianNoob的建议,但是在文本文件的每一行上绘制每条“线”,甚至由许多点组成。 Even if you don't wish to do that, it would be easiest to simply print out only the numbers and not the whole object. 即使您不希望这样做,仅打印数字而不打印整个对象也是最简单的。

After creating two lines, maybe text file looks like: 创建两行之后,文本文件可能看起来像:

249 114 207 87 230 110
115 240 90 210 112 245

Now you can read all lines like you do already, and then split each line by the space (or whatever character you want to put between values) 现在,您可以像读取已经读取的所有行一样,然后按空格(或要在值之间放置的任何字符)分隔每行

//for each line in lines
String[] values = line.split(" ");
//Note we are adding 2 to i in each loop, because the 2nd value will be y value
for (int i = 0; i < values.length; i+=2)
{
    //Now just recreate the points
    //values[i] = x, values[i+1] = y
}

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

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