1

In my application, I need to draw JTextArea on certain position on my panel, and I need to be able to zoom in and out on it.

For positioning it, I can use absolute positioning, but scaling poses a challenge - I can easily scale it down by tweaking paintComponent method:

@Override
public void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    g2.scale(0.5,0.5);
    super.paintComponent(g2);
}

But scaling up doesn't work - since I set the bounds of the component using setBounds explicitly, part of scaled up component ends up not being drawn. How can I solve it?

EDIT: A bit of explanation.

The application I maintain is a sort of a graphic editor, and I need to add a functionality of adding a something like a note to working area, so that user can directly type the text on that working area. As in most graphic editors, the working area can be moved and zoomed in/out, so the text area where user is typing the text should move and scale too.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Rogach
  • 26,050
  • 21
  • 93
  • 172
  • I've never seen this sort of functionality -- zooming in and out of a JTextArea, an image yes, but never a JTextArea. Out of curiosity, what is the purpose of this. Your reply may have bearing on the possible solutions for your problem. Also note that most of the Swing experts here avoid using null layout and absolute positioning if at all possible, so you may need to justify this a bit too. Luck. – Hovercraft Full Of Eels Oct 07 '12 at 15:00
  • @HovercraftFullOfEels - Added explanation. I don't see how this can be easily done with non-absolute positioning layout, but maybe I am missing something. – Rogach Oct 07 '12 at 15:05
  • No, you may be right, and absolute positioning may be the only way. Again, I've not done anything like this, so I don't know what requirements are necessary. A very small compilable and runnable program that doesn't require resources that aren't available to us (images, databases) and that we can compile and run would go a long way towards helping us fully understand your problem and thus be able to come up with possible solutions, an [sscce](http://sscce.org). – Hovercraft Full Of Eels Oct 07 '12 at 15:07
  • It's kind of hard to come up with sscce here - since the whole problem boils down to a JPanel, on which stuff is drawn, and user can move or scroll in/out (this part with adding text to panel is obviously not implemented yet). But I'll try to do something about it. – Rogach Oct 07 '12 at 15:16
  • I'd suggest JXLayer (JLayer in Java 7), but the examples are a little thin on the ground now days – MadProgrammer Oct 07 '12 at 19:05
  • @MadProgrammer - I saw many references to JXLayer, but it seems very dead these days (and I can't use JLayer, since most of users are on java 6). – Rogach Oct 08 '12 at 06:09
  • JXLayer is still valid (in it's current incarnation) for Java 6, but yes, I'd say it's focus has shifted to java 7 – MadProgrammer Oct 08 '12 at 06:20

1 Answers1

2

If the text component must remain editable when scaled, you may be able to scale the Font used in (rough) proportion to the scale factor, as shown here.

image

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • But can I use `float` font size, like 30.2px? With that, it would be possible to get almost exact proportion. – Rogach Oct 07 '12 at 18:15
  • The example uses `new Font()`, but `deriveFont()` is probably a better choice. An integral size seems adequate, but you could try `deriveFont(AffineTransform trans)`. – trashgod Oct 07 '12 at 20:58
  • I think I can also use `.deriveFont(Float size)`, so not to mess with AffineTransforms. – Rogach Oct 08 '12 at 06:06