1

I am making a paint program and I'm coming across some trouble figuring out how to make a text tool. What I want to do is click and drag to make a textbox appear on the canvas. The problem is that I don't want this to just be a JTextComponent floating over my canvas component. That causes issues with the border flickering when I move it and parts of my cursor looking like they're under the textbox when my mouse is right next to it.

What I was going to do was create a JTextArea and then fire mouseEvents at is based on my mouse's location relative to the textbox on the canvas. The one on the canvas would really just be a painted representation (using .getGraphics()) of the actual JTextArea that I haven't added to any component. The problem with this is that you can't call .getGraphics() unless the component is displayed on the screen.

I'm trying to avoid recreating the entire JTextComponent code just for something like this that seems like it should be simple.

Does anyone have any solutions to this problem or alternate ideas?

pigi5
  • 74
  • 1
  • 10

1 Answers1

0

You could make a JTabbedPane. One tab for paint panel and another for the textPane. and the you could give and action listener to some button. the actionlistener should give the paint method's graphics a Graphics.drawString(...); the string is what the textComponent gave. moving the string around in your paint program is more difficult but you could have a mouseListener that will identify mouse drags, those will set the new (x,y) point of you Graphics.drawString(...);

Hope this helped a bit.

ItamarG3
  • 4,092
  • 6
  • 31
  • 44
  • The problem is that using getText doesn't copy all of the parts of a JTextComponent like the flickering carrot. For that I would need getGraphics, which I can't use unless the component is actually visible. – pigi5 May 15 '14 at 21:09
  • You could and isFocused(); when it is focuses, you could use setCaretPosition(); that way only when the user wants to edit the test, there will be a caret... If you want to have multiple text areas, you could use threads... And have wait when it isn't focused. – ItamarG3 May 17 '14 at 10:13
  • setCaretPosition() is for JTextComponents. I would not be able to draw the caret on my screen along with the graphics.drawString() without doing it manually. By then it would be just as easy to copy the java code for a JTextArea into my program and alter it from there. – pigi5 May 18 '14 at 23:59