2 Replies - 203 Views - Last Post: 18 May 2014 - 04:28 AM Rate Topic: -----

#1 bizonpp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 17-May 14

Creating a window in 2D ch array (to draw inside)

Posted 17 May 2014 - 10:41 PM

Hello. I am relatively new to java, and i am trying to create a window inside of a 2d char array, and eventually i will have to draw other shapes in this window. so for example

* * * * * * * * *
* *
* *
* *
* *
* *
* *
* *
* * * * * * * * *

The problem is my window is not drawing correctly too the border, but a couple extra chars on the x columns. Any help is appreciated. here is code. The dimensions of the window will eventually be passed through scanner in main, if i ever work out how to even draw it.
Also, in class we never learnt to use the Graphics class, so im pretty sure we are not supposed to use it.

public class Window 
{   //default values
    private int xRow;
    private int yCol;
    private char ch; 

    
    public char[][] windowz = new char[30][20];  //30,20 (yx)flat values cuz doesnt work
    
    public Window (int xRow, int yCol, char ch) //constructor to import values
    {                                           //from main ONE DAY
        this.xRow = xRow;
        this.yCol = yCol;
        this.ch = ch;
    }
    
    public void drawWindowArray()
    {
        for(int i=0;i<30;i++) //y
        {
            for(int j=0;j<20;j++) //x
            {
                
                if(j==1 || j==19)
                {
                    System.out.println("i: "+i+" j: "+j);
                  windowz[i][j] = ch;
                }
                
                if(j>1 && i==0 ) //top col,x
                  {
                    
                     windowz[i][j] = (ch);      
                   }
                if(j>1 && i==windowz.length-1) //bottom col,x
               {
                   windowz[i][j]=ch;
                }
            }
        }
    }
    
    public void printArray()
    {
        for(int i=0;i<windowz.length;i++) //y or ROWS
        {
            System.out.println(); //new line every Y
            for(int j=0;j<windowz[j].length;j++) //x or COLS
            {
                System.out.print(windowz[i][j]);
            }
        }
    }
        
}



sigh. couldnt even draw it right here :P it just has to be a square/rectangle really...

Is This A Good Question/Topic? 0
  • +

Replies To: Creating a window in 2D ch array (to draw inside)

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2699
  • View blog
  • Posts: 11,367
  • Joined: 20-September 08

Re: Creating a window in 2D ch array (to draw inside)

Posted 18 May 2014 - 02:25 AM

Don't be too hard on yourself. It's actually quite a subtle point. The jvm initialises primitives to zero values, which in the case of your char matrix will be a null char - which won't print properly. You will have to initialize it before you draw your frame borders (or anything else), such that it will be filled with blanks as opposed to nulls.

    public void initMatrix(char[][] matrix) {
        for (int i = 0; i < matrix.length; i++) {
            java.util.Arrays.fill(matrix[i], ' ');
        }
    }


Hint: 'never' use magic numbers in code. You can derive them all in your case from instance variables you should be using to store the width and height of your matrix.

This post has been edited by g00se: 18 May 2014 - 02:29 AM
Reason for edit:: Clarification

Was This Post Helpful? 1
  • +
  • -

#3 bizonpp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 17-May 14

Re: Creating a window in 2D ch array (to draw inside)

Posted 18 May 2014 - 04:28 AM

View Postg00se, on 18 May 2014 - 02:25 AM, said:

Don't be too hard on yourself. It's actually quite a subtle point. The jvm initialises primitives to zero values, which in the case of your char matrix will be a null char - which won't print properly. You will have to initialize it before you draw your frame borders (or anything else), such that it will be filled with blanks as opposed to nulls.

    public void initMatrix(char[][] matrix) {
        for (int i = 0; i < matrix.length; i++) {
            java.util.Arrays.fill(matrix[i], ' ');
        }
    }


Hint: 'never' use magic numbers in code. You can derive them all in your case from instance variables you should be using to store the width and height of your matrix.



Ah of course! Thank you very much :) And yes, i was just using them for testing purposes. Same with that random println in the loop, was trying to see where the error was. Lets hope i can get this done now! :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1