CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2011
    Posts
    189

    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 overriding paintComponent(Graphics g) , but most of it failed since my main class extends JFRAME and not JPANEL hence the inability to override paintComponent() .
    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 G O O G L E ! but nothing has been relevant.

    Cheers,
    -Cens
    Last edited by cens; November 8th, 2012 at 08:54 AM.

  2. #2
    Join Date
    Nov 2011
    Posts
    189

    Unhappy Re: draw image at mouse position

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

  3. #3
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    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.
    Last edited by keang; November 13th, 2012 at 03:39 AM.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  4. #4
    Join Date
    Nov 2011
    Posts
    189

    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?

  5. #5
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    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.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured