注册 | 登录

解决How to draw a stairs line using a drawline in java?

itPublisher 分享于



推荐:How to run java and junit in command line

1、一般初学Java时,安装jdk后,都会在命令行用javac编译一下程序,然后用java运行。如javac ,会生成一个HelloWorld.class的文件,然后java He

I want to make a simple stairway line having an interval distance of 200 meters in every line. 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.

Could someone give me an idea about this matter?

Here's the code:

public void paint(Graphics g)

    Graphics2D g2d = (Graphics2D) g;

    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.drawLine(x_world, y_world, x_pixel, y_pixel);



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);
java line draw paint
  this question
asked Oct 10 '11 at 8:44 sack 459 3 11 39


3 Answers


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)

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

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

Disclaimer: code is uncompiled, untested - intended to give a hint.

  this answer
edited Oct 10 '11 at 9:30 answered Oct 10 '11 at 9:23 xtofl 28.4k 6 66 141      tnx for this hint.. I already got it..! it works now.. –  sack Oct 10 '11 at 12:23      +15 for the good hint.. ;-) –  sack Oct 10 '11 at 12:27


A single drawLine does not draw a stair. 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 answer
answered Oct 10 '11 at 8:50 Ingo Kegel 25.7k 5 41 72      tnx for the idea, it helps a lot.. –  sack Oct 10 '11 at 12:23


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

  this answer
answered Oct 10 '11 at 8:56 JackalopeZero 2,101 6 19 48      hm... A hidden state machine with two states? Isn't that a bit overkill? –  xtofl Oct 10 '11 at 9:01      Cheers mate for the down vote. Good way to encourage new users to answer questions. –  JackalopeZero Oct 10 '11 at 9:39      Really sorry that I didn't like this answer. Don't take it personal, you will get your
  of up-votes, too, from 'the community'. In fact, please don't hesitate to downvote answers you disagree with- that's the way I learn from people smarter than me. –  xtofl Oct 10 '11 at 9:54      Well the answer isnt 'not useful'. This approach would still work. In fact Ive used it myself in some of my games.Also, I purposely wrote 'This may not be the right answer'. Sorry if Im not 'smarter than you', was just trying to offer some guidance from my experiences. –  JackalopeZero Oct 10 '11 at 10:27      @Ronny yeah i know.. but it also helps me anyway.. tnx for the code.. ;-) –  sack Oct 10 '11 at 12:26  |  show more comments








您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱