Results 1 to 14 of 14
- 12-14-2010, 05:44 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 21
- Rep Power
- 0
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
- 12-14-2010, 05:48 PM #2
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)
- 12-15-2010, 02:50 PM #3
Member
- Join Date
- Dec 2010
- Posts
- 21
- Rep Power
- 0
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
- 12-15-2010, 02:56 PM #4
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.
- 12-15-2010, 03:03 PM #5
Member
- Join Date
- Dec 2010
- Posts
- 21
- Rep Power
- 0
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
- 12-15-2010, 03:15 PM #6
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.
- 12-15-2010, 03:23 PM #7
Member
- Join Date
- Dec 2010
- Posts
- 21
- Rep Power
- 0
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
- 12-15-2010, 03:26 PM #8
And like I said, you should be using the paintComponent method of a JPanel or JComponent.
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.
What does the SSCCE link tell you?
- 12-15-2010, 03:46 PM #9
Member
- Join Date
- Dec 2010
- Posts
- 21
- Rep Power
- 0
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
- 12-15-2010, 04:18 PM #10
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.
- 12-15-2010, 04:32 PM #11
Member
- Join Date
- Dec 2010
- Posts
- 21
- Rep Power
- 0
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-15-2010, 04:48 PM #12
Member
- Join Date
- Dec 2010
- Posts
- 21
- Rep Power
- 0
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 ??
- 12-15-2010, 04:57 PM #13
I think you're looking for >=.
But it seems like you're on the right track.
-
Similar Threads
-
create own shape
By kepep in forum Java 2DReplies: 2Last Post: 12-15-2010, 07:43 PM -
draw shape with user defined values
By cka in forum New To JavaReplies: 4Last Post: 07-14-2010, 03:35 PM -
Draw Shape later of Press Button, other implementation., where is mistake?
By joseluisbz in forum Java 2DReplies: 0Last Post: 05-20-2010, 08:18 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 -
implementing shape
By sidkdbl07 in forum Java 2DReplies: 1Last Post: 01-12-2008, 06:42 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks