Results 1 to 14 of 14
  1. #1
    matrixcool is offline Member
    Join Date
    Dec 2010
    Posts
    21
    Rep Power
    0

    Default How to draw a shape with listener ??

    Hi ,

    this may sounds weird .. I wanna know how to make a listener for a particular shape for example a rectangle so that when I click it , it does something




    the photo above is an example of what I want ,, so when I click on any square I want a window to appear .. could anyone please show me a piece of code so I can understand


    thanks in advance

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,926
    Rep Power
    8

    Default

    Well you could just add a MouseListener to the JPanel (I'm assuming that's what's painting the grid). What have you tried? Where are you stuck?

    How to Write a Mouse Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)

  3. #3
    matrixcool is offline Member
    Join Date
    Dec 2010
    Posts
    21
    Rep Power
    0

    Default

    thanks 4 your reply but you didn't understand what I want ..

    I know how to draw rectangles but the problem is I don't know for example if I have 4 rectangles I want when I click on one rectangle it shows me something and when I click on an other rectangle it shows me different thing ,, could anyone please elaborate with a piece of code.. because I didn't understand the explanation on the link you gave me it's so confusing !

    I'm really stuck and I'd appreciate the help

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,926
    Rep Power
    8

    Default

    I do understand what you want. And you need to write a MouseListener to detect mouse clicks. If you don't understand the tutorial, ask a specific question about it and post an SSCCE demonstrating where you're stuck.

    That's the first step. Worry about "showing something", whatever that means, after you get the logic down for detecting which square was clicked.

  5. #5
    matrixcool is offline Member
    Join Date
    Dec 2010
    Posts
    21
    Rep Power
    0

    Default

    1st I really appreciate your help.

    2nd look at this picture:




    I'm trying to draw each square of those and this is what I'm doing:

    Java Code:
    public void paint (Graphics g){
    		super.paint(g);
    		
    		
    		g.drawRect(30,60,320,300);
    		g.setColor(Color.WHITE);
    		g.fillRect(31,61,40,40);
    		g.setColor(Color.BLACK);
    		g.fillRect(70,61,40,40);
    		g.setColor(Color.WHITE);
    		g.fillRect(110,61,40,40);
    
    // and so on

    and I'm stuck how to make each square when you click on it a window appears with its coordinate (the coordinates that are in the picture not in pixels)

    I don't know how to do it even when I saw the tutorial it's confusing


    help plz

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,926
    Rep Power
    8

    Default

    First step- Do your painting correctly. You should probably be using the paintComponent method of a JPanel or JComponent. Are you?

    Second step- Get a MouseListener working at all. Just detect a click anywhere on the custom component.

    Third step- Print out the cursor location coordinates in the MouseListener you wrote. Compare those values with the row and column you're in to figure out the basic math you need to figure out which square was clicked.

    Post an SSCCE if you get stuck.

  7. #7
    matrixcool is offline Member
    Join Date
    Dec 2010
    Posts
    21
    Rep Power
    0

    Default

    I'm painting using the paint method

    public void paint(Graphics g){}

    Can I add mouse listener to each square?? if I can how ??

    and what is SSCCE ?? does it mean "Posting the code I'm stuck in" ??

    thanks

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,926
    Rep Power
    8

    Default

    Quote Originally Posted by matrixcool View Post
    I'm painting using the paint method
    And like I said, you should be using the paintComponent method of a JPanel or JComponent.

    Quote Originally Posted by matrixcool View Post
    Can I add mouse listener to each square?? if I can how ??
    You'll be adding the MouseListener to the component that draws the squares.

    I suppose you could make each square its own component with its own listener, but in my opinion, that's overkill.

    Quote Originally Posted by matrixcool View Post
    and what is SSCCE ?? does it mean "Posting the code I'm stuck in" ??
    What does the SSCCE link tell you?

  9. #9
    matrixcool is offline Member
    Join Date
    Dec 2010
    Posts
    21
    Rep Power
    0

    Default

    I finished drawing .. but still I don't know how to add a mouse listener to each square could you please tell me with a piece of code ,, that's what remain to solve this problem

  10. #10
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,926
    Rep Power
    8

    Default

    Quote Originally Posted by matrixcool View Post
    I finished drawing .. but still I don't know how to add a mouse listener to each square could you please tell me with a piece of code ,, that's what remain to solve this problem
    You don't need to add a listener to each square. You add the listener to the component that draws the squares. The tutorial spells out exactly how to do that. I'm not sure what else you want. Chances are, nobody is going to write the code for you. Follow the steps I gave you above.

  11. #11
    matrixcool is offline Member
    Join Date
    Dec 2010
    Posts
    21
    Rep Power
    0

    Default

    Thanks for your great efforts and I really appreciate it ,,

    But why there is no body gonna write a piece of code not THE WHOLE CODE just for clarification ??

    Thanks a lot

  12. #12
    matrixcool is offline Member
    Join Date
    Dec 2010
    Posts
    21
    Rep Power
    0

    Default

    OK , I'm trying to compare the coordinates in pixels but it gives me errors like this

    int aX=a.getX();
    int aY=a.getY();

    if (aX => 31 )
    //the rest


    is this allowed ??

  13. #13
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,926
    Rep Power
    8

    Default

    I think you're looking for >=.

    But it seems like you're on the right track.

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

    Default

    Quote Originally Posted by matrixcool View Post
    But why there is no body gonna write a piece of code not THE WHOLE CODE just for clarification ??
    No, it's not our job and there's really no need to do this. There are Swing tutorials where you will find examples and lessons. Also, you can find examples by searching this and other forums.

Similar Threads

  1. create own shape
    By kepep in forum Java 2D
    Replies: 2
    Last Post: 12-15-2010, 07:43 PM
  2. draw shape with user defined values
    By cka in forum New To Java
    Replies: 4
    Last Post: 07-14-2010, 03:35 PM
  3. Replies: 0
    Last Post: 05-20-2010, 08:18 PM
  4. Replies: 1
    Last Post: 04-02-2010, 12:48 PM
  5. implementing shape
    By sidkdbl07 in forum Java 2D
    Replies: 1
    Last Post: 01-12-2008, 06:42 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
  •