我想在每條線上製作一條間距爲200米的簡單樓梯線。正如你在代碼中看到的那樣,它的屏幕高度(y1_world)爲2000米,屏幕寬度(x1_world)爲1125.該代碼只能在斜線位置工作,而不是在樓梯上,這是我的問題。如何在java中使用繪製線繪製樓梯線?
有人能給我一個關於這件事的想法嗎?
下面的代碼:
public void paint(Graphics g)
{
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setBackground(Color.white);
int x0_pixel = 0;
int y0_pixel = 0;
int x1_pixel = getWidth();
int y1_pixel = getHeight();
int x0_world = 0;
int y0_world = 0;
int x1_world = 2000; // meters
int y1_world = 1125; // meters
double x_ratio = (double) x1_pixel/x1_world;
double y_ratio = (double) y1_pixel/y1_world;
double len = x1_world; // meters
double interval = 200; // meters
int x_world = 0;
int y_world = 0;
while (((y_world += interval) <= y1_world) && ((x_world +=interval) <= x1_world))
{
int x_pixel = convertToPixelX(x_world, x_ratio);
int y_pixel = convertToPixelY(y_world, y_ratio);
g2d.setColor(Color.BLUE);
g2d.drawLine(x_world, y_world, x_pixel, y_pixel);
}
Toolkit.getDefaultToolkit().sync();
g2d.dispose();
}
private static int convertToPixelY(int y_world, double y_ratio)
{
return (int) (y_world * y_ratio);
}
private static int convertToPixelX(int x_world, double ratio)
{
return (int) (x_world * ratio);
}
tnx這個提示..我已經明白了..!它現在的作品.. – sack
+15的好提示.. ;-) – sack