Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: I have a problem to load a set of points(x, y)

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    2
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default I have a problem to load a set of points(x, y)

    Hi guys
    I will be very appreciate if somebody could tell me what is wrong with my code? Namely I am trying to develop a simple applet that has button "Load" to load set of points specified by x,y coordinates from a file and display these point in applet window. I store these point as ListArray everything seems to working fine apart displaying these point in window as specified by its coordinates. Could anybody help me with this issue, thanks in advance and the code is a below:
    P.s I only attached the part of the code, If anybody can only suggest to load these values on display it on screen
    public class Project extends JApplet implements ActionListener {
    	private JPanel contentPane;
    	private JPanel buttonPane;
    	private JButton load;
    	private RectangleArea rectangleArea;
    	private PointSet pointSet = new PointSet();
    	private Point pTemp = null;
    	private boolean showMax = false;
     
            //This is my code to load data from data.txt 
    	public Project() {
    		init();
     
    		try {
                ArrayList<Integer> column1 = new ArrayList<Integer>();   // Defining an integer Array List
                ArrayList<Integer> column2 = new ArrayList<Integer>();   // Defining an integer Array List
     
                Scanner myfile = new Scanner(new FileReader("E:\\app\\data.txt")); // Reading file using Scanner
     
                while (myfile.hasNext()) {          // Read file content using a while loop
                    column1.add(myfile.nextInt());      // Store the first integer into the first array list
                    column2.add(myfile.nextInt());    // Store the next integer into the second array list
                }
                    System.out.println("column 1 elements are:\n" + column1);  
                    System.out.println("column 2 elements are:\n" + column2);
     
     
                myfile.close(); // close the file
     
    		}catch (Exception e) {   
                e.printStackTrace(); 
    		}
     
    	}
     
    	public void init() {
    		enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    		contentPane = (JPanel) this.getContentPane();
    		buttonPane = new JPanel();
    		contentPane.setLayout(new BorderLayout());
    		GridLayout buttonLayout = new GridLayout(0,9);
    		buttonPane.setLayout(buttonLayout);
    		buttonPane.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
    		random = new JButton("Load");
    		load.addActionListener(this);
    		load.setToolTipText("Display data");
    		buttonPane.add(Box.createHorizontalStrut(10));
    		buttonPane.add(new JLabel("",SwingConstants.CENTER));
    		buttonPane.add(new JLabel("",SwingConstants.CENTER));
    		buttonPane.add(load);	
     
    		buttonPane.add(new JLabel("",SwingConstants.CENTER));
    		buttonPane.setAlignmentX(CENTER_ALIGNMENT);
    		rectangleArea = new RectangleArea(this);
    		MouseListener listener = new MouseListener(this);
    		rectangleArea.addMouseListener(listener);
    		rectangleArea.setAlignmentX(LEFT_ALIGNMENT);
    		contentPane.add(rectangleArea,BorderLayout.CENTER);
    		contentPane.add(buttonPane,BorderLayout.SOUTH);
    	}
     
    	public void actionPerformed(ActionEvent e){
    		 if (e.getSource() == load) {
    				//isOK=true;
    				pointSet.clear();
    		    //[B]I can set a single point
                        //But how to load these values from my txt fille [/B]		 
    	            Point p = new Point();
    				p.setX(44);
    				p.setY(22);
    				pointSet.addPoint(p);
    				this.addPoint(p);
    				this.setPTemp(p);
    		 }
    		 getRectangleArea().repaint();
    		 }
     
    	public void addPoint(Point p) {
    		this.pointSet.addPoint(p);
    	}
     
    	public PointSet getPointSet() {
    		return pointSet;
    	}
     
    	public void setPointSet(PointSet pointSet) {
    		this.pointSet = pointSet;
    	}
     
    	public JButton getMaxD() {
    		return maxD;
    	}
     
    	public void setMaxD(JButton maxD) {
    		this.maxD = maxD;
    	}
     
    	public boolean isShowMax() {
    		return showMax;
    	}
     
    	public void setShowMax(boolean showMax) {
    		this.showMax = showMax;
    	}
     
    	public Point getPTemp() {
    		return pTemp;
    	}
     
    	public void setPTemp(Point temp) {
    		pTemp = temp;
    	}
     
     
    	public RectangleArea getRectangleArea() {
    		return rectangleArea;
    	}
     
    	public void setRectangleArea(RectangleArea rectangleArea) {
    		this.rectangleArea = rectangleArea;
    	}
    }


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: I have a problem to load a set of points(x, y)

    This is probably not it but why do you have // twice in there?

    er("E:\\app\\data.txt"))

    Also, I think that \ is an escape character.

    Are you sure you didn't mean

    "E:/app/data.txt"

    ?

    Also, if anything goes wrong, your whole code segment will go into catch block.

    I could be wrong, but I think that only the definition of the Scanner would need the try block around it.

    What is RectangleArea? What is PointSet?

    Also, Point has no setX() and setY() methods. It has a setLocation(int x, int y).

    I cannot find the variable random being declared anywhere (and neither can the compiler).

    You cannot instantiate MouseListener, it is abstract. You either had to add a MouseListener like this

    item.addMouseListener(new MouseListener()

    {

    // Mouse Listener methods

    });

    or you have to have the class that contains the item your adding the mouselistener to implement it like so

    public class YourClass extends BlaBlaBla implements MouseListener
    {

    ......

    item.addMouseListener(this);

    .......

    // Mouse Listener methods must be defined in class.

    Do you have a RectangleArea constructor that takes a Project object as a param?

    rectangleArea = new RectangleArea(this);
    Last edited by javapenguin; May 25th, 2012 at 09:25 PM.

  3. The Following User Says Thank You to javapenguin For This Useful Post:

    mikos (May 26th, 2012)

  4. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: I have a problem to load a set of points(x, y)

    I store these point as ListArray everything seems to working fine apart displaying these point in window as specified by its coordinates
    Not sure if post #2 helps or confuses....but you should define what you mean by "displaying these points in window". Do you wish to draw these point on a JPanel? Are you familiar with java 2D? Trail: 2D Graphics (The Java™ Tutorials)

  5. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: I have a problem to load a set of points(x, y)

    What's wrong with my post? I wasn't wrong about the MouseListener, the fact that there is no setX() or setY() in the Point class, or, as far as I know, that \ is an escape character.

  6. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: I have a problem to load a set of points(x, y)

    Quote Originally Posted by javapenguin View Post
    What's wrong with my post? I wasn't wrong about the MouseListener, the fact that there is no setX() or setY() in the Point class, or, as far as I know, that \ is an escape character.
    Given past experience, I hesitate to even spend my time answering your question as my response more often than not falls on deaf ears. But if you insist...the original poster's code does not reference what MouseListener or Point are. There are no imports, nor class references in the posted code - you are guessing that these refer to the J2SE classes and not classes in another package with the same class name - the original poster never mentioned a compile time error, which should raise suspicion. And yes, \ is an escape character, meaning on some systems it is necessary to define a file path - again, the original poster never mentioned a problem with this part of the code.

    I hope that your attempts to try and answer non-existent questions help - its up to the original poster to decide (and I hope they decide in your favor). Nevertheless, your response doesn't try to answer the underlying problem posted
    everything seems to working fine apart displaying these point in window as specified by its coordinates. Could anybody help me with this issue
    Last edited by copeg; May 26th, 2012 at 12:44 AM.

  7. The Following User Says Thank You to copeg For This Useful Post:

    mikos (May 26th, 2012)

  8. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: I have a problem to load a set of points(x, y)

    Also, does the OP want the values of the points to be displayed on the screen or the points themselves, or both?

    I mean, does he/she want:

    ex.
    1.) Point 1: x = 100 y = 200
    Point 2 : x = 900 y = 500
    ........

    or

    2.) Do they want a point drawn on the screen? I can't find a drawPoints() method in the Graphics class and wouldn't know what to say as to how to do that.

    Perhaps the Graphics drawOval() method with a height and width of like 1 or 2 or something like that.

    The OP wasn't that clear as to what problem they were having with the displaying of the points.

    1.) That they didn't know how to do it and were wondering how to
    2.) That they tried something but it wasn't working.

  9. The Following User Says Thank You to javapenguin For This Useful Post:

    mikos (May 26th, 2012)

  10. #7
    Junior Member
    Join Date
    May 2012
    Posts
    2
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: I have a problem to load a set of points(x, y)

    Thank you for all the replies, but finally I managed to find a way to draw my points on JPanel. I didn't know how to load my points from my both ArrayList as a point's coordinates (x,y) and draw them on JPanel. I have all the classes but I was stuck with getting the x from one column1 and then y from column2 and to put them on the screen. Thank you again

Similar Threads

  1. Get points along a line in Java Swing
    By libinthomas in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 16th, 2012, 07:42 AM
  2. Distance between 2 points
    By captain in forum Java Theory & Questions
    Replies: 3
    Last Post: February 22nd, 2012, 12:53 AM
  3. ArrayList Plotting Points
    By basketball8533 in forum Collections and Generics
    Replies: 1
    Last Post: October 18th, 2011, 03:59 PM
  4. I like plotting points :) HI!
    By fractalorbit in forum Member Introductions
    Replies: 1
    Last Post: September 5th, 2011, 10:49 AM
  5. Getting all points in line
    By Mike in forum Java Theory & Questions
    Replies: 7
    Last Post: September 13th, 2010, 11:59 AM