如何编辑被转换成图像的文本?或任何其他的方法来实现/编辑文本 [英] How to edit a text that is converted into image? or any other approach to realize/edit text

查看:152
本文介绍了如何编辑被转换成图像的文本?或任何其他的方法来实现/编辑文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关项目中,我要调整的文字,就像在Adobe Photoshop。但是,当我试图调整(通过鼠标拖动)textpane,只有textpane得到了调整,而不是里面的文字。然后我试图转换我的文字图像,并试图调整再次(在后提到如何来调整在Java 文本),它工作得很好。现在我有我的编辑文本的另一个问题。即一旦我的文本转换为图像,然后我怎么编辑(如:改变文字颜色等)。

对此有何想法?


解决方案

  

..一旦我的文本转换为图像,然后我怎么编辑(如:改变文字颜色等)。


不要。这是更好(质量),更容易绘制图像的每个选择与文本要求时,在所需字体,从的AffineTransform RenderinhHints 颜色(等)。

 进口java.awt中的*。
进口java.awt.geom.AffineTransform中;进口的javax.swing *。公共类StretchLabels {    字符串s =敏捷的棕色狐狸跳过懒惰的狗!
    字体的字体=新Font(Font.SERIF,Font.PLAIN,24);    公共JComponent的getGUI(){
        JPanel的GUI =新JPanel(新的BorderLayout());        JLabel的L1 =新StretchTextLabel(S);
        l1.setFont(字体);
        gui.add(L1,BorderLayout.CENTER);        返回贵;
    }    公共静态无效的主要(字串[] args)抛出异常{
        可运行R =新的Runnable(){
            @覆盖
            公共无效的run(){
                JFrame的F =新的JFrame(Streeetch我!);
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(真);                StretchLabels如=新StretchLabels();
                f.setContentPane(eg.getGUI());
                f.pack();                f.setVisible(真);
            }
        };
        SwingUtilities.invokeLater(R);
    }
}类StretchTextLabel扩展JLabel {
    私有静态最后的serialVersionUID长1L =;    公共StretchTextLabel(String s)将{
        超(S);
    }    @覆盖
    公共无效的paintComponent(图形克){
        颜色C = gr.getColor();
        setForeground(新颜色(0,0,0,0));
        super.paintComponent方法(克);
        setForeground(C);
        gr.setColor(C);
        Graphics2D的G =(Graphics2D的)克;
        g.setRenderingHint(
                RenderingHints.KEY_TEXT_ANTIALIASING,
                RenderingHints.VALUE_TEXT_ANTIALIAS_ON);        。双PW = this.get preferredSize()宽度;
        。双pH值= this.get preferredSize()高度;
        。双W = this.getSize()宽度;
        。双H = this.getSize()高度;        //设置FG(文本)颜色
        g.setColor(this.getForeground());        //舒展
        舒展的AffineTransform =
                AffineTransform.getScaleInstance(W / PW,H / PH);
        g.setTransform(拉伸);
        g.drawString(的getText(),0,this.getFont()的getSize());
    }
}


  我

..如何编辑图像中的文字?


我通常提供的控制做到这一点是这样的:

不过,当然,这不包括:


  1. 颜色,它在主界面控制(基本涂料的应用程序。)结果

  2. 将拖累作为结果的文本缩放。这可能通过使用可调整大小的元素通过两种@trashgod或证明自己(上图)来实现。

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?

解决方案

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

这篇关于如何编辑被转换成图像的文本?或任何其他的方法来实现/编辑文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆