Results 1 to 6 of 6
  1. #1
    rippon is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

    Default How can I click on an image from drawstring()?

    I want to draw a string. Then I want the user to click on the JPanel and I want to take action depending upon whether or not the user clicked on the string.

    Struggling to understand my textbook. From what Iíve read, I *think* I need a FontReaderContext and/or FontMetrics and/or LineMetrics.

    My textbook isn't really giving me the sort of code examples I need.

    Please can someone show me a bit of code along the lines Iím thinking?

    Thanks.

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default Re: How can I click on an image from drawstring()?

    Don't draw the string. Use a JLabel, then you can just add a MouseListener to the label.

  3. #3
    rippon is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

    Default Re: How can I click on an image from drawstring()?

    Thanks. Yes, I thought of that, but I *think* drawString() might be better (or even necessary) - because I want my text to keep moving around like a moving target that the user has to hit (with a click). So I thought repaint() with a Timer would be the way to go.

    Couldn't quite see how to do this with a JLabel: would I have to keep creating a "new JLabel("target")" with every repaint() and keep destructing the previous one from the previous repaint()?

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default Re: How can I click on an image from drawstring()?

    You can just use setLocation(...);

  5. #5
    rippon is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

    Default Re: How can I click on an image from drawstring()?

    Yes, of course. (I can be quite thick sometimes.)

    Thanks.

  6. #6
    Grkn is offline Member
    Join Date
    Oct 2011
    Posts
    65
    Rep Power
    0

    Default Re: How can I click on an image from drawstring()?

    there is other way of course :)
    you have to get pixel map(locations) of String. Then keep it in arraylist
    add panel e action listener then checks wheater it is in map or not

Similar Threads

  1. Replies: 2
    Last Post: 06-22-2011, 01:19 PM
  2. How to change size of drawString in Jpanel?
    By david522 in forum New To Java
    Replies: 1
    Last Post: 05-17-2011, 01:56 PM
  3. drawString() method not working correctly
    By tnixon22 in forum New To Java
    Replies: 2
    Last Post: 02-25-2011, 08:57 PM
  4. help with drawString
    By h3nch in forum AWT / Swing
    Replies: 5
    Last Post: 01-16-2010, 02:58 PM
  5. drawString with Chinese Characters
    By vaskarbasak in forum Advanced Java
    Replies: 1
    Last Post: 06-10-2008, 07:49 AM

Posting Permissions

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