#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Frame Buffer Line drawing


    Hi all,
    Can anyone give an example of frame buffer line drawing?
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    See the answer on the other forum you posted this question on.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2011
    Posts
    5
    Rep Power
    0
    Originally Posted by NormR
    See the answer on the other forum you posted this question on.
    Originally Posted by NormR
    What is the output that the program generates?
    What's wrong with it?

    You need to draw a grid on paper and look at it line by line to see what must be output for each line
    the thing is I'm lost in writing the display and clear function. Thank
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2011
    Posts
    50
    Rep Power
    4
    It looks as though you are confusing pixels with characters. Pixels are the individual 'dots' that make up a screen.

    You cannot clear characters from the text console per say. It takes a dirty little hack like this to clear the console's output:

    Code:
    public void clear() {
      for (int i = 0; i < 23; i++) {
         System.out.print("\n");
      }
    }
    You can display your 2D array of booleans like so:
    Code:
    public void display(boolean[][] pixels) {
      for (int x = 0; x < pixels.length; x++) {
         for (int y = 0; y < pixels[].length; y++) {
             if (pixels[x][y]  == true) {
                 System.out.println("*");
             } else {
                 System.out.println(" ");
             }
        }
    }
  8. #5
  9. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Cross posted at:

    http://www.javaprogrammingforums.com/whats-wrong-my-code/8974-java-frame-buffer-line-drawing.html#post32374
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2011
    Posts
    5
    Rep Power
    0
    Thanks a lot,
  12. #7
  13. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    got the following error when trying to compile. any suggestion ?
    NOTE: NPEs occur in execution not at compile.

    Exception in thread "main" java.lang.NullPointerException
    at ConsoleFrameBuffer.display(ConsoleFrameBuffer.java:24)
    at ConsoleFrameBuffer.main(ConsoleFrameBuffer.java:41)
    What variable at line 24 is null?
    The display() method is called from line 41
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2011
    Posts
    5
    Rep Power
    0
    Originally Posted by NormR
    What variable at line 24 is null?
    The display() method is called from line 41

    Thanks
  16. #9
  17. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    What variable at line 24 is null?
    And why is that variable null?

    Where has pixels been assigned a value?
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2011
    Posts
    50
    Rep Power
    4
    Code:
    public class ConsoleFrameBuffer { 
    
        private boolean pixels[][]; 
        private int width, height; 
    
        public ConsoleFrameBuffer(int width, int height) 
    	{
    		this.height=height; 
    		this.width=height;
    		pixels = new boolean[width][height];
    	}     
        
    	public ConsoleFrameBuffer() 
    	{
    		this.width=79;
    		this.height=23;
    		pixels = new boolean[width][height];
    	}
    	
        void setPixel(int x, int y, boolean b) {
    		pixels[x][y] = b;
    	}
      
        void setPixel(int x, int y) {
    		pixels[x][y] = true;
    	}             
    
        int getWidth() {
    		return width;
    	}
     
        int getHeight() {
    		return height;
    	} 
    
        public void clear() { 
              for (int i = 0; i < 23; i++) { 
                 System.out.print("\n"); 
            } 
        } 
    
    
        public void display() { 
              for (int x = 0; x < getWidth(); x++) { 
                 for (int y = 0; y < getHeight(); y++) { 
                     if (pixels[x][y]  == true) { 
                         System.out.print("*"); 
                     } else { 
                         System.out.print(" "); 
    				} 
    			}
    		System.out.println();
    		} 
        } 
        
        public static void main(String [] args) { 
            ConsoleFrameBuffer fb = new ConsoleFrameBuffer(); 
            //fb.display(); 
            int x = 0, y = 0; 
            while (x < fb.getWidth()) { 
                while (x < fb.getWidth() && y < fb.getHeight()) 
                    fb.setPixel(x++, y++); 
                y--; 
    
                while (x < fb.getWidth() && y >= 0) 
                    fb.setPixel(x++, y--); 
                if (y < 0)y = 0; 
            } 
    
           fb.clear(); 
           fb.display(); 
        } 
    }

    Output
    Code:
    *
     *
      *
       *
        *
         *
          *
           *
            *
             *
              *
               *
                *
                 *
                  *
                   *
                    *
                     *
                      *
                       *
                        *
                         *
                          *
                          *
                         *
                        *
                       *
                      *
                     *
                    *
                   *
                  *
                 *
                *
               *
              *
             *
            *
           *
          *
         *
        *
       *
      *
     *
    *
    *
     *
      *
       *
        *
         *
          *
           *
            *
             *
              *
               *
                *
                 *
                  *
                   *
                    *
                     *
                      *
                       *
                        *
                         *
                          *
                          *
                         *
                        *
                       *
                      *
                     *
                    *
                   *
                  *
                 *
    The main change I made was to instantiate the boolean array. Also fixed a few bugs and formatting issues. The actual logic is all yours and looks like it needs a few tweaks to produce the desired output.

    In the future, please make your code more readable if you want people to be able to help you with it.

    Also, I noticed you have imports for and Applet. This is all character console stuff (which is why I was upset earlier about the use of the word pixel here). You will need to take a look at java.awt.BufferedImage if you want to use actual pixels.
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Thumbs up


    ChristopherLowe, thank you so much. I understand much better right. Thank you for your help.
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2011
    Posts
    50
    Rep Power
    4
    No problem at all my friend.

    If you would like a hand converting this into an applet just say so.

IMN logo majestic logo threadwatch logo seochat tools logo