If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

draw image at mouse position

I have recently started the developement of a minigame called "toro".
As this is my first time using graphics, i have no ideea where to start, so i'll just make a brief introduction to the program and ask you for help.
Toro is a game in which you have to defend 5 dots against a bull, by telling the dots where to go via a cursor. The program is composed from a JPANEL inside a JFRAME .
What i need to do is the following:
1) Add a mouse listener on the JPANEL so i could get the coordinates of the mouse as it you click it. [done]
2) When Clicked, the panel should display an image (100*100px if it matters) to the coordinates returned by the listener[pending]

What i have tried:

I know that you will not accept giving help without me trying by myself, so i'll tell you what i already tried.
Bassically everything i tried was related to overridingpaintComponent(Graphics g) , but most of it failed since my main class extends JFRAME and not JPANEL hence the inability to overridepaintComponent().
I have also tried making another class extending JPANEL, tentative which failed.

From the tutorials I've seen i learned how to draw an image at the very beginning of the execution, but not when triggered by an event ( mouseClicked ).

Please help me, i really dont know what to look for anymore, as i already consulted GOOGLE! but nothing has been relevant.

Re: draw image at mouse position

i imagined i would get an answer pretty fast.. i didnt think it was much of a problem..

We do this as volunteers, giving our knowledge and experience for free hence we answer questions as and when we can. If you want a guaranteed quick answer then you will have to pay for it.
It never ceases to amaze me that someone who can't do something can assume the problem is simple and it will be easy for someone else to tell them how to do it.

Bassically everything i tried was related to overriding paintComponent(Graphics g) , but most of it failed since my main class extends JFRAME and not JPANEL hence the inability to override paintComponent() .

You will need to use a component such as JPanel as the canvas to draw your images on.

The best way to do this type of thing is to think of each thing in the game as an object and each object type will have a component to draw it's representative shape eg board, dot, bull. So you could have a Board class that extends a Swing component and acts as a container for the game objects, a Dot class that extends a Swing component to draw a dot and a Bull class that extends a Swing component to draw a bull. The advantage of using separate classes is it means you can easily add functionality for an object type such as background images, animations, selections etc.

Re: draw image at mouse position

thanks keang, and sorry..

I started the project and so far it's working decently...
i only have one question:

as the game will implify "movement collision" what component should i use for the bull and for the dots? For now i used JLabels for the dots but im afraid that that wont be the best choice regarding collision.. what do you think?

Re: draw image at mouse position

If JLabels have some feature you need then create your own class that extends JLabel and override the 2 contains methods so they return true when a given point is inside the shape of your dot. You can then call contains(..) to find out if the bull has hit a dot.

* The Perfect Platform for Game Developers: Android
Developing rich, high performance Android games from the ground up is a daunting task. Intel has provided Android developers with a number of tools that can be leveraged by Android game developers.

* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.