0

I need to draw lines between in java from a file that is formatted the following way:

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

    }

}
JLott
  • 1,818
  • 3
  • 35
  • 56
  • I have never messed with adjacency matrixes before and I have gotten so confused to the point of giving up. I will post my code of doing it a different way. I wanted to do an adjacency matrix so I could try to implement an algorithm I found. – JLott Feb 01 '13 at 23:24
  • Now where exactly you are getting problem? – Smit Feb 01 '13 at 23:26
  • 1
    I don't know how to put a file with that format into an adjacency matrix. – JLott Feb 01 '13 at 23:27
  • It looks like you are not reading file correctly and extracting those values from file. – Smit Feb 01 '13 at 23:29
  • I updated the answer according to your need. I hope this will help you to sort your issue out. Good Luck – Smit Feb 04 '13 at 18:42

1 Answers1

1

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. It should go something like below. I am also giving you link how to make line is 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);

    }
}

If you have any question let us know.

Community
  • 1
  • 1
Smit
  • 4,685
  • 1
  • 24
  • 28
  • I edited my question so you might understand what I am struggling with. I don't know how I should modify my program to draw lines between the points in the file. Right now the program just plots the points. – JLott Feb 02 '13 at 02:50
  • Change from enhanced for loop to a normal for loop. Then get the last used (i-1) point from the list of points. Then, draw line from p1.x, p1.y to p2.x, p2.y. –  Feb 04 '13 at 18:15
  • @Legend She is student and I am just showing her a way how it can be accomplished. I think its not good to give OP everything ready. She needs to learn. However the point you made I am agree with that. Thanks. – Smit Feb 04 '13 at 18:37
  • Thank you for helping :) I also like to learn which is why I didn't ask the exact assignment which was to draw lines based on nearest neighbor ;) I just needed the basics of connecting the lines so I could modify it how I needed. – JLott Feb 05 '13 at 18:51