绘制矩形
我正在编写一个程序,它接受用户的坐标,并用星号(*)在屏幕上绘制一些形状,
例如矩形,即矩形= 100,150,50,50 作为x,y,宽度,高度。我该怎么办?
I am writing a program that accepts coordinates from the user and drawing some shapes on the screen with stars(*)
e.g Rectangle i.e rectangle=100,150,50,50 as x,y,width,height. how can I do it??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为本教程对您非常有用:http://download. oracle.com/javase/tutorial/2d/geometry/primitives.html
I think that this tutorial is very useful for you: http://download.oracle.com/javase/tutorial/2d/geometry/primitives.html
什么是“用星星画画”?如果这是具有等宽字体的文本模式,您需要以星形水平线打印它,计算正确的长度。对于矩形来说,这很容易,对于三角形来说,有点不那么容易,五边形只是一个矩形和一些三角形的组合。请参阅 Bresenham 算法 获取灵感。
另请注意,文本模式分辨率相当差;标准终端窗口只有 80 个字符宽,而且您几乎不可能让它比 200-300 个字符更宽,因此正确的舍入很重要。
What 'drawing with stars' is? If this is text mode with monospaced font, you'll need to print it in horizontal lines of stars, calculating right lengths. For rectangles this is easy, for triangles, a bit less easy, pentagon is just a combination of a rectangle and some triangles. See Bresenham algorithm for inspiration.
Also note that text mode resolution is quite poor; standard terminal window in only 80 chars wide, and you can hardly have it far wider than say 200-300 chars, so correct rounding is important.
如果它只是矩形,那么将以下内容放入您的绘制方法中应该可以工作...
对于三角形和其他形状,它有点困难,但您可以计算出分隔点的线的梯度,因此获得每个点的初始 x线。
一般来说,尽量避免“明星画”......
If it's only rectangles, then putting the following in your paint method should work...
For triangles and other shapes it's a bit tougher, but you can figures out the gradients of the lines separating the points, and therefore get the initial x for each line.
Generally, try to avoid 'star drawing'...
除非您指的是 GUI,否则 Y 坐标可以是您必须“跳过”的行数,而 x 坐标类似于您在行中输入的空格数。
所以基本上,x = 2 和 y = 3 意味着您必须向下移动 3 行并向右移动 2 个空格。
Unless you are referring to GUI, the Y coordinate can be the amount of lines you have to 'skip' and the x coordinate resemble the amount of spaces you have enter in the line.
So basically, x = 2 and y = 3 means that you will have to go down 3 lines and move 2 spaces to the right.
以下来自 javadoc
Below is from javadoc