Java2D:增加线宽
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2839508/
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
Java2D: Increase the line width
提问by Boolean
I want to increase the Line2D width. I could not find any method to do that. Do I need to actually make a small rectangle for this purpose?
我想增加 Line2D 宽度。我找不到任何方法来做到这一点。我真的需要为此目的制作一个小矩形吗?
采纳答案by aioobe
You should use setStroke
to set a stroke of the Graphics2D
object.
您应该使用setStroke
来设置Graphics2D
对象的描边。
The example at http://www.java2s.comgives you some code examples.
http://www.java2s.com 上的示例为您提供了一些代码示例。
The following code produces the image below:
下面的代码产生下面的图像:
import java.awt.*;
import java.awt.geom.Line2D;
import javax.swing.*;
public class FrameTest {
public static void main(String[] args) {
JFrame jf = new JFrame("Demo");
Container cp = jf.getContentPane();
cp.add(new JComponent() {
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(new BasicStroke(10));
g2.draw(new Line2D.Float(30, 20, 80, 90));
}
});
jf.setSize(300, 200);
jf.setVisible(true);
}
}
(Note that the setStroke
method is not available in the Graphics
object. You have to cast it to a Graphics2D
object.)
(请注意,该setStroke
方法在Graphics
对象中不可用。您必须将其强制转换为Graphics2D
对象。)
This post has been rewritten as an article here.
这篇文章在这里被改写为一篇文章。
回答by ZhaoGang
What is Stroke
:
什么是Stroke
:
The BasicStroke class defines a basic set of rendering attributes for the outlines of graphics primitives, which are rendered with a Graphics2D object that has its Stroke attribute set to this BasicStroke.
BasicStroke 类为图形基元的轮廓定义了一组基本的呈现属性,这些属性是用 Graphics2D 对象呈现的,该对象的 Stroke 属性设置为此 BasicStroke。
https://docs.oracle.com/javase/7/docs/api/java/awt/BasicStroke.html
https://docs.oracle.com/javase/7/docs/api/java/awt/BasicStroke.html
Note that the Stroke
setting:
注意Stroke
设置:
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(new BasicStroke(10));
is setting the line width,since BasicStroke(float width)
:
正在设置线宽,因为BasicStroke(float width)
:
Constructs a solid BasicStroke with the specified line width and with default values for the cap and join styles.
构造一个具有指定线宽和帽和连接样式的默认值的实心 BasicStroke。
And, it also effects other methods like Graphics2D.drawLine(int x1, int y1, int x2, int y2)
and Graphics2D.drawRect(int x, int y, int width, int height)
:
而且,它还会影响其他方法,例如Graphics2D.drawLine(int x1, int y1, int x2, int y2)
和Graphics2D.drawRect(int x, int y, int width, int height)
:
The methods of the Graphics2D interface that use the outline Shape returned by a Stroke object include draw and any other methods that are implemented in terms of that method, such as drawLine, drawRect, drawRoundRect, drawOval, drawArc, drawPolyline, and drawPolygon.
使用 Stroke 对象返回的轮廓 Shape 的 Graphics2D 接口的方法包括 draw 和根据该方法实现的任何其他方法,例如 drawLine、drawRect、drawRoundRect、drawOval、drawArc、drawPolyline 和 drawPolygon。