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

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

Java2D: Increase the line width

javaswinggraphicsjava-2d

提问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 setStroketo set a stroke of the Graphics2Dobject.

您应该使用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);
    }
}

enter image description here

在此处输入图片说明

(Note that the setStrokemethod is not available in the Graphicsobject. You have to cast it to a Graphics2Dobject.)

(请注意,该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 Strokesetting:

注意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。