Results 1 to 10 of 10
  1. #1
    zeena123 is offline Member
    Join Date
    Mar 2012
    Location
    UK - Essex
    Posts
    5
    Rep Power
    0

    Default How to compare resuts

    Hi,

    I am using NetBeans to create a small project on the side. The logic behind is that there is a face embedded in the picture which the user or player has to click on, and the x and y coordinates should be stored to a .txt file and used for comparison later. Before they do this, the experimentor must click on the face to set the coordinates of the face first. I have done this by using rectangles, so basically the coordinates of where the face on the image is already set. However, for some reason, it is allowing the players to click anywhere and prints that a face has been found, even when it has not.
    I need some help trying to figure out how to change my code so that it prints out the x and y coordinates of where both players clicked after all clicks have been made on all images, to a .txt file.

    this is my coding so far:
    Java Code:
    int clicked = 0;
        public ArrayList<Point> player1_clicks =  new ArrayList<Point>();
        public ArrayList<Point> player2_clicks =  new ArrayList<Point>();
        
        public ArrayList<Rectangle> facesBounds = new ArrayList<Rectangle>();
         facesBounds.add(new Rectangle(437,360,459,398));
    
    if (currentRect != null) {
                //Draw a rectangle on top of the image.
                g.setXORMode(Color.white); //Color of line varies
                                           //depending on image colors
                g.drawRect(rectToDraw.x, rectToDraw.y,
                           rectToDraw.width - 1, rectToDraw.height - 1);
            }
    
    public void mouseClicked(MouseEvent e){
            clicked++;
            //Point2D p = new Point2D((int)e.getX(), (int)e.getY()); 
            Point p = new Point((int)e.getX(), (int)e.getY());
        
            if(clicked%2==0){
                   player2_clicks.add(p);
            }else{
                   player1_clicks.add(p);
            }
    for(Rectangle rec : facesBounds){
                for(Point pt : player1_clicks){
                    if(rec.contains(pt)){
                        System.out.println("p1 found a face");
                    }
                }
            }
            
            for(Rectangle rec : facesBounds){
                for(Point pt : player2_clicks){
                    if(rec.contains(pt)){
                        System.out.println("p2 found a face");
                    }
                }
            }
        }
    Last edited by Norm; 03-28-2012 at 07:39 PM. Reason: added code tags

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,416
    Rep Power
    25

    Default Re: How to compare resuts

    code so that it prints out the x and y coordinates
    Where are the x,y values available?
    Where do you want to print them?
    Are you asking how to save the data available in one method in a class variable so that another method can see it?
    To write text data to a file, you could use the PrintWriter class.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    zeena123 is offline Member
    Join Date
    Mar 2012
    Location
    UK - Essex
    Posts
    5
    Rep Power
    0

    Default Re: How to compare resuts

    The x,y values come from when the mouse is clicked on the image with a getX and getY method. At the moment, the coordinates are printed in the output compiler, however i would like to print them in a message window. Is there a way to do this?
    For the last question, this would be an option, as once the user clicks and retrieves the coordinates, they should be stored so that they can be compared using another method.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,416
    Rep Power
    25

    Default Re: How to compare resuts

    would like to print them in a message window
    The JOptionPane class could be used to show them in a window.
    coordinates, they should be stored
    You could create a class object like a Point to hold the two values and save that object in a list, say an arraylist.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    zeena123 is offline Member
    Join Date
    Mar 2012
    Location
    UK - Essex
    Posts
    5
    Rep Power
    0

    Default Re: How to compare resuts

    Quote Originally Posted by Norm View Post
    The JOptionPane class could be used to show them in a window.

    You could create a class object like a Point to hold the two values and save that object in a list, say an arraylist.
    Sorry for the delay in response, this is a side project for me and I have just got back to working on it.
    Sorry, i did not quite understand tyour response, but i tried to work on what i did understand from it. So I have used rectangles to create 'FaceBounds' so they get the coordinates of the rectangle around the image in the picture i am trying to get the coordinates for and have used your suggestion and applied to a method i posted below. However, for some reason i cannot see the actual coordinates any more, is this because i have changed the print line?
    Sorry for sounding a bit dense in advanced i am fairly new to this!

    Java Code:
     public ArrayList<Point> player1_clicks =  new ArrayList<Point>();
        public ArrayList<Point> player2_clicks =  new ArrayList<Point>();
        
        public ArrayList<Rectangle> facesBounds = new ArrayList<Rectangle>();
        
         public void mouseClicked(MouseEvent e){
            clicked++;
            //Point2D p = new Point2D((int)e.getX(), (int)e.getY()); 
            Point p = new Point((int)e.getX(), (int)e.getY());
        
            if(clicked%2==0){
                   player2_clicks.add(p);
            }else{
                   player1_clicks.add(p);
    // you can change this to any EVEN number, 2 means each player gets 1 chance to guess the face
    		//4 means each player gets two chances to guess the face, 6 means each player gets 3 goes etc etc.
    		if(clicked%2==0){
    		
    			for(Rectangle rec : facesBounds){
    				for(Point pt : player1_clicks){
    					if(rec.contains(pt)){
    						System.out.println("p1 found a face");
    					}
    				}
    			}
    			
    			for(Rectangle rec : facesBounds){
    				for(Point pt : player2_clicks){
    					if(rec.contains(pt)){
    						System.out.println("p2 found a face");
    					}
    				}
    			}
    			
    			player1_clicks =  new ArrayList<Point>();
    			player2_clicks =  new ArrayList<Point>();
                            clicked=0;
    		
    		}
            
        }
    
            }

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,416
    Rep Power
    25

    Default Re: How to compare resuts

    i cannot see the actual coordinates any more
    Please post the output from the program that shows what you are talking about.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    zeena123 is offline Member
    Join Date
    Mar 2012
    Location
    UK - Essex
    Posts
    5
    Rep Power
    0

    Default Re: How to compare resuts

    Okay. This is the output in the compiler:

    init:
    Deleting: C:\Users\zina\Desktop\FaceInACrowd\build\built-jar.properties
    deps-jar:
    Updating property file: C:\Users\zina\Desktop\FaceInACrowd\build\built-jar.properties
    Compiling 1 source file to C:\Users\zina\Desktop\FaceInACrowd\build\classes
    compile:
    run:
    p1 found a face
    p2 found a face

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,416
    Rep Power
    25

    Default Re: How to compare resuts

    Can you explain what the problem is with that output?
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    zeena123 is offline Member
    Join Date
    Mar 2012
    Location
    UK - Essex
    Posts
    5
    Rep Power
    0

    Default Re: How to compare resuts

    Initially, i had the coordinates where the mouse was being clicked being printed with the print line 'p1 found a face'. It no longer does that after i added the print statement.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,416
    Rep Power
    25

    Default Re: How to compare resuts

    If you want something different printed out, change the println statement to print out what you want to see.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. treeset compare
    By samaravadhi in forum New To Java
    Replies: 6
    Last Post: 09-07-2011, 03:59 PM
  2. Compare not working
    By pahiker in forum New To Java
    Replies: 3
    Last Post: 02-13-2011, 05:49 PM
  3. how to compare icons
    By panic in forum New To Java
    Replies: 9
    Last Post: 05-21-2009, 08:16 AM
  4. Compare value in array
    By cassysumandak in forum New To Java
    Replies: 5
    Last Post: 04-04-2009, 11:43 PM
  5. Compare 2 XML
    By Peter in forum XML
    Replies: 1
    Last Post: 07-05-2007, 02:58 AM

Posting Permissions

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