0

Hey guys so I thought of an idea and so far I've made some progress. I want to be able to change my levels terrain based on coordinates. The coordinates are selected based on where you click and is added on an ArrayList of Points. These points are then sorted by their x value and then drawn as follows:

public Image renderTerrain() throws SlickException {

    Image image = new Image(MAP_WIDTH, MAP_HEIGHT);

    Graphics gr = image.getGraphics();

    int startx = 0;
    int starty = 0;

    for(Point point : points) {

        starty = (int) point.getY();

        int tox = (int) point.getX();
        int toy = (int) point.getY();


        for(int x = startx; x < tox; x++) {
            for(int y = starty; y < MAP_HEIGHT; y++) {
                gr.drawLine(x,y,x,y);
            }
        }

        startx = tox;

    }

    gr.flush();

    return image;
}

This would render something like so:

enter image description here

However What I want to achieve is like so.

enter image description here

What alterations would I need to make in order to achieve this?

Thanks.

Duncan Palmer
  • 2,865
  • 11
  • 63
  • 91

1 Answers1

0

I answered my own question while waiting for an answer.

Here is my solution:

public Image renderTerrain() throws SlickException {

    Image image = new Image(MAP_WIDTH, MAP_HEIGHT);

    Graphics gr = image.getGraphics();

    int startx = 0;
    int starty = (int)points.get(0).getY();

    for(Point point : points) {


        int tox = (int) point.getX();
        int toy = (int) point.getY();

        gr.setColor(Color.pink);//line
        gr.drawLine(startx,starty,tox,toy);
        gr.setColor(Color.green); //height
        gr.drawLine(tox, toy, tox, MAP_HEIGHT);
        gr.setColor(Color.yellow); //base
        gr.drawLine(tox, MAP_HEIGHT-1, startx, MAP_HEIGHT-1);
        gr.drawLine(tox, MAP_HEIGHT-2, startx, MAP_HEIGHT-2);

        startx = tox;
        starty = toy;
    }

    gr.flush();

    return image;
}

Outcome:

enter image description here

Duncan Palmer
  • 2,865
  • 11
  • 63
  • 91