简体   繁体   English

如何在Java中使用绘制线绘制楼梯线?

[英]How to draw a stairs line using a drawline in java?

I want to make a simple stairway line having an interval distance of 200 meters in every line. 我想制作一条简单的阶梯线,每条线的间隔距离为200米。 As you can see in the code, it has a screen height(y1_world) of 2000 meters and a screen width(x1_world) of 1125. The code works only in a slant position of lines and not in a stairway and that is my problem. 正如您在代码中看到的那样,它的屏幕高度(y1_world)为2000米,屏幕宽度(x1_world)为1125。该代码仅在倾斜的线条位置而不在楼梯上起作用,这就是我的问题。

Could someone give me an idea about this matter? 有人可以给我一个关于这件事的想法吗?

Here's the code: 这是代码:

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);
   }

You're doing too little : you should draw a line up, then a line to the right. 您做得太少了 :您应该画一条线,然后在右边画一条线。 If I were you, I would encapsulate that in a 'stair' function: 如果我是你,我会将其封装在“阶梯”函数中:

 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 );
}

You're doing too much , too: scaling your image to your viewport happens to be the specialty of AffineTransform (here's a brief intro ) 您也做得太多 :将图像缩放到视口恰好是AffineTransform的特色(这里是一个简短的介绍

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 );
    }
 }

Disclaimer: code is uncompiled, untested - intended to give a hint. 免责声明:代码未经编译,未经测试-旨在提供提示。

A single drawLine does not draw a stair. 一条drawLine不会绘制楼梯。 You have to draw two lines: A horizontal and a vertical one: 您必须画两条线:水平线和垂直线:

    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

This may not be the right answer, but it seems like you might need to setup a loop to draw the lines in a step shape: 这可能不是正确的答案,但是似乎您可能需要设置一个循环才能以阶梯形状绘制线条:

bool vert = false;

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

This is not the exact code! 这不是确切的代码! Just a general idea of what might work. 只是对可能有用的一般想法。

I hope this makes sense. 我希望这是有道理的。 You are just trying to get it to draw a vertical line first then a horizontal line, then repeat. 您只是想让它先绘制一条垂直线,然后再绘制一条水平线,然后重复。 Rather than just one long line. 而不只是一条长线。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM