java文本区大小,如何在java中调整文本大小

该篇博客介绍了如何在Java中模仿Photoshop中的文字缩放功能,通过使用AffineTransform和BufferedImage进行图形操作,展示了如何调整字体大小并创建动态效果。读者将学习如何在绘制组件时,通过图形上下文动态改变文本大小。
摘要由CSDN通过智能技术生成

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

QnS4q.png

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

zy2q4.png

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

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值