This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Swing / AWT / SWT and the fly likes Drawing text inside a Graphics2D shape Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Drawing text inside a Graphics2D shape" Watch "Drawing text inside a Graphics2D shape" New topic
Author

Drawing text inside a Graphics2D shape

Veronique Kus
Ranch Hand

Joined: Jun 13, 2010
Posts: 41
My application allows the user to draw shapes. Say I've got a rectangle (subclass of Rectangle2D.Double) drawn, now I want to allow the user to double click on the rectangle and a text field with flashing cursor should appear. The user can type in the desired text and pressed enter or click outside the shape to stop editing text. The text gets displayed inside the shape.

I was wondering if anyone could point me in the right direction for achieving the result described. For a start I don't want to get preoccupied with the flashing cursor, I just jneed the editable 2D text... My own ideas include creating a new inner class inside the shape class that would represent the text area and would extend the KeyAdapter class... or extend JTextArea... I thought about it but I'm not convinced to any of my ideas and I'm not even completely sure how I could go about implementing them. Would really appreciate some help.

Let me know if any more information is needed.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41591
    
  55
Extending JTextField or JTextArea sounds like a good idea, if those two classes don't provide the necessary functionality out of the box. Being Swing components, they can be positioned at a position of your choosing. And you can add a KeyListener to them in order to provide whatever behavior is desirable in response to keystrokes.


Ping & DNS - my free Android networking tools app
Veronique Kus
Ranch Hand

Joined: Jun 13, 2010
Posts: 41
Ok thanks. I'm still not sure though about the specifics of the implementation... for example, how do I make the text area appear inside the desired shape? I mean, I don't think I can just add a JTextArea to a Rectangle2D object?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41591
    
  55
No, but you can put it at an absolute position. Since you know where the Rectangle2D object is (right?), you can calculate where the text field should be.
Veronique Kus
Ranch Hand

Joined: Jun 13, 2010
Posts: 41
Ok I tried but for some reason it does not work. I'm posting below my DrawingCanvas class which is where all the shapes are drawn and where the JTextArea should also be drawn.


So in the mouseClicked() method I'm trying to display a JTextArea anywhere (for now) on the drawingCanvas object but it doesn't appear when I double click (and I've got a shape selected). Is there something wrong with this call? The task looks pretty simple, I must be not seeing something obvious...
Veronique Kus
Ranch Hand

Joined: Jun 13, 2010
Posts: 41
If anyone would like to run this code I'm posting the other classes needed:



Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41591
    
  55
You need to call a setLayout(null) on the JPanel, and you also need to call setBounds on the JTextArea with the coordinates where it should be placed (and then possibly call repaint). This explains it: Doing Without a Layout Manager (Absolute Positioning)
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Suggestions unrelated to your original problem but which I tripped over on reading your code:

1) Don't mix AWT components (i.e., TextField) with Swing components (everything else).
2) I'd rename Shape to be some other name as it conflicts with the Shape interface in the AWT library.
3) Don't declare your enum as several static inner classes. Use one single enum in its own file.

Hopefully more to come,... but it's a lot of code.
4) Simplify your code so that the current code is for testing and solving your current problem only and that's it. Get rid of all unrelated code. This unrelated code may be necessary to the final product sure, but it's only getting in the way of your and our understanding of the problem at hand.

Much luck!
Veronique Kus
Ranch Hand

Joined: Jun 13, 2010
Posts: 41
Wow, you guys are fast with replying!
Ulf, I'll try it out in a sec.
Pete, thanks for the suggestions. I was also wondering... don't you think it's a little bit weird that drawing lines is also handled by the RectangularShape class? I had some serious problems trying to think of a good design for handling the shapes... I need all shapes to share a common interface since all of them are stores in one ArrayList (so that they can all be painted together). But I'm thinking maybe I should have two or more lists from which different kinds of shapes would be drawn....
this would mean though that I would have to check for different classes when handling shapes (because, for example, a line would not have a getCenter() method and rectangular shapes would) and I don't know which would be better - awkward design and a single list or more reasonable desing and more than one list containing different classes of shapes.
Veronique Kus
Ranch Hand

Joined: Jun 13, 2010
Posts: 41
Ok, so this works, thank you. However, I'm still wondering if there's any way of creating text inside shapes using the graphics' drawString method? It sounds more appealing to me because I would be able to draw the string when the shape is drawn, so they would be bound together. In case of JTextAreas, I need to record the shape which the text area belongs to and the position of the text area... Maybe a text area could appear only for editing and then the text would be recorded and drawn using the drawString() method - do you thing this would be a good approach?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41591
    
  55
However, I'm still wondering if there's any way of creating text inside shapes using the graphics' drawString method? It sounds more appealing to me because I would be able to draw the string when the shape is drawn, so they would be bound together. In case of JTextAreas, I need to record the shape which the text area belongs to and the position of the text area... Maybe a text area could appear only for editing and then the text would be recorded and drawn using the drawString() method - do you thing this would be a good approach?

Yes, absolutely. The JTextArea is for entering the text, and then it should be drawn using drawString, and the JTextArea removed. I actually assumed that's what you had in mind.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19674
    
  18

Veronique Kus wrote:Maybe a text area could appear only for editing and then the text would be recorded and drawn using the drawString() method - do you thing this would be a good approach?

That's probably what I would have done. You can even use one single JTextField (or JTextArea for multilines) and reuse it. You create and add it at the start of the program but hide it (setVisible(false)). When you double click on a shape you move it to the right position (setBounds), show it (setVisible(true)) and use it. When done* get its text, store it with the shape, and hide the field again. The shape will then use drawString to draw its text.

* using a FocusListener. With JTextField you can also use an ActionListener to listen for enter presses.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Drawing text inside a Graphics2D shape