java/swing:将文本字符串转换为形状

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2466233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 21:18:28  来源:igfitidea点击:

java/swing: converting a text string to a Shape

javaswinggraphicstext

提问by Jason S

I want to convert some arbitrary text to a Shape (java.awt.Shape) and then stroke/fill the Shape to draw it. How can I do this?

我想将一些任意文本转换为 Shape ( java.awt.Shape),然后描边/填充 Shape 以绘制它。我怎样才能做到这一点?

回答by Savvas Dalkitsis

Hm I did not know the answer to this but after a bit tweaking and poking around in with Eclipse content assist i found this which seems to be what you need:

嗯,我不知道这个问题的答案,但是在使用 Eclipse 内容帮助进行了一些调整和探索之后,我发现这似乎是您所需要的:

EDIT: i changed to code to change the way the string is displayed which is the reason you asked what you asked :) Try it. It renders the string with red color and a dashed outline

编辑:我更改为代码以更改字符串的显示方式,这就是您问什么的原因:) 试试看。它将字符串呈现为红色和虚线轮廓

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.font.GlyphVector;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Test extends JPanel{

    private Shape s;

    public Test() {
        Font f = getFont().deriveFont(Font.BOLD, 70);
        GlyphVector v = f.createGlyphVector(getFontMetrics(f).getFontRenderContext(), "Hello");
        s = v.getOutline();
        setPreferredSize(new Dimension(300,300));
    }
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g.create();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.translate(100, 150);
        g2.rotate(0.4);
        g2.setPaint(Color.red);
        g2.fill(s);
    g2.setPaint(Color.black);
        g2.setStroke(new BasicStroke(3, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 1, new float[]{1,0.4f,1.5f}, 0));
        g2.draw(s);
    }

    public static void main(String[] args) {
        JFrame f = new JFrame("Test");
        Component c = new Test();
        f.getContentPane().add(c);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.pack();
        f.setVisible(true);
    }
}

Also note that you can get the individual characters from the string by calling:

另请注意,您可以通过调用从字符串中获取单个字符:

getGlyphOutline(glyphIndex)

回答by objects

Use the TextLayoutclass (see the getOutline() method). Theres an example here

使用TextLayout类(请参阅 getOutline() 方法)。这里有一个例子

回答by Buhake Sindi

If I understood you correctly, this is not to address your exact answer, but it's a start...

如果我理解正确的话,这不是为了解决您的确切答案,而是一个开始......

//Rough pseudo code
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.TexturePaint;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;



BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = (Graphics2D)image.getGraphics();

//Paint with texturing brush
Rectangle2D rect2D = new Rectangle2D.Double(0, 0, width, height);
graphics.setPaint(new TexturePaint(image, rect2D));
graphics.fill(rect2D);

//Draw text
graphics.drawString("my text goes here", xPos, yPos);

In Summary,

总之,

  1. Create a BufferedImageobject of widthand heightand ImageType.
  2. Get the image's Graphicsobject.
  3. Paint that graphics like you please (i.e. create a rectangle, circle, text, etc.)
  4. Write that image to a stream (file, ServletRequest, etc.)
  1. 创建BufferedImage的对象widthheightImageType
  2. 获取图像的Graphics对象。
  3. 随意绘制图形(即创建矩形、圆形、文本等)
  4. 将该图像写入流(文件、ServletRequest 等)