Results 1 to 10 of 10
Thread: Java shape detection
- 10-02-2011, 08:47 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 8
- Rep Power
- 0
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:
And the output is: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));
But it supposed to return "true" because the square I traced with the .contains covers the cercle...0-0
289-300.0
273-300.0
false
Thanks,
Ara
-
Re: Java shape detection
I don't think we can guess what your code is doing without seeing the pertinent parts.
- 10-02-2011, 09:08 PM #3
Member
- Join Date
- Dec 2009
- Posts
- 8
- Rep Power
- 0
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)); } } }
-
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.
- 10-02-2011, 10:33 PM #5
Member
- Join Date
- Dec 2009
- Posts
- 8
- Rep Power
- 0
Re: Java shape detection
Hi,
Thanks for your reply,
I modified the output code to this:
And the output: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)); }
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...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
That's the problem...
-
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.
- 10-02-2011, 11:03 PM #7
Member
- Join Date
- Dec 2009
- Posts
- 8
- Rep Power
- 0
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?
-
Re: Java shape detection
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.
- 10-03-2011, 12:02 AM #9
Member
- Join Date
- Dec 2009
- Posts
- 8
- Rep Power
- 0
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)); } } } } }
-
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.
Similar Threads
-
Simple Shape Recognition Java Program
By TheShadow in forum New To JavaReplies: 4Last Post: 09-04-2011, 08:56 AM -
can anione help mi with the shape recognition part of the java programming
By muhaimin in forum New To JavaReplies: 5Last Post: 04-27-2011, 06:04 AM -
Java keypress detection help
By pghazanfari in forum Advanced JavaReplies: 1Last Post: 05-29-2010, 08:30 PM -
how to change the shape of the JFrame to a oval shape
By kiki2009 in forum Java 2DReplies: 1Last Post: 04-02-2010, 12:48 PM -
COM Port Detection in Java
By talasilasumanth in forum Forum GuidesReplies: 1Last Post: 09-10-2008, 03:18 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks