gpt4 book ai didi

java - 是否可以设置文本的外描边?

转载 作者:行者123 更新时间:2023-12-02 10:23:14 29 4
gpt4 key购买 nike

我正在尝试为文本制作外描边。当我增加 BasicStrokewidth 参数时,我的轮廓会在文本内部和外部展开,但我只需要外部部分。

我的结果: Outlined Text

有一些属性吗?或者也许有一种方法可以从文本中剪切笔画?

示例代码:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.geom.AffineTransform;


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

public class FontPaint extends JApplet {
public void init() {
FontPanel fontPanel = new FontPanel();
getContentPane().add(fontPanel, BorderLayout.CENTER);
}

public static void main(String[] args) {
FontPanel starPanel = new FontPanel();

JFrame f = new JFrame("Font");
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});

f.getContentPane().add(starPanel, BorderLayout.CENTER);
f.setSize(new Dimension(550, 200));
f.setVisible(true);
}
}

class FontPanel extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
setBackground(Color.white);

Graphics2D g2 = (Graphics2D) g;

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);

g2.setRenderingHint(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);

FontRenderContext frc = g2.getFontRenderContext();
Font f = new Font("Helvetica", 1, 60);
String s1 = "Java Source and Support.";
TextLayout textTl = new TextLayout(s1, f, frc);

AffineTransform transform1;
Shape outline1 = textTl.getOutline(null);
transform1 = g2.getTransform();

double newWidth1 = 301;
double newHeight1 = 427;

textTl.draw(g2, (int)newWidth1, (int)newHeight1);

transform1.translate(newWidth1, newHeight1);

g2.transform(transform1);
g2.setColor(Color.blue);
g2.setStroke(new BasicStroke(4.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND));
g2.draw(outline1);

}
}

最佳答案

可能没有什么好的“半程”。所以首先绘制轮廓,然后填充字形 vector 。 (仅适用于不透明颜色。)

transform1 = g2.getTransform();

double newWidth1 = 301;
double newHeight1 = 427;

transform1.translate(newWidth1, newHeight1);

g2.transform(transform1);
g2.setColor(Color.blue);
g2.setStroke(new BasicStroke(4.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND));
g2.draw(outline1);

textTl.draw(g2, 0, 0);

这应该是一个捷径:

...
transform1.translate(-newWidth1, -newHeight1);
textTl.draw(g2, (int)newWidth1, (int)newHeight1);

关于java - 是否可以设置文本的外描边?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54181110/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com