• 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

Drawing text inside a Graphics2D shape

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Veronique Kus
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If anyone would like to run this code I'm posting the other classes needed:



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

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.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic