• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Hayles Berry
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, that's fine. Now you just have to write the paint method so that it makes use of them.
 
Hayles Berry
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please don't edit previous posts. They invalidate the responses you got. Like now my previous post no longer makes sense.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Rancher
Posts: 43081
77
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic