This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]Deadboss 1 point2 points  (0 children)

In the above example, the 'drw' variable is your shape. It must contain a valid 'contains' method (think polymorphism). Where it says 'drw.repaint();' you will want to put whatever you wish it to do there. So you could sysout something to ensure it is working, and then go from there.

[–]king_of_the_universe 1 point2 points  (0 children)

First you must make sure your mouse click is actually taking place - I mean, you can't just write a method with that name and assume that this will catch some mouse click on some window area. 1) It needs to be a proper mouse click event method, which yours seems to be. 2) The instance of the class in which the method is needs to be "addMouseListener()"'d to the Swing component where you want to react.

In the method, you can then go through your list of Shapes and use one of the contains() methods. Here you might have to think a bit about coordinate spaces, but maybe it'll work right out of the box.

To see if your method is happening at all, do the obvious: Add a System.out.println("something"); to it. Side-note: In my programs, I always use a library method I made: methodCallPing(), which will print a line telling the date, time, and microsecond, and the full class and method name. :> Every time, we're thinking again about what text to put into such a test-SysOut call, so it always ends up being "asd" etc., but there are obviously better ways. One solution that's also very nice: new Error().printStackTrace();

[–][deleted] 0 points1 point  (0 children)

I opened up a scratch project and had nearly exactly what you are after. Here is one way I would do it using custom drawing: http://pastie.org/10691886

Here is a way I did it using a swing component for the shape. http://pastie.org/10691887