Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

image - How to resize text in java

I have seen that in photoshop text can be easily resized just by dragging them. How can we do the same thing in Java? Any idea on how to resize text in java? Added a snapshot of letter "A" resized in photoshop

enter image description here


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);
    }
} 
Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

One way is to use an AffineTransform (this variant also fades the color).

Stretch (& fade) using a Serif font for the text

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...