java - 在文件中的点之间绘制一条线

标签 java file-io drawing adjacency-matrix

我需要在 java 中从格式如下的文件中绘制线条:

5 //Number of lines of points
10   10
23   56
15   34
32   67
76   45

我想我必须设置两个数组,然后以某种方式添加这样的值,但我完全迷失了,确实需要一些指导。 任何帮助,将不胜感激!下面的代码是需要修改来绘制线条的。现在它只是绘制点。

代码:

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("\t");
                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);
            }
        }

    }

}

最佳答案

这里的代码将帮助您如何读取文件以及如何从那里提取值。在做任何其他事情之前,你首先必须正确地做到这一点。

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class BufferedReaderExample {

    public static void main(String[] sm) {

        BufferedReader br = null;

        try {

            String sCurrentLine;

            br = new BufferedReader(new FileReader("You file path"));
            String[] xy;
            // get your points and convert very first line
            int points = Integer.parseInt(br.readLine()); 
            while ((sCurrentLine = br.readLine()) != null) {
                xy = sCurrentLine.split("\\s+"); // split by whitespace
                System.out.println(xy[0] +" : "+ xy[1]);
                // Do something
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null)
                                        br.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

    }
}

编辑: --> 如果你想在两点之间画线。你必须使用drawline()方法。它应该像下面这样。我还为您提供了如何使 line 为 java 的链接。

1. Graphic drawLine() API

2. How to draw lines in Java

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    for (Line line : lines) {
        g.setColor(line.color);
        g.drawLine(line.x1, line.y1, line.x2, line.y2);

    }
}

如果您有任何疑问,请告诉我们。

关于java - 在文件中的点之间绘制一条线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14656734/

相关文章:

PHP readfile() 导致损坏的文件下载

android - 获取对话框中 View 相对于父 View 的位置

java - 在Windows上直接获取Java可访问性

c - 哪些事件会导致 ferror 返回非零值?

c++ - 如何在不使用boost的情况下创建目录?

java - GraphViz 的纯 Java 重新实现?

opencv - 如何确定使用openCV绘制的椭圆的中心坐标?

java - 如果位数不固定,如何限制小数点后的位数

java - 有限制的 Hibernate 实体

java - getView 从未因我之外的原因被调用