简体   繁体   English

如何在Java中调整文字大小

[英]How to resize text in java

I have seen that in photoshop text can be easily resized just by dragging them. 我已经看到在Photoshop中,只需拖动即可轻松调整文本大小。 How can we do the same thing in Java? 我们如何在Java中做同样的事情? Any idea on how to resize text in java? 关于如何在Java中调整文本大小的任何想法? Added a snapshot of letter "A" resized in photoshop 添加了在Photoshop中调整大小的字母“ A”的快照

在此处输入图片说明


Please let me know what is wrong with this code? 请让我知道这段代码有什么问题吗?

public class ResizeImage extends JFrame {

    public ResizeImage(){
        JPanel panel = new JPanel(){
            public void paintComponent(Graphics g) {
                // In your paint(Graphics g) method
                // Create a buffered image for use as text layer
                BufferedImage textLayer = new BufferedImage(240, 240, 
                                              BufferedImage.TYPE_INT_RGB);

                // Get the graphics instance of the buffered image
            Graphics2D gBuffImg = textLayer.createGraphics();

                // Draw the string
                gBuffImg.drawString("Hello World", 10, 10);

                // Rescale the string the way you want it
                gBuffImg.scale(200, 50);

                // Draw the buffered image on the output's graphics object
                g.drawImage(textLayer, 0, 0, null);
                gBuffImg.dispose();
            }
        };
        add(panel);
    }

    public static void main(String [] args){
        ResizeImage frame = new ResizeImage();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        frame.setSize(300, 300);
        frame.setVisible(true);
    }
} 

One way is to use an AffineTransform (this variant also fades the color). 一种方法是使用AffineTransform (此变体也会使颜色褪色)。

使用Serif字体拉伸(和淡入淡出)文本

import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import javax.swing.*;
import java.io.File;
import javax.imageio.ImageIO;

public class StretchText {

    public static void main(String[] args) throws Exception {
        // used to stretch the graphics instance sideways
        AffineTransform stretch = new AffineTransform();
        int w = 640; // image width
        int h = 200; // image height
        int f = 21; // Font size in px
        String s = "The quick brown fox jumps over the lazy dog.";

        final BufferedImage bi = new BufferedImage(
                w,h,BufferedImage.TYPE_INT_RGB);
        Graphics2D g = bi.createGraphics();
        g.setFont(new Font("Serif",Font.PLAIN,f));
        g.setRenderingHint(
                RenderingHints.KEY_TEXT_ANTIALIASING, 
                RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

        // paint BG
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, w, h);
        g.setColor(Color.BLACK);

        for (int i=0; (i*f)+f<=h; i++) {
            g.drawString(s, 0, (i*f)+f);
            // stretch
            stretch.concatenate(
                    AffineTransform.getScaleInstance(1.18, 1d));
            g.setTransform(stretch);

            // fade
            Color c = g.getColor();
            g.setColor(new Color (
                    c.getRed(),
                    c.getGreen(),
                    c.getBlue(),
                    (int)(c.getAlpha()*.75)));
        }

        g.dispose();

        ImageIO.write(bi, "png", new File(
                new File(System.getProperty("user.home")), 
                "StretchText.png"));
        Runnable r = new Runnable() {
            @Override
            public void run() {
                JLabel gui = new JLabel(new ImageIcon(bi));
                JOptionPane.showMessageDialog(null, gui);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

You can use TextLayout to get the geometry, as shown here . 您可以使用TextLayout获得的几何形状,如图所示这里 The example scales the image to fill the frame. 该示例缩放图像以填充框架。 JInternalFrame might be a good choice to leverage the frame's resizing feature. JInternalFrame可能是利用框架的大小调整功能的不错选择。 Alternative, the example cited here shows how to click and drag multiple selections. 另外, 此处引用的示例显示了如何单击和拖动多个选择。

图片

u can define type of font 您可以定义字体类型

eg 例如

Font f = new Font("SansSerif", Font.BOLD, 40)

Unfortunately the java api doesn't have a native free-form scaling/transform method fonts. 不幸的是,Java api没有本地的自由格式缩放/转换方法字体。

You can however rescale a BufferedImage or Graphics object with the scale(x, y) method . 但是,您可以使用scale(x, y)方法重新缩放BufferedImage或Graphics对象。 So you can try an approach with "layers" instead. 因此,您可以尝试使用“图层”代替。 Ie draw objects, such as text, in their own layer (ie a BufferedImage) first and then on the actual graphics output. 即先在自己的图层(即BufferedImage)中绘制对象(例如文本),然后再在实际的图形输出上绘制对象。

So draw the text as usual on a BufferedImage and rescale it the way you want. 因此,像往常一样在BufferedImage上绘制文本,然后按照您想要的方式缩放它。 Here is some simple sample code to get you starting. 这是一些简单的示例代码,可以帮助您入门。

// In your paint(Graphics g) method

// Create a buffered image for use as text layer
BufferedImage textLayer = new BufferedImage(240, 240, 
                                  BufferedImage.TYPE_INT_ARGB);

// Get the graphics instance of the buffered image
Graphics2D gBuffImg = buffImg.createGraphics();

// Draw the string
gBuffImg.drawString("Hello World", 0, 0);

// Rescale the string the way you want it
gBuffImg.scale(240, 120);

// Draw the buffered image on the output's graphics object
g.drawImage(gBuffImg, 0, 0, null);

The actual size of the text layer could be determined with the help of the FontMetrics object but I'll leave that as an exercise for the OP. 文本层的实际大小可以在FontMetrics对象的帮助下确定,但我将其作为OP的练习。

This can be done at the Graphics level using Graphics.setTransform() . 可以使用Graphics.setTransform()在“图形”级别完成此操作。 However I believe it is more obvious to do this at the Font level using the lesser known Font.deriveFont(transform) . 但是,我相信使用鲜为人知的Font.deriveFont(transform)在Font级别执行此操作更为明显。 For example 例如

// create transform
AffineTransform affineTransform = new AffineTransform();
affineTransform.scale(1d, 3d);

// create font using that transform
Font stretchedFont = g.getFont().deriveFont(affineTransform);

// set font as normal
g.setFont(stretchedFont);

// render as normal
g.drawString("Stretched", 23, 45);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM