在两点之间画一条线
你好 我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 Swing 中:
如果您在
JPanel
上绘图,通常会将此代码放入paintComponent
方法中:要查看
Graphics
上的所有可用方法> 类,请参阅 Javadocs。In Swing:
IF you are drawing on a
JPanel
, you will usually put this code in thepaintComponent
method:To see all available methods on the
Graphics
class, see the Javadocs.看一下 Graphics.drawLine 方法。
您基本上需要重写一些小部件(如 JPanel)或获取 Canvas,并在 Paint 方法中执行以下操作:
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:
对于 JFrame,您可以在继承 JFrame 类的类内部添加一个 Paint 方法,当 JVM 准备好在 JFrame 上绘图时运行该方法。然后,在其中,您将调用图形的“drawLine”方法,如所示(确保“Graphics”类已导入,并将 X1、Y1、X2、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.):
您也可以尝试以下操作:
现在将该函数调用为:
您可以将
var 点数组
更改为您喜欢的任何点。这应该用黑线连接所有点。如果输入三个点,它会形成一个三角形,四个点会形成一个正方形,依此类推。如果我犯了错误,请告诉我。
You can also try this:
Now call the function as:
You can change the
var points array
to any points you like.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.