I have been struggling with this program for much longer than I should be. I am teaching myself java and I was wanting to try to knock out a few of the basics by reading in a file formatted like:
Code:
3 //Number of lines in the file
14 15
43 23
34 12
then I wanted to take those points and draw a line in between them all. I think I can do it by creating an int[][] array and then add the points to where I can eventually just use drawLine(x1,y1,x2,y2). There may be a much better way to do this, but I am completely stuck. Any help would be appreciated!
Code:
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 = "file.txt";
public double[][] myPoints;
public static void main(final String[] args){
try{
final BufferedReader br = new BufferedReader(new FileReader(new File(FILE)));
points = new myPoints[Integer.parseInt(br.readLine())];
int i = 0;
int xMax = 0;
int yMax = 0;
while(br.ready()){
String line;
for (int i = 0; (line = br.readLine()) != null; i++) {
for (int i = 0; i <= even.length; i++) {
final String[][] split = even.split(" ");
final int x = Integer.parseInt(split1[0]);
final int y = Integer.parseInt(split1[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);
//This was just when I was plotting the points, but my drawline stuff will go here.
for(final Point p : points){
g.fillRect((int) p.getX(), (int) p.getY(), 2, 2);
}
}
}
}