问题描述
我无法调整线条粗细.我可以在 Graphics 中这样做还是必须在 Graphics2D 中这样做?如果是这样,我如何更改程序以使其运行?
I am having trouble adjusting line thickness. Can I do that in Graphics or do i have to do it in Graphics2D? If so, how do I alter the program to make it run?
谢谢!
import java.applet.Applet;
import java.awt.*;
public class myAppletNumberOne extends Applet {
public void paint (Graphics page) {
//Something here???
}
}
推荐答案
是的,您必须在 Graphics2D 中执行此操作,但这几乎不是问题,因为 Swing 中的每个 Graphics 都是一个 Graphics2D 对象(它只是保留旧界面以实现兼容性原因).
Yes you have to do it in Graphics2D, but that's hardly an issue, as every Graphics in Swing is a Graphics2D object (it just keeps the old interface for compatibility reasons).
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(new BasicStroke(3));
g2.drawLine(...); //thick
...
}
如您所见,g2.setStroke(...) 允许您更改笔划,甚至还有一个 BasicStroke 可以轻松选择线宽.
As you can see, the g2.setStroke(...) allows you to change the stroke, and there's even a BasicStroke which provides for easy line width selection.
这篇关于将 Java Graphics 用于小程序时,您可以增加线条粗细吗?我不相信 BasicStroke 有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!