Results 1 to 15 of 15

Thread: Veriable Help

  1. #1
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default Veriable Help

    Im doing a java program that has a math function for finding the line of best fit. I would like to know what it is that i do to alow me to add as many veriable i want. i was each x,y pair to be a veriable, but i dont know how to make my program so i can add as many points as i want. how do i do it?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default

    So with this java.awt.Point, i could do 10 points for one line of fit, but then the next time i could do 20,000 points of i wanted? i will reaserch this java.awt.point functions.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You are researching the wrong class. Point will allow you to have objects with x and y fields. It's the ArrayList that allows you to work with a variable-sized collection of objects.

  5. #5
    DC200 is offline Member
    Join Date
    Dec 2008
    Posts
    49
    Rep Power
    0

    Default

    Here's an example of what Fubarable is telling you about.

    Java Code:
    ArrayList<java.awt.Point> points = new ArrayList<java.awt.Point>();
    
    for (int i=0; i<10; i++) {
       points.add(new java.awt.Point());
    }
    
    //etc...
    You can then access each point by directly referencing its index using ArrayList's "get(int index)" method.

    ArrayList (Java 2 Platform SE v1.4.2)
    Last edited by DC200; 10-13-2010 at 05:47 PM.

  6. #6
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default

    Ok, i understand how an array works, but my question was really, how do i add to the array. i curently have a window with a graph and to text fields at the bottom, one x, and one y. when i click the mouse, the cordanates of where i clicked are printed into the textfield. ultimately, i want to read the x and y from the text fields and add them to an array list. but lets do something simpler, i would like to have a user input, where i can type a number, then that number is added to an array list. and i can keep typing numbers over and over and over, untill say, i press enter, at which point i print all the numbners from my array.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Jcbconway View Post
    Ok, i understand how an array works, but my question was really, how do i add to the array.
    No, you don't understand as it's not an array. Again, it's an ArrayList. A post just above yours shows how to add to this list. If you need to understand more, please google for some tutorials to learn about this.

    i curently have a window with a graph and to text fields at the bottom, one x, and one y. when i click the mouse, the cordanates of where i clicked are printed into the textfield. ultimately, i want to read the x and y from the text fields and add them to an array list. but lets do something simpler, i would like to have a user input, where i can type a number, then that number is added to an array list. and i can keep typing numbers over and over and over, untill say, i press enter, at which point i print all the numbners from my array.
    Then please check the tutorials and give it a try. If after you've done this your code doesn't work, please feel free to come on back and post it with details on how it's not working.

    Luck!

  8. #8
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default

    No i do understand. My question, wich you seem to be not catching is, how to i add, an infinate amount of user inputes, in his for statement, would i do a userinpute code that adds what i type to an aray? and how would i stop adding to that arraylist, and stop adding to the list.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Jcbconway View Post
    No i do understand. My question, wich you seem to be not catching is, how to i add, an infinate amount of user inputes, in his for statement, would i do a userinpute code that adds what i type to an aray? and how would i stop adding to that arraylist, and stop adding to the list.
    This is controlled by your event listener. If you want to add something to the ArrayList on button press, then one object is added in the button ActionListener's actionPerformed method. If you want to add coordinates on mouse press, then the MouseListener's mousePressed method will add. And you likely wouldn't use a for loop in these examples.

    You control it all.
    Last edited by Fubarable; 10-13-2010 at 11:54 PM.

  10. #10
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default

    Yes, but with the add(x); to the arraylist, would i not need to ever add a cap to the arraylist when say i push enter i just would use what is curently in the array ist?

  11. #11
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    There's no need to cap the array list. Just add a point every time the user clicks (or remove a point if the user clicks on an existing etc etc. You can add lots of functionality.)

    Then when the user clicks "Calculate" the line of best fit could be shown. Or that line could be recalculated with every change in the data. But in any case you are not obliged to cap the list in any way.

    Of course, you may want to limit the user to say a million points. That's a different question. In that case you would check the size of the array and only add the point if the size didn't exceed your preimposed limit.

  12. #12
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default

    I see what your saying. So, i would be actively adding to the list, and by hitting enter (or like you said constantly checking line of fit and have an updating line) it would matter that i can still add to the list, its just going to use what is currently in the list.

  13. #13
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default

    Ok, so this is a little program i just wipped up, it makes a window, and a panel, and adds a mouseadapter to the panel, so where ever you click, it returns the x,y cordnates. i am not sure how to add an arraylist here, and how to go about adding to the arraylist.

    Main:

    Java Code:
    package arraylistener;
    
    import javax.swing.*;
    public class Main extends JFrame{
        JFrame f1;
        public void frameSetup(){
            f1 = new JFrame();
            Window w = new Window();
            w.windowSetup();
            add(w);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(200,200);
            setResizable(true);
            setLocationRelativeTo(null);
            setVisible(true);
        }
        public static void main(String[] args) {
            Main m = new Main();
            m.frameSetup();
        }
    }
    Window:
    Java Code:
    package arraylistener;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    
    public class Window extends JPanel {
        JPanel p1;
    
        public void windowSetup() {
            addMouseListener(new mAdapter());
            p1 = new JPanel();
            setSize(200, 200);
            setBackground(Color.yellow);
            setFocusable(true);
        }
        public class mAdapter extends MouseAdapter{
            
            public void mousePressed(MouseEvent e){
                System.out.println(e.getX() + " " + e.getY());
    
                
                
            }
    
        }
    }
    some help would be nice here haha (:
    you guys are soo helpful and thank you!

  14. #14
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I hope you don't take this the wrong way, but the kindest thing we can do to those classes is shoot them. But, before we do, I might remark that you use a pattern like:

    Java Code:
    public class Whatever extends Something {
        Something s1;
        // etc
    }

    In general, I don't think you need to do both extend and have a variable of the type you are extending.

    As for Main the following is lifted straight out of Oracle's Tutorial:

    Java Code:
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame("Best fit");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new PointsPanel());
                frame.pack();
                frame.setVisible(true);
            }
        });
    }

    The whole tutorial is well worth reading. Much of it can be skipped at first reading, except to note that it's there when you later want to use Borders, TabbedPanes, Menus etc.

    PointsPanel is what I am calling what you called Window - as that latter name has a special meaning within gui programming.

    Now PointsPanel itself:

    Java Code:
    import /* as required */ //(1)
    
    public class PointsPanel extends JPanel {
        public PointsPanel() {  //(2)
            setPreferredSize(new Dimension(200, 200));
            addMouseListener(myMouseListener); //(3)
            setBackground(Color.yellow);
            setFocusable(true);
        }
    
        private MouseAdapter myMouseListener  = new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                System.out.println(e.getX() + " " + e.getY());
            }
        }
    }

    (1) A very good habit to get into is to import all and only the classes you actually use. This is a real pain to begin with but pays off as you (of necessity) become better aquainted with the packes that hold the classes you use often, and you end up with somewhat "self documenting" code.

    (2) No init(), just a plain old constructor.

    (3) Again, note the name change. I've called it myMouseListener because it's ... my mouse listener. And note how you don't have to explicitly define a new class and then instantiate it: you can declare it as a (private) member variable whose type is given as the adapter.

    So on to your questions: I think it's pretty clear where you would add the point to the array. (say, if not). I mean there is only one place where you have enough information to invoke "toAdd=new Point(x,y)". And the array list has an add() method to allow you to do that. The real question is where would you declare this array list? The answer is: as another private member variable of the PointsPanel class.

    (Full disclosure: I wrote something like this once. But I was interested in more than just the points, I also wanted the "residuals" ie the distance from the points to the line. Also I wanted to make lots of different displays: tables, histograms, stem and leaf plots. The details don't matter, but the point is I had to have something much more elaborate than a simple array list for my data. And as a result I did not put the data in the class that displayed the points. However for your simple example you might as well: just recognise that if things get complex you might want the data to be in a class or classes of its own)

    One final thing is that you haven't considered the business of painting! The section in the tutorial on performing custom painting is very relevant.
    Last edited by pbrockway2; 10-14-2010 at 02:49 AM. Reason: corrected code - it's worth noting that it hasn't compiled, much less tested...

  15. #15
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default

    I understate paint methods, and what your getting at with the classes. BUT, my hole point is to get the array list working, in a concept, not necessarily an effective program.

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
  •