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.
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.
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()?
Re: How can I click on an image from drawstring()?
You can just use setLocation(...);
Re: How can I click on an image from drawstring()?
Yes, of course. (I can be quite thick sometimes.)
Thanks.
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