2011-10-10 137 views
0

我想在每條線上製作一條間距爲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); 
    } 

回答

2

你做得太少:你應該劃一條線,然後在右邊劃一條線。如果我是你,我會封裝在一個「樓梯」功能:

public void step(Graphics2d g) { 
    Point midPoint = getMidPoint(); 
    Point endPoint = getEndPoint(); 
    drawStep(g, currentPoint, midPoint, endPoint); 
    currentPoint = endPoint; 
} 

public void drawStep(Graphics2d g, Point first, Point mid, Point last) { 
    g.drawLine(first.x, first.y, mid.x, mid.y); 
    g.drawLine(mid.x, mid.y, last.x, last.y); 
} 

public Point getMidPoint(){ 
    return new Point(currentPoint.x, currentPoint.y + stepHeight); 
} 

public Point getEndPoint(){ 
    return new Point(currentPoint.x + stepWidth, currentPoint.y + stepHeight); 
} 

你做得太多,太:縮放圖像到你的視口恰好是AffineTransform專業(這裏有一個brief intro

public void paint(Graphics gx) { 
    Graphics2D g = (Graphics2D) gx; 

    AffineTransform scale = AffineTransform.getScaleInstance(
      xPixels/numberOfSteps*stepWidth, 
      yPixels/numberOfSteps*stepHeigth); 
    g.transform(scale); 
    for(int i = 0; i < numberOfSteps; ++ i) { 
     step(g); 
    } 
} 

免責聲明:代碼是未編譯的,未經檢驗 - 旨在給出提示。

+0

tnx這個提示..我已經明白了..!它現在的作品.. – sack

+0

+15的好提示.. ;-) – sack

1

drawLine不畫一個臺階。你要畫兩條線:一個水平和垂直的一:

g2d.drawLine(x_world, y_world, x_pixel, y_world); // keep y constant 
    g2d.drawLine(x_pixel, y_world, x_pixel, y_pixel); // keep x constant 
+0

tnx的想法,它有很大的幫助.. – sack

0

這可能不是正確的答案,但它好像你可能需要建立一個循環來畫線呈階梯狀:

bool vert = false; 

while(x_pixel <= x_world){ 
    if (vert){ 
     g.drawLine(x, y); 
     vert = True; 
    else{ 
     g.drawLine(y,x); 
     vert = False; 

這不是確切的代碼!只是一個可能工作的一般概念。

我希望這是有道理的。你只是試圖讓它先畫一條垂直線然後畫一條水平線,然後重複。而不僅僅是一條長長的路線。

+0

hm ...一個隱藏的狀態機有兩個狀態?這是不是有點矯枉過正? – xtofl

+0

乾杯隊友爲反對票。鼓勵新用戶回答問題的好方法。 – JackalopeZero

+0

真的很抱歉,我不喜歡這個答案。不要把它看作個人,你也會從「社區」中獲得你的最高票數。事實上,請不要猶豫,以減少你不同意的答案 - 這是我從比我聰明的人那裏學習的方式。 – xtofl