[英]Draw points defined in file in Java
I am trying to learn some of the graphics side of Java. 我正在尝试学习Java的某些图形方面。 I made a text file that lists the points and the first line in the file determines how many points there are. 我制作了一个列出这些点的文本文件,文件的第一行确定了多少点。 I need to take the points in the text file and plot them on a canvas. 我需要将点放在文本文件中并将其绘制在画布上。 I have looked at several resources and I just do not understand how everything works together. 我看过几种资源,但我只是不明白所有事情如何协同工作。 I cannot even find a tutorial that gives all of the code and explains where each part goes and how it is called. 我什至找不到一个提供所有代码并解释每个部分的位置以及如何调用它的教程。 Basically, I am confused by JPanels, JFrames, and basically the whole process. 基本上,我对JPanels,JFrames和整个过程感到困惑。 Below is the code that I have written so far and a screen shot of what the file looks like. 以下是我到目前为止编写的代码以及该文件的外观的屏幕快照。
Code: 码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.Graphics.*;
import java.util.*;
import java.io.*;
public class drawPoints extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
this.setBackground(Color.WHITE);
g.setColor(Color.BLUE);
try{
FileInputStream fstream = new FileInputStream("Desktop/Assign2Test1.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
int i = 1;
while ((strLine = br.readLine()) != null){
final Point[] points = new Point[Integer.parseInt(br.readLine())];
final String[] split = strLine.split("\u0009");
points[i++] = new Point(Integer.parseInt(split[0]), Integer.parseInt(split[1]));
}
in.close();
}catch (Exception e){ System.err.println("Error: " + e.getMessage());
}
}
import javax.swing.*;
public class mainDrawPoint
{
public static void main(String args[])
{
JFrame f = new JFrame("Draw Points Application");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
drawPoints dP = new drawPoints();
f.add(dP);
f.setSize(500,500);
f.setVisible(true);
}
}
All the code is doing is putting the values in an array. 代码要做的就是将值放入数组中。
The x and y coordinates are separated by a tab. x和y坐标由制表符分隔。 Any help would be appreciated! 任何帮助,将不胜感激!
Something you should consider is the following: 您应该考虑以下内容:
From what I can tell, the first number in there is the count. 据我所知,第一个数字是计数。 If that is indeed it, you don't need the LinkedList for the Strings. 如果确实如此,则不需要Strings的LinkedList。 Build a Point array, for example: 构建一个Point数组,例如:
final Point[] points = new Point[Integer.parseInt(br.readLine())];
From there, using that looping system you have with the strLine variable, use that String and work out how to parse it, for example: 从那里开始,使用具有strLine变量的循环系统,使用该String并计算出如何解析它,例如:
int i = 0; // Put this outside of the while loop.
//While loop condition check here
final String[] split = strLine.split("\u0009"); // Unicode character for tab.
points[i++] = new Point(Integer.parseInt(split[0]), Integer.parseInt(split[1])); // Assuming length 2 of split.
As for rendering the points, create a new class, one that extends JPanel. 至于渲染点,请创建一个新类,该类扩展了JPanel。 In that class, add the following code and fill in the TODO: 在该类中,添加以下代码并填写TODO:
@Override
public void paintComponent(final Graphics g){
//TODO: Paint what you want here, automatically double-buffered
}
Now, when you create a new Panel class and add it to a JFrame, whenever you call for a repaint() from the JFrame it will render the code in the paintComponent() method of the Panel class. 现在,当您创建一个新的Panel类并将其添加到JFrame时,每当您从JFrame调用repaint()时,它将在Panel类的paintComponent()方法中呈现代码。 If you have any other questions, feel free to ask. 如果您还有其他疑问,请随时提问。
Edit: Example code: 编辑:示例代码:
import javax.swing.*;
import java.awt.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
public class Test {
private static final String FILE = "Desktop/Assign2Test1.txt";
private static Point[] points;
public static void main(final String[] args){
try{
final BufferedReader br = new BufferedReader(new FileReader(new File(FILE)));
points = new Point[Integer.parseInt(br.readLine())];
int i = 0;
int xMax = 0;
int yMax = 0;
while(br.ready()){
final String[] split = br.readLine().split("\u0009");
final int x = Integer.parseInt(split[0]);
final int y = Integer.parseInt(split[1]);
xMax = Math.max(x, xMax);
yMax = Math.max(y, yMax);
points[i++] = new Point(x, y);
}
final JFrame frame = new JFrame("Point Data Rendering");
final Panel panel = new Panel();
panel.setPreferredSize(new Dimension(xMax + 10, yMax + 10));
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);
frame.repaint();
} catch (final Exception e){
e.printStackTrace();
}
}
public static class Panel extends JPanel {
@Override
public void paintComponent(final Graphics g){
g.setColor(Color.RED);
for(final Point p : points){
g.fillRect((int) p.getX(), (int) p.getY(), 2, 2);
}
}
}
}
obtain the instance of the Graphics object from the element by using the panel.getGraphics()
method. 使用panel.getGraphics()
方法从元素中获取Graphics对象的实例。 later use the graphics object to draw lines using the graphics.drawLine()
method . 稍后使用graphics对象使用graphics.drawLine()
方法绘制线条。
you can split the content of a line using a regex string.split("\\t")
. 您可以使用正则表达式string.split("\\t")
分割行的内容。 this will give you an array with 2 elements (since you say that each line contains 2 numbers with a tab in between). 这将为您提供一个包含2个元素的数组(因为您说每行包含2个数字,并且它们之间使用制表符)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.