5

我已经看到,在 Photoshop 中,只需拖动它们即可轻松调整文本的大小。我们如何在 Java 中做同样的事情?关于如何在java中调整文本大小的任何想法?添加了在 Photoshop 中调整大小的字母“A”的快照

在此处输入图像描述


请让我知道这段代码有什么问题?

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);
    }
} 
4

5 回答 5

11

一种方法是使用一个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);
    }
}
于 2012-11-18T13:25:21.030 回答
4

您可以使用来获取几何图形,如此TextLayout所示。该示例缩放图像以填充框架。可能是利用框架的调整大小功能的好选择。或者,此处引用的示例显示了如何单击和拖动多个选择。JInternalFrame

图片

于 2012-11-18T15:18:49.820 回答
3

你可以定义字体类型

例如

Font f = new Font("SansSerif", Font.BOLD, 40)
于 2012-11-18T12:54:28.730 回答
1

不幸的是,java api 没有原生的自由格式缩放/转换方法字体。

scale(x, y)但是,您可以使用方法重新缩放 BufferedImage 或 Graphics 对象。因此,您可以尝试使用“层”的方法。即先在自己的图层(即一个BufferedImage)中绘制对象,例如文本,然后再在实际的图形输出上。

所以像往常一样在 BufferedImage 上绘制文本并按照你想要的方式重新调整它。这是一些简单的示例代码,可帮助您入门。

// 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);

文本层的实际大小可以在 FontMetrics 对象的帮助下确定,但我将把它作为 OP 的练习。

于 2012-11-18T13:49:51.377 回答
0

这可以在图形级别使用Graphics.setTransform(). 但是我相信在字体级别使用鲜为人知的Font.deriveFont(transform). 例如

// 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);
于 2017-10-21T05:04:36.643 回答