绘制矩形

发布于 2024-10-08 08:55:16 字数 95 浏览 0 评论 0原文

我正在编写一个程序,它接受用户的坐标,并用星号(*)在屏幕上绘制一些形状,

例如矩形,即矩形= 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 技术交流群。

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

发布评论

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

评论(5

亣腦蒛氧 2024-10-15 08:55:16

什么是“用星星画画”?如果这是具有等宽字体的文本模式,您需要以星形水平线打印它,计算正确的长度。对于矩形来说,这很容易,对于三角形来说,有点不那么容易,五边形只是一个矩形和一些三角形的组合。请参阅 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.

盗琴音 2024-10-15 08:55:16

如果它只是矩形,那么将以下内容放入您的绘制方法中应该可以工作...

int xIncrement = (int)g.getFont().getStringBounds("*", null).getWidth();  
int yIncrement = (int)g.getFont().getStringBounds("*", null).getHeight(); 
for(int i = y; i < y + height; i += xIncrement)  
    for(int j = x; j < x + width; j += yIncrement)
        g.drawString("*", j, i);  

对于三角形和其他形状,它有点困难,但您可以计算出分隔点的线的梯度,因此获得每个点的初始 x线。

一般来说,尽量避免“明星画”......

If it's only rectangles, then putting the following in your paint method should work...

int xIncrement = (int)g.getFont().getStringBounds("*", null).getWidth();  
int yIncrement = (int)g.getFont().getStringBounds("*", null).getHeight(); 
for(int i = y; i < y + height; i += xIncrement)  
    for(int j = x; j < x + width; j += yIncrement)
        g.drawString("*", j, i);  

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'...

泡沫很甜 2024-10-15 08:55:16

除非您指的是 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.

留蓝 2024-10-15 08:55:16

以下来自 javadoc

x - 新的 x 坐标
该矩形的左上角 y -
左上角的新 y 坐标
这个矩形的角

Below is from javadoc

x - the new x coordinate for the
top-left corner of this Rectangle y -
the new y coordinate for the top-left
corner of this Rectangle

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