wood burning stoves 2.0*
The moose likes Applets and the fly likes How to draw a String to an applet screen at the point of mouse click? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "How to draw a String to an applet screen at the point of mouse click?" Watch "How to draw a String to an applet screen at the point of mouse click?" New topic
Author

How to draw a String to an applet screen at the point of mouse click?

Hayles Berry
Ranch Hand

Joined: Dec 16, 2012
Posts: 32
I am currently creating a Java applet. The applet works by taking text input from the user and displaying it to the screen when they press return. This is the point up to which my program is working at the moment. I was wondering whether there was a way in which I could make it so the text input/String is drawn when the mouse is clicked, at the point of mouse click.

Many thanks in advance to anyone who can help me out with this.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41613
    
  55
Certainly. You'd need a MouseListener that records the X/Y coordinates of where the click happened, and then call repaint() to have the component be drawn anew.

This assumes that you have altered the paintComponent method (assuming you extend JApplet) or paint method (assuming you extend Applet) so that it draws the str´╗┐ing at those coordinates. That means you need to store the coordinates somewhere where the paint/paintComponent method can get at it.


Ping & DNS - my free Android networking tools app
Hayles Berry
Ranch Hand

Joined: Dec 16, 2012
Posts: 32
Ulf Dittmer wrote:Certainly. You'd need a MouseListener that records the X/Y coordinates of where the click happened, and then call repaint() to have the component be drawn anew.

This assumes that you have altered the paintComponent method (assuming you extend JApplet) or paint method (assuming you extend Applet) so that it draws the string at those coordinates. That means you need to store the coordinates somewhere where the paint/paintComponent method can get at it.


Okay, that does seem to make sense.

I have it working now, but I have now been presented with a new problem.
Before I got the text to be drawn at the point of mouse click, I had two polygons that were only drawn to the screen by the program if a certain two keyboard characters were entered as part of user input (either together or separately). Since fixing this topic's problem, those two polygons no longer show-up on screen at all when the certain two characters are typed in.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41613
    
  55
Yes, that's fine. Now you just have to write the paint method so that it makes use of them.
Hayles Berry
Ranch Hand

Joined: Dec 16, 2012
Posts: 32
Ulf Dittmer wrote:Yes, that's fine. Now you just have to write the paint method so that it makes use of them.


Done, but I now have a new problem I've just mentioned in my last post. ^^
Thanks for all your help so far, by the way. It means a lot.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41613
    
  55
Please don't edit previous posts. They invalidate the responses you got. Like now my previous post no longer makes sense.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41613
    
  55
You need to keep track of all state of what should and should not be drawn in some appropriate data structures, so that any time the paint method is called (and you'll never know when it will be called), it draws exactly that which should be shown at that point in time. It sounds as if that is not the case here.
Hayles Berry
Ranch Hand

Joined: Dec 16, 2012
Posts: 32
Ulf Dittmer wrote:You need to keep track of all state of what should and should not be drawn in some appropriate data structures, so that any time the paint method is called (and you'll never know when it will be called), it draws exactly that which should be shown at that point in time. It sounds as if that is not the case here.


Okay. How would I do that, exactly?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41613
    
  55
That depends on what might be displayed at any given time. The thing to keep in mind is that the paint method might be called any number of times, whether you expect it to or not. Whatever it doesn't draw whenever it's called, might be gone from the display. So... your data structures and the paint method need to work together to produce whatever it is that should be displayed.
Hayles Berry
Ranch Hand

Joined: Dec 16, 2012
Posts: 32
Ulf Dittmer wrote:That depends on what might be displayed at any given time. The thing to keep in mind is that the paint method might be called any number of times, whether you expect it to or not. Whatever it doesn't draw whenever it's called, might be gone from the display. So... your data structures and the paint method need to work together to produce whatever it is that should be displayed.


What do you mean by 'data structures'?

Also, this is the code fragment that's giving me the problem.

I don't know whether you'll be able to tell me where I'm going wrong?
What should be drawn every time the user inputs a string of text is just that, the string. But if the user types in a string that contains either one or both of my "special characters" ('h'/'H' or 'b'/'B', called 'sl1' and 'sl2' in my code), that character's relevant polygon should be displayed in its' place. The rest of the string should also be displayed as normal.

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41613
    
  55
Calling repaint from within the paint method is wrong - the paint method should draw on the screen whatever needs drawing on the screen. After it's done there should not be a need to update anything, because the paint method did just update the display to reflect the latest changes. It looks like you're still assuming that the paint method is called whenever something happened that causes the display to change - that is not the case.

By "data structures" I mean variables that hold the complete state of what's displayed on the screen. They need to contain all the information needed to redraw everything from scratch - what string to show, where to show it, where any polygons should be etc. I would start by getting that inp lace for just drawing the string wherever the user clicks. Then procedd to the polygon issue.
Hayles Berry
Ranch Hand

Joined: Dec 16, 2012
Posts: 32
Actually, it's all okay now, I've managed to figure it all out from what you'd said so far.
Thank-You for all your help, it means a lot.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to draw a String to an applet screen at the point of mouse click?