user2753082
user2753082

Reputation: 41

java code for TextBox in MS Paint

I am trying to create a code that simulates a MS Paint in java using Jframe. I want to create a textbox like field like MS Paint has,wherein you drag a box and according to your preference you set a size for it. What I do is I draw a rectangle first and then get the dimensions by mouse event listener and pass these values to a function that creates a JtextArea of given size and width. However, I need to extend the Jframe class which creates a new frame on top of the one that already exists. I try to pass my original frame as a parameter to draw upon for the JtextArea which does not work. Is there any way of implementing JtextArea without extending the frame class? And If possible any relevant example to draw a textbox which is similar to MS Paint. Please note that I don't want to use the Graphics.drawstring method. Thanx.

Upvotes: 0

Views: 732

Answers (2)

StanislavL
StanislavL

Reputation: 57391

Create a temp JTextArea and add to your drawing panel with null layout to be placed over the rectangle.

When edit is done (text entered) remove the temp textarea, get the user entered text and draw it in the original rectangle.

Upvotes: 1

Catalina Island
Catalina Island

Reputation: 7126

Since you don't want to use drawString() directly, java.awt.font.TextLayout is probably the best option for rendering text.

Upvotes: 1

Related Questions