简体   繁体   English

在文件中的点之间画一条线

[英]Drawing a line between points in a file

I need to draw lines between in java from a file that is formatted the following way: 我需要通过以下格式的文件在java中画线:

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

I think I am going to have to set up two arrays and then somehow add the values like that but I am completely lost and really need some guidance. 我想我将不得不设置两个数组,然后以某种方式添加类似的值,但是我完全迷失了,确实需要一些指导。 Any help would be appreciated! 任何帮助,将不胜感激! The code below is what needs to be modified to draw lines. 下面的代码是需要修改以绘制线条的代码。 Right now it just plots the points. 现在,它只是绘制点。

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

    }

}

Here is code that will help you how to read file and how to extract values from there. 这里的代码将帮助您如何读取文件以及如何从中提取值。 This is first you have to make it correctly before doing any other stuff. 首先,您必须先正确进行操作,然后再进行其他操作。

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

    }
}

EDIT: --> if you want to draw line between two points. 编辑: ->如果要在两点之间画线。 you have to make use of drawline() method. 您必须使用drawline()方法。 It should go something like below. 它应该像下面这样。 I am also giving you link how to make line is java. 我还给你链接如何使行是java。

1. Graphic drawLine() API 1.图形drawLine()API

2. How to draw lines in Java 2.如何用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);

    }
}

If you have any question let us know. 如果您有任何问题,请告诉我们。

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

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