drawing rectangle

徘徊边缘 提交于 2019-12-11 07:07:03

问题


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??


回答1:


I think that this tutorial is very useful for you: http://download.oracle.com/javase/tutorial/2d/geometry/primitives.html




回答2:


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.




回答3:


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




回答4:


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.




回答5:


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



来源:https://stackoverflow.com/questions/4493087/drawing-rectangle

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!