samedi 25 février 2017

Draw square in java without Java Graphics

How would we make a square pattern on a canvas, given two of its coordinates.

The following function is supposed to draw a square with the given coordinates (x1,y1), (x3,y3) on the canvas of size 20x16 -

public static void main(String[] args) {
    DrawMe(20, 16, 16, 14, 8, 14);        
}

    public static void DrawMe(int yaxis, int xaxis, int x1, int y1, int x3, int y3) { 
    int l=Math.abs(x3-x1); int l2=l/2;
    int x2=x1+l2; int y2=y1+l2;
    int x4=x1+l2; int y4=y1-l2;
    char [ ] [ ] canvas = new char [xaxis] [yaxis]; 
    for (int x = 0; x < xaxis; x++) { 
        for (int y = 0; y < yaxis; y++) { 

            if((y==x1 && x==y1) || (y==x2 && x==y2) || (y==x3 && x==y3) || (y==x4 && x==y4))
                //canvas[x][y]='#';
                System.out.print('#');

            else { 
                //canvas[x][y]='.'; 
                System.out.print('.');
            }                
        }  
        System.out.println();
    }/
}

But, the code gives me an output like this -

....................                                                                                                                                                                                 
....................                                                                                                                                                                                 
....................                                                                                                                                                                                 
....................                                                                                                                                                                                 
....................                                                                                                                                                                                 
....................                                                                                                                                                                                 
....................                                                                                                                                                                                 
....................                                                                                                                                                                                 
....................                                                                                                                                                                                 
....................                                                                                                                                                                                 
....................                                                                                                                                                                                 
....................                                                                                                                                                                                 
....................                                                                                                                                                                                 
........#.......#...                                                                                                                                                                                 
....................

Whereas I want something like this -

....................                                                                                                                                                                                 
....................                                                                                                                                                                                 
....................                                                                                                                                                                                 
....................                                                                                                                                                                                 
....................                                                                                                                                                                                 
....................                                                                                                                                                                                 
....................                                                                                                                                                                                 
....................                                                                                                                                                                                 
....................                                                                                                                                                                                 
....................                                                                                                                                                                                 
............#.......
..........#####.....
.........#######....
........#########...
.........#######....

I tried many things but it is just not happening. Could anyone please help me figuring it out? It'd be a great help!

Please excuse me if the question seems silly. Just trying to get some basics clear. Thank you in advance.

Aucun commentaire:

Enregistrer un commentaire