Results 1 to 10 of 10
  1. #1
    Ara1992 is offline Member
    Join Date
    Dec 2009
    Posts
    8
    Rep Power
    0

    Default Java shape detection

    Hey guys,

    So basically I have 5 squares and 4 of them has a little circle ( ellipse ) inside.

    So what I want is when a user clicks a square containing a circle. I need it to move to another square.

    But I am having problems early. So far, I managed to find in which square the user has clicked. But I am trying to see if the square contains a cercle.

    But it doesnt work for some reason...

    Here is the code:
    Java Code:
                    System.out.println(square+"-"+point);
                    System.out.println(event.getX()+"-"+cercle[point].getX());
                    System.out.println(event.getY()+"-"+cercle[point].getY());
                    System.out.println(cercle[point].contains(250+(square*100),250,100,100));
    And the output is:
    0-0
    289-300.0
    273-300.0
    false
    But it supposed to return "true" because the square I traced with the .contains covers the cercle...

    Thanks,
    Ara

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

    Default Re: Java shape detection

    I don't think we can guess what your code is doing without seeing the pertinent parts.

  3. #3
    Ara1992 is offline Member
    Join Date
    Dec 2009
    Posts
    8
    Rep Power
    0

    Default Re: Java shape detection

    Ok well here is the pain method and the Event method:

    Java Code:
        public void paint(Graphics g) {
    
            // Appeler la méthode paint de la superclasse.
            super.paint(g);
    
            // Créer la 2D par le remplacement du type de g en Graphics2D.
            Graphics2D g2d = (Graphics2D) g;
    
    
            // Tracer autant de rectangles demandes par l'utilisateur.
            rectangle = new Rectangle.Double[5];
    
            for (int x = 0; x < 5; x++) {
                rectangle[x] = new Rectangle2D.Double(250 + (x * 100), 250, 100, 100);
                g2d.draw(rectangle[x]);
            }
    
            // Tracer autant de cercles necessaire.
            cercle = new Ellipse2D[4];
            int y = 0;
    
            for (int x = 0; x < 5; x++) {
                g2d.setPaint(Color.black);
                if (x == 2) {
                    g2d.setPaint(Color.white);
                    x = 3;
                } else if (x == 4) {
                    g2d.setPaint(Color.white);
                }
                cercle[y] = new Ellipse2D.Double(300 + (x * 100), 300, 10, 10);
                g2d.fill(cercle[y]);
                y++;
            }
    
        }
    
    
    
        private class GestEvt extends MouseAdapter{
            public void mouseClicked(MouseEvent event){
               int x = event.getX();
               int y = event.getY();
               int square;
               int point;
               
               // Creer la zone du rectangle
               if(((y>249 && y<351) && (x>250 && x<(250+(5*100))))){
                    square = (x-250)/100;
                    point = square;
                    if(square == 4){
                        point = 3;
                    }
                    
                    // Verifier s'il contient un point
                    System.out.println(square+"-"+point);
                    System.out.println(event.getX()+"-"+cercle[point].getX());
                    System.out.println(event.getY()+"-"+cercle[point].getY());
                    System.out.println(cercle[point].contains(250+(square*100),250,100,100));
                    
              }
            }
        }

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

    Default Re: Java shape detection

    So you've got an array of Ellipse2D, cercle. Perhaps you want to use a for loop in your mousePressed method (don't use mouseClicked), and iterate through all the Ellipses to see if their bounds contains the mouse location.

  5. #5
    Ara1992 is offline Member
    Join Date
    Dec 2009
    Posts
    8
    Rep Power
    0

    Default Re: Java shape detection

    Hi,

    Thanks for your reply,

    I modified the output code to this:
    Java Code:
                    for (int i = 0; i < 4; i++) {
                        System.out.println(event.getX()+"-"+cercle[i].getX());
                        System.out.println(event.getY()+"-"+cercle[i].getY());
                        System.out.println(cercle[i].contains(250+(square*100),250,100,100));
                    }
    And the output:
    302-300.0
    271-300.0
    false
    302-400.0
    271-300.0
    false
    302-600.0
    271-300.0
    false
    302-700.0
    271-300.0
    false
    Even though the first false, should be a true. Because, if we make a square going from 250,250 with a width of 100x100, the square in 300x300 is in that box...

    That's the problem...

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

    Default Re: Java shape detection

    For more help from me, consider creating and posting an SSCCE. We don't want the whole application, just a smaller application that shows your problem and that we can run. Please check out the link.

  7. #7
    Ara1992 is offline Member
    Join Date
    Dec 2009
    Posts
    8
    Rep Power
    0

    Default Re: Java shape detection

    Well, the rest of my code is just the main method and another method for creating my JFrame and placing elements on it...

    Is it really needed?

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

    Default Re: Java shape detection

    Quote Originally Posted by Ara1992 View Post
    Well, the rest of my code is just the main method and another method for creating my JFrame and placing elements on it...

    Is it really needed?
    No it's not absolutely required, as we're not going to kick you out of the forum and call you names if you don't post a decent SSCCE. But if you want my help, I'm asking you for it. You see we're all volunteers who donate our free time to help. I don't think it's asking too much for you to make it easier for us to help you. So up to you what you do. As for me, I'll wait a bit to see if you post it.

  9. #9
    Ara1992 is offline Member
    Join Date
    Dec 2009
    Posts
    8
    Rep Power
    0

    Default Re: Java shape detection

    I am sorry, I didn't want to insult you or anything. I was just wondering if it were absoluty needed, because I was always told to keep my code short when posting for help xD

    But here is the full code:
    Java Code:
    /**
     * Travail fait par Aravinthan Sivaneswaran
     * Date: 27-Sep-2011
     * Nom du projet: Projet d'intégration (La guerre des pions)
     */
    /**
     * Cette classe permet d'avoir les differents fenetres
     * necessaire pour le projet ainsi que les
     * �v�nements de gestion ( clavier et souris )
     */
    // Les imports necessaire au projet
    import javax.swing.*;
    import java.awt.*;
    import java.awt.geom.*;
    import java.awt.event.*;
    
    class views extends JFrame {
        
        // Declarer le panneau de jeu
        JPanel gameplayPanel;
    
        // D�clarer les boutons qui sera utilis� a travers la classe.
        JComboBox moveWhite;
        JComboBox moveBlack;
        
        // Declarer les formes qui sera utilise a travers la classe.
        Rectangle2D.Double[] rectangle;
        Ellipse2D[] cercle;
        
    
        // Fonction qui permet d'avoir les differents f�netres du fonction.
        public views() {
    
            super("Creer les formes pour le jeu");
    
            setTitle("Game window");
            setSize(1200, 700);
    
            gameplayPanel = new JPanel();
            JPanel movesPanel = new JPanel();
            
            GestEvt listener=new GestEvt();
            gameplayPanel.addMouseListener(listener);
    
            Container contentPane = getContentPane();
    
    
            JLabel white_label = new JLabel("Bouger les pions blanc");
            moveWhite = new JComboBox();
            //moveWhite.addItemListener(this);
            moveWhite.addItem("Bouger pion 1");
            moveWhite.addItem("Bouger pion 2");
    
            JLabel black_label = new JLabel("Bouger les pions noir");
            moveBlack = new JComboBox();
            //moveBlack.addItemListener(this);
            moveBlack.addItem("Bouger pion 1");
            moveBlack.addItem("Bouger pion 2");
    
            JTextArea userMoves = new JTextArea(20, 100);
    
    
            gameplayPanel.add(white_label);
            gameplayPanel.add(moveWhite);
            gameplayPanel.add(black_label);
            gameplayPanel.add(moveBlack);
    
            movesPanel.add(userMoves);
    
            contentPane.add(gameplayPanel, BorderLayout.CENTER);
            contentPane.add(movesPanel, BorderLayout.SOUTH);
    
        }
    
        // Fonction qui permet de dessiner les diff�rent formes 2D.
        public void paint(Graphics g) {
    
            // Appeler la méthode paint de la superclasse.
            super.paint(g);
    
            // Créer la 2D par le remplacement du type de g en Graphics2D.
            Graphics2D g2d = (Graphics2D) g;
    
    
            // Tracer autant de rectangles demandes par l'utilisateur.
            rectangle = new Rectangle.Double[5];
    
            for (int x = 0; x < 5; x++) {
                rectangle[x] = new Rectangle2D.Double(250 + (x * 100), 250, 100, 100);
                g2d.draw(rectangle[x]);
            }
    
            // Tracer autant de cercles necessaire.
            cercle = new Ellipse2D[4];
            int y = 0;
    
            for (int x = 0; x < 5; x++) {
                g2d.setPaint(Color.black);
                if (x == 2) {
                    g2d.setPaint(Color.white);
                    x = 3;
                } else if (x == 4) {
                    g2d.setPaint(Color.white);
                }
                cercle[y] = new Ellipse2D.Double(300 + (x * 100), 300, 10, 10);
                g2d.fill(cercle[y]);
                y++;
            }
    
        }
    
    
    
        private class GestEvt extends MouseAdapter{
            public void mousePressed(MouseEvent event){
               int x = event.getX();
               int y = event.getY();
               int square;
               int point;
               
               // Creer la zone du rectangle
               if(((y>249 && y<351) && (x>250 && x<(250+(5*100))))){
                    square = (x-250)/100;
                    point = square;
                    if(square == 4){
                        point = 3;
                    }
                    
                    // Verifier s'il contient un point
                    for (int i = 0; i < 4; i++) {
                        System.out.println(event.getX()+"-"+cercle[i].getX());
                        System.out.println(event.getY()+"-"+cercle[i].getY());
                        System.out.println(cercle[i].contains(250+(square*100),250,100,100));
                    }
                    
              }
            }
        }
    }

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

    Default Re: Java shape detection

    I would do things differently as I would create two BufferedImages, one with a black circle in it and one with a white circle in it and use them to create two ImageIcons, again one with a white circle and the other with black. I'd then create 5 JButtons and place the black icons in 2 of them and the white icons in 2 of them. When a button is pressed, I'd simply get its Icon and see if it has one, and if so, if it == the blackIcon vs the whiteIcon.

    For example, please check the file I've downloaded. Change its extension from .zip to .jar and then run it and you'll see what I mean.
    Attached Files Attached Files

Similar Threads

  1. Simple Shape Recognition Java Program
    By TheShadow in forum New To Java
    Replies: 6
    Last Post: 07-31-2013, 09:25 PM
  2. Replies: 5
    Last Post: 04-27-2011, 06:04 AM
  3. Java keypress detection help
    By pghazanfari in forum Advanced Java
    Replies: 1
    Last Post: 05-29-2010, 08:30 PM
  4. Replies: 1
    Last Post: 04-02-2010, 12:48 PM
  5. COM Port Detection in Java
    By talasilasumanth in forum Forum Guides
    Replies: 1
    Last Post: 09-10-2008, 03:18 PM

Posting Permissions

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