Java2D:增加线宽

发布于 2024-09-01 07:16:54 字数 56 浏览 6 评论 0原文

我想增加 Line2D 宽度。我找不到任何方法来做到这一点。我是否需要为此目的实际制作一个小矩形?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

緦唸λ蓇 2024-09-08 07:16:54

您应该使用 setStroke 来设置 Graphics2D 对象的笔划。

http://www.java2s.com 中的示例给出你一些代码示例。

以下代码生成以下图像:

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);
    }
}

在此处输入图像描述

(请注意,setStroke 方法是在 Graphics 对象中不可用,您必须将其转换为 Graphics2D 对象。)


这篇文章已被重写为文章 此处

You should use setStroke to set a stroke of the Graphics2D object.

The example at http://www.java2s.com gives you some code examples.

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 setStroke method is not available in the Graphics object. You have to cast it to a Graphics2D object.)


This post has been rewritten as an article here.

梦回旧景 2024-09-08 07:16:54

什么是中风

BasicStroke 类定义了一组基本的渲染属性
图形基元的轮廓,用
其 Stroke 属性设置为此的 Graphics2D 对象
基本笔划。

https://docs.oracle.com/javase/7 /docs/api/java/awt/BasicStroke.html

请注意,Stroke 设置:

Graphics2D g2 = (Graphics2D) g;
g2.setStroke(new BasicStroke(10));

正在设置线宽,因为 BasicStroke(float width)

使用指定的线宽以及端点和连接样式的默认值构造一个实心 BasicStroke。

而且,它还会影响其他方法,例如 Graphics2D.drawLine(int x1, int y1, int x2, int y2) 和 Graphics2D.drawRect(int x, int y, int width, int height) ):

Graphics2D接口中使用轮廓Shape的方法
由 Stroke 对象返回的包括绘制和任何其他方法
都是按照该方法实现的,例如drawLine,drawRect,
drawRoundRect、drawOval、drawArc、drawPolyline 和 drawPolygon。

What is 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.

https://docs.oracle.com/javase/7/docs/api/java/awt/BasicStroke.html

Note that the Stroke setting:

Graphics2D g2 = (Graphics2D) g;
g2.setStroke(new BasicStroke(10));

is setting the line width,since BasicStroke(float width):

Constructs a solid BasicStroke with the specified line width and with default values for the cap and join styles.

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):

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文