我已经看到,在photoshop中的文字可以很容易地只是通过拖动调整大小。 我们怎样才能做到在Java中同样的事情? 如何在Java中调整文本的任何想法? 新增信快照“A”调整大小在Photoshop
请让我知道什么是错的代码?
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
(此变体也变淡的颜色)。
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
获得的几何形状,如图所示这里 。 的示例缩放图像以填充帧。 JInternalFrame
可能是一个不错的选择,以充分利用该框架的调整大小功能。 替代,引用的例子在这里展示了如何单击并拖动多重选择。
u能定义字体的类型
如
Font f = new Font("SansSerif", Font.BOLD, 40)
不幸的是,Java API的没有一个原生形式自由的缩放/变换方法字体。
但是,可以重新调整一个BufferedImage或图形对象与所述scale(x, y)
的方法 。 所以,你可以尝试用“层”来代替的方法。 即,第一,然后绘制对象,诸如文本,在他们自己的层(即一个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。
这可以在图形进行水平使用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);