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 3 of 3

Thread: contains method not working?

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Location
    London
    Posts
    15
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default contains method not working?

    Hello, I'm making a program where when the user clicks inside of the polygon it'll display a message saying they clicked inside of it and a different message for when they click out of it, I've done this using an if statement. I just found out about the contains method today and tried using it within the if statement, but I don't believe it is working because it seems to ignore the first condition completely and just go for the else part so it always comes with the message that they clicked outside of it even if they are clicking in. I might try to do something different but I would like to know why its not working, could please someone tell me?

    Edit: Sorry I forgot to mention that the program is also supposed to print the co-ordinates of where they clicked in the message too

    Here is my code:

    import javax.swing.* ;
    import java.awt.* ;
    import java.awt.event.* ;
     
    public class KanjiInterface extends JFrame {
     
    	JMenuBar menuBar ;
    	JMenu menuSession ;
    	JMenuItem subNewGame, subLoadGame ;
    	MyComponent graphArea ;
    	Color x = Color.blue ;
    	//Boolean inPoly = false ;
    	int xco[]={100,150,50} ;
    	int yco[]={100,150,150} ;
    	Polygon poly = new Polygon(xco,yco,3) ;
     
    	KanjiInterface(){
    		//Creates the window
    		super("Kanji Game") ;
    		setLocation( new Point(100, 100) ) ;
    		//this.getContentPane().setBackground( Color.white ) ;
    		setSize( 600, 600 ) ;
    		setResizable( true ) ;
     
    		//Creates the Menu Bar along with sub-menus
    		menuBar = new JMenuBar() ;
    		menuSession = new JMenu("Session") ;
    		subNewGame = new JMenuItem("New Session") ;
    		subLoadGame = new JMenuItem("Load Session") ;
    		menuBar.add(menuSession) ;
    		menuSession.add( subNewGame ) ;
    		menuSession.add( subLoadGame ) ;
    		this.setJMenuBar(menuBar) ;
     
    		//Create Panel for graphics
    		graphArea = new MyComponent() ;
    		graphArea.setBackground(Color.WHITE) ;
    	 	add(graphArea, BorderLayout.CENTER) ;
    	 	setVisible( true ) ;
     
    	 	//Adding listener
    	 	this.addMouseListener(new MouseCatcher ()) ;
     
    	}
     
    	class MyComponent extends JPanel{
    	public void paintComponent(Graphics g){
    		super.paintComponent(g) ;
    	    g.setColor(x) ;
    		//poly.addPoint(100,100) ;
    	    //poly.addPoint(150,150) ;
    	    //poly.addPoint(50,150) ;
    	    g.fillPolygon(poly) ;
     
     
    	}
    	}
     
    	public class MouseCatcher extends MouseAdapter {
     
    		public void mouseClicked(MouseEvent e)
    		{
     
        		int xpos = e.getX();
    			int ypos = e.getY();
    			if(poly.contains(xpos,ypos)){
    			    System.out.println("You clicked on "+xpos+","+ypos) ;
    			    x = Color.red ;
    				//inPoly = true ;
    			}
    			/*
                if(inPoly){
    			    System.out.println("You clicked on "+xpos+","+ypos) ;
    			    x = Color.red ;
                }
                */
                else{
                	System.out.println("You didn't click on "+xpos+","+ypos) ;
                }
     
    		    repaint() ;
     
    		}
     
    	}
    }

    Note: My main is in a different class (using Eclipse).
    Last edited by DudeJericho; April 19th, 2011 at 12:29 PM.


  2. #2
    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: contains method not working?

    How do the coordinates printed differ from what you'd expect? Quickly glancing at the code I'd suspect the behavior is because the listener is added to the JFrame and not the JPanel itself, thus the coordinates retrieved are relative to the JFrame. Carefully looking at those coordinates would easily test this hypothesis.

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

    DudeJericho (April 19th, 2011)

  4. #3
    Junior Member
    Join Date
    Feb 2011
    Location
    London
    Posts
    15
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: contains method not working?

    Thank you! That was the problem! I'm quite new to Java so I doubt I would have noticed that..

Similar Threads

  1. Can you pass parameters from one method into another method?
    By u-will-neva-no in forum Java Theory & Questions
    Replies: 2
    Last Post: April 14th, 2011, 07:46 AM
  2. Help with toString method and an addObject method?
    By Camisado in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 12th, 2011, 07:00 AM
  3. Can i call init() method in destroy method.?
    By muralidhar in forum Java Servlet
    Replies: 1
    Last Post: October 22nd, 2010, 11:18 AM
  4. Cannot seem to get this working
    By OttawaGuy in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 28th, 2010, 03:41 PM
  5. Replies: 4
    Last Post: January 27th, 2009, 12:03 AM