在两点之间画一条线

发布于 2024-10-03 14:01:59 字数 77 浏览 5 评论 0原文

你好 我有 2 个点 (X1,Y1)(X2,Y2) 如何在它们之间画一条线? 谢谢

Hi
I have 2 points (X1,Y1) and (X2,Y2) how can I draw a line between them?
thanks

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

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

发布评论

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

评论(4

絕版丫頭 2024-10-10 14:01:59

在 Swing 中:

Graphics g;
g.drawLine(X1, Y1, X2, Y2);

如果您在 JPanel 上绘图,通常会将此代码放入 paintComponent 方法中:

@Override
protected void paintComponent(Graphics g) {
    g.drawLine(X1, Y1, X2, Y2);
}

要查看 Graphics 上的所有可用方法> 类,请参阅 Javadocs

In Swing:

Graphics g;
g.drawLine(X1, Y1, X2, Y2);

IF you are drawing on a JPanel, you will usually put this code in the paintComponent method:

@Override
protected void paintComponent(Graphics g) {
    g.drawLine(X1, Y1, X2, Y2);
}

To see all available methods on the Graphics class, see the Javadocs.

所有深爱都是秘密 2024-10-10 14:01:59

看一下 Graphics.drawLine 方法。

您基本上需要重写一些小部件(如 JPanel)或获取 Canvas,并在 Paint 方法中执行以下操作:

graphics.drawLine( p1.x, p1.y, p2.x, p2.y );

Take a look at the Graphics.drawLine method.

You'll basically need to override some widget (like JPanel) or get a Canvas and in the paint method you do something like:

graphics.drawLine( p1.x, p1.y, p2.x, p2.y );
婴鹅 2024-10-10 14:01:59

对于 JFrame,您可以在继承 JFrame 类的类内部添加一个 Paint 方法,当 JVM 准备好在 JFrame 上绘图时运行该方法。然后,在其中,您将调用图形的“drawLine”方法,如所示(确保“Graphics”类已导入,并将 X1、Y1、X2、Y2 替换为您选择的整数。):

public void paint(Graphics g) {
    g.drawLine(X1,X2,Y1,Y2);
}

For a JFrame, you would add a paint method, which is ran when the JVM is ready to draw on the JFrame, inside of the class that has inherited the JFrame class. Then, inside of that, you would call the 'drawLine' method of the graphic, as demonstrated (ensure that the "Graphics" class has been imported and replace the X1, Y1, X2, Y2 with the integars of your choice.):

public void paint(Graphics g) {
    g.drawLine(X1,X2,Y1,Y2);
}
折戟 2024-10-10 14:01:59

您也可以尝试以下操作:

var draw = function(ctx,x1,y1,x2,y2) {

    ctx.strokeStyle = "Black";
    ctx.lineWidth = 4;
    ctx.beginPath();
    ctx.moveTo(x1,y1);
    ctx.lineTo(x2,y2);
    ctx.stroke();

};


var drawPoints = function(ctx,points) {

    ctx.strokeStyle = "Black";
    ctx.lineWidth = 4;
    for(var i = 0; i<points.length -1;i++){
        draw(ctx,points[i][0],points [i][1],points[i+1][0],points[i+1][1]);
    }

};


var ctx = canvas.getContext("2d")

现在将该函数调用为:

drawPoints(ctx, points);

您可以将 var 点数组 更改为您喜欢的任何点。

var points = [[50,50],[50,100],[100,100],[100,50]];

这应该用黑线连接所有点。如果输入三个点,它会形成一个三角形,四个点会形成一个正方形,依此类推。如果我犯了错误,请告诉我。

You can also try this:

var draw = function(ctx,x1,y1,x2,y2) {

    ctx.strokeStyle = "Black";
    ctx.lineWidth = 4;
    ctx.beginPath();
    ctx.moveTo(x1,y1);
    ctx.lineTo(x2,y2);
    ctx.stroke();

};


var drawPoints = function(ctx,points) {

    ctx.strokeStyle = "Black";
    ctx.lineWidth = 4;
    for(var i = 0; i<points.length -1;i++){
        draw(ctx,points[i][0],points [i][1],points[i+1][0],points[i+1][1]);
    }

};


var ctx = canvas.getContext("2d")

Now call the function as:

drawPoints(ctx, points);

You can change the var points array to any points you like.

var points = [[50,50],[50,100],[100,100],[100,50]];

This should connect all the points with a black line. If you enter three points it makes a triangle, with four, a square and so on. Please let me know if I made a mistake.

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