1

For my project I want to resize text just like in Adobe Photoshop. But when I tried resizing(by mouse drag) the textpane, only the textpane was getting resized and not the text inside it. Then I tried converting my text to image and tried resizing it again (As mentioned in the post How to resize text in java) and it worked fine.. Now I have another problem of editing my text. i.e. once my text is converted to image then how do i edit it (eg: changing the text color, etc).

Any idea on this?

3
  • The example cited here calls createImage() whenever setText() gets called.
    – trashgod
    Nov 21, 2012 at 2:37
  • this site is giving me some error while posting the code!! But please refer this link (stackoverflow.com/questions/8281886/stretch-a-jlabel-text/…). Here they are doing the same thing. They have stretched the text "Sample" by converting it to image. Now can you tell me how do i edit that text (i.e. lets say I want to change the color of Sample after it is converted to image).
    – user001
    Nov 21, 2012 at 2:48
  • Andrew could you please more elaborate?
    – user001
    Nov 21, 2012 at 3:00

2 Answers 2

3

..once my text is converted to image then how do I edit it (eg: changing the text color, etc).

Don't. It is better (quality) and easier to paint the image each choice with the text when requested, in the desired Font, starting Point, AffineTransform, RenderinhHints and Color (etc.).

StretchLabels

import java.awt.*;
import java.awt.geom.AffineTransform;

import javax.swing.*;

public class StretchLabels {

    String s = "The quick brown fox jumps over the lazy dog!";
    Font font = new Font(Font.SERIF, Font.PLAIN, 24);

    public JComponent getGUI() {
        JPanel gui = new JPanel(new BorderLayout());

        JLabel l1 = new StretchTextLabel(s);
        l1.setFont(font);
        gui.add(l1, BorderLayout.CENTER);

        return gui;
    }

    public static void main(String[] args) throws Exception {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                JFrame f = new JFrame("Streeetch me!");
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                StretchLabels eg = new StretchLabels();
                f.setContentPane(eg.getGUI());
                f.pack();

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

class StretchTextLabel extends JLabel {
    private static final long serialVersionUID = 1L;

    public StretchTextLabel(String s) {
        super(s);
    }

    @Override
    public void paintComponent(Graphics gr) {
        Color c = gr.getColor();
        setForeground(new Color(0,0,0,0));
        super.paintComponent(gr);
        setForeground(c);
        gr.setColor(c);
        Graphics2D g = (Graphics2D)gr; 
        g.setRenderingHint(
                RenderingHints.KEY_TEXT_ANTIALIASING, 
                RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

        double pw = this.getPreferredSize().width;
        double ph = this.getPreferredSize().height;
        double w = this.getSize().width;
        double h = this.getSize().height;

        // Set FG (text) color
        g.setColor(this.getForeground());

        // stretch
        AffineTransform stretch =
                AffineTransform.getScaleInstance(w/pw, h/ph);
        g.setTransform(stretch);
        g.drawString(getText(), 0, this.getFont().getSize());
    }
}

.. how do I edit the text in the image?

I typically offer controls to do it like this:

Text Control

But of course, that does not cover:

  1. Color, which has a control in the main GUI (a basic paint app.)
    Basic paint app.
  2. Scaling of the resulting text by 'drag'. Which might be achieved by using a resizable element as demonstrated by either @trashgod or myself (above).
2
  • Hi Andrew, the above code will convert text to image but after the conversion how do I edit the text in the image?
    – user001
    Nov 21, 2012 at 15:32
  • "..edit the text in the image?" Set the text of the label. You might have a statically sized text field above it (or as a pop-up) to edit the text. Nov 22, 2012 at 1:15
0

Instead of converting the text to image, why don't you increase the text's size so it grows along with the pane?

3
  • I may be wrong, but I can't find how to do it. I think I don't have enough priviledge to write comments to other people's messages... Nov 21, 2012 at 2:52
  • Yes I could do that. But I have a very rare requirement of increasing only the width of text and not height. So I thought Stretching would be a good option
    – user001
    Nov 21, 2012 at 2:53
  • Well, I don't know if I'm being naive, but in that case I guess you could store the original text and perform the "text to image" conversion everytime you change styling properties. Nov 21, 2012 at 2:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.