blahbla 1 Newbie Poster

need some help with this lab
this class is to draw a line with *'s
i guess in the command prompt since it uses System.out to print

i was given the function names and the comments that tells me what the function does
i wrote the code in the functions of the FrameBuffer class
those comments and the main are the only info i have on how to write it

public class FrameBuffer {
    FrameBuffer(int width, int height) {	////// Creates a frame buffer of specified resolution 
		this.width = width;
		this.height = height;
		pixels = new boolean [width][height];
		clear();
		}
    FrameBuffer() {		////// Creates a default-sized frame buffer
		width = 50;
		height = 20;
		pixels = new boolean [width][height];
		clear();
	}	

    void setPixel(int x, int y, boolean b) {pixels[x][y] = b;} //// Sets pixel (x, y) to b
    void setPixel(int x, int y) {pixels[x][y] = true;}		//// Sets pixel (x, y) to true 

    int getWidth() {return width;}
    int getHeight() {return height;}

    void clear() {		///// Clears frame buffer
		for(int i=0;i<width;i++)
			for(int j=0;j<height;j++)
				setPixel(i,j,false);
	}	 

    void display() {	///// displays frame buffer on System.out as " "'s and "*"s
		for(int i=0;i<width;i++)
			for(int j=0;j<height;j++)
				if(pixels[i][j] == true)
					System.out.print("*");
				else System.out.print(" ");
	}
		
    private boolean pixels[][];      //////this was given 
    private int width, height;      //////this was given


public static void main(String [] args) {     ///////the main was given as well
        FrameBuffer fb = new FrameBuffer();
        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.display();
    }

}

i wrote the stuff in the class FrameBuffer
the main was tested and it works
its output is some thing like a v or w depending on how big it is
:::

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

but when i run my code it comes out something like this ::

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

i cant figure out why it does this, dont understand why it does this as well
i'd like any help at all but if it is possible dont give me the code, i still want to do it myself
what i did wrong? something i'm missing? i'd like some hints, pushing in the right direction, stuff like that

any help is much appreciated
thanks in advance

Dani AI

Generated

For : the output pattern points to the display routine emitting characters column-by-column without inserting end-of-line markers, so the terminal receives a long stream of characters that wraps into the repeated vertical groups seen. The problem is an ordering/orientation mismatch between how the 2D boolean array is stored and how it is printed.

Fixing strategy (no full code here, just the approach):

  • Make the printing routine iterate rows (height) as the outer loop and columns (width) as the inner loop, then write a line break after finishing each row. That produces one text line per framebuffer row.
  • Alternatively, change the in-memory layout so the first index is the row (height) and the second is the column (width). Either convention works, but all operations (clear, setPixel, display, and any tests in main) must use the same convention.
  • Keep index bounds in mind when using post-increment operations in the drawing logic; ensure no setPixel call slips past the buffer edges.

Quick debugging checks:

  • Reduce the frame size (e.g., very small width/height) and set a single known pixel (center) to see how coordinates map to the printed output.
  • Temporarily mark a simple pattern (a border or cross) to confirm which index is row vs column.
  • Add an assertion or temporary print of the (x,y) values right before setting a pixel to catch out-of-range calls.

Consistency is the key: pick row-major or column-major, apply it everywhere, and ensure the display emits a newline after each row. That will produce the expected diagonal/V shapes instead of the repeated columns.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.