如何在java中调整文本大小

我已经看到,在Photoshop中的文本可以很容易地resize拖动它们。 我们如何在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); } } 

一种方法是使用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); } } 

您可以使用TextLayout来获取几何graphics,如下所示。 该示例缩放图像以填充框架。 JInternalFrame可能是利用框架resizefunction的不错select。 另外, 这里引用的例子显示了如何点击和拖动多个select。

图片

你可以定义字体的types

例如

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

不幸的是,java api没有一个本地的自由格式缩放/转换方法字体。

但是,您可以使用scale(x, y)方法重新调整BufferedImage或Graphics对象的scale(x, y) 。 所以你可以用“图层”来尝试一种方法。 即在他们自己的图层(即一个BufferedImage)中,然后在实际的graphics输出中绘制对象,如文本。

因此,像往常一样在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的练习。

这可以使用Graphics.setTransform()在Graphics级别完成。 不过,我相信在字体级别使用较less的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);