簡體   English   中英

在文件中的點之間畫一條線

[英]Drawing a line between points in a file

我需要通過以下格式的文件在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()方法。 它應該像下面這樣。 我還給你鏈接如何使行是java。

1.圖形drawLine()API

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

    }
}

如果您有任何問題,請告訴我們。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM