How to edit a text that is converted into image? or any other approach to realize/edit text

大兔子大兔子 提交于 2019-11-28 10:34:38

问题


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?


回答1:


..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.).

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:

But of course, that does not cover:

  1. Color, which has a control in the main GUI (a 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:


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



来源:https://stackoverflow.com/questions/13485501/how-to-edit-a-text-that-is-converted-into-image-or-any-other-approach-to-realiz

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!