Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Feb 2013
    Thanked 0 Times in 0 Posts

    Adding values to an int myPoints[][] array

    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:

       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!

        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){
                    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;
        				 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));
                } catch (final Exception e){
            public static class Panel extends JPanel {
                public void paintComponent(final Graphics g){
        //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);

  2. #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Saskatoon, Saskatchewan
    Thanked 2,668 Times in 2,637 Posts
    Looks to me that you have jumped into the deep end of the pool before trying out the shallow end. There's a lot here you are wanting to do, but you're not familiar with the basics before continuing into the intermediate functionality.
    Start simple, then move to complex. The idea you have is right, but its implementation is incorrect. You have datatype mismatches here, and incorrect usage of object properties.

    Here's what I suggest. Start without worrying about a visual rendering component. Drop all associations with gui components, and work it off of a command line app. The goal is to get the data from the file, and into an array or collection. Choose if the array should be primitive like a double[][], or an array of object such as Point. I'd suggest using a multidimensional primitive since that will kill two birds with one stone. Once you have the data in the array, then iterate it using a for or foreach style for syntax and just a tabular style output. When you have this data, then you can move into gui components.
    Watch for your datatypes and property handling. myPoints isn't a datatype, so it cannot be instantiated, and points isn't defined as any datatype. Watch your scope as well, nested loops cannot use the same control variable (ie: i), since i will be altered in both the inner and outer loops. Use different variables for control (i, j are typically good options). I'd suggest using split on a simple space character, and then using trim on the strings provided.
    When you can get the data into the array properly, then you can look at the gui aspect. I'd extend a JPanel directly and accept the data collection into the constructor OR give it a file to read itself.
    If you don't follow the simple to complex approach when learning a language, you'll always end up in the situation of "its not working but I don't know what is wrong". This is why you need to get a solid understanding of each part one at a time in order to diagnose what is wrong. That way you can look at something and say "the file is correctly read and stored, but its not rendered on the panel properly" so that gives you a spot to debug instead of being unsure of what in particular isn't working.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts