The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Java Help
|
Frame Buffer Line drawing
Discuss Frame Buffer Line drawing in the Java Help forum on Dev Shed. Frame Buffer Line drawing Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

May 16th, 2011, 08:34 PM
|
|
Registered User
|
|
Join Date: May 2011
Posts: 5
Time spent in forums: 1 h 9 m 33 sec
Reputation Power: 0
|
|
|
Frame Buffer Line drawing
Hi all,
Can anyone give an example of frame buffer line drawing?
|

May 16th, 2011, 09:25 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: Eastern Florida
|
|
|
See the answer on the other forum you posted this question on.
|

May 16th, 2011, 09:38 PM
|
|
Registered User
|
|
Join Date: May 2011
Posts: 5
Time spent in forums: 1 h 9 m 33 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by NormR See the answer on the other forum you posted this question on. |
Quote: | 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
|

May 17th, 2011, 12:53 AM
|
|
Contributing User
|
|
Join Date: Apr 2011
Posts: 50

Time spent in forums: 18 h 43 m 47 sec
Reputation Power: 3
|
|
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(" ");
}
}
}
|

May 17th, 2011, 06:53 AM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: Eastern Florida
|
|
|
Cross posted at:
http://www.javaprogrammingforums.com/whats-wrong-my-code/8974-java-frame-buffer-line-drawing.html#post32374
|

May 17th, 2011, 03:55 PM
|
|
Registered User
|
|
Join Date: May 2011
Posts: 5
Time spent in forums: 1 h 9 m 33 sec
Reputation Power: 0
|
|
|
Thanks a lot,
|

May 17th, 2011, 04:03 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: Eastern Florida
|
|
Quote: 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
|

May 17th, 2011, 04:24 PM
|
|
Registered User
|
|
Join Date: May 2011
Posts: 5
Time spent in forums: 1 h 9 m 33 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by NormR What variable at line 24 is null?
The display() method is called from line 41 |
Thanks
|

May 17th, 2011, 04:33 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: Eastern Florida
|
|
Quote: | What variable at line 24 is null? |
And why is that variable null?
Where has pixels been assigned a value?
|

May 17th, 2011, 07:28 PM
|
|
Contributing User
|
|
Join Date: Apr 2011
Posts: 50

Time spent in forums: 18 h 43 m 47 sec
Reputation Power: 3
|
|
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.
|

May 17th, 2011, 07:37 PM
|
|
Registered User
|
|
Join Date: May 2011
Posts: 5
Time spent in forums: 1 h 9 m 33 sec
Reputation Power: 0
|
|
|

May 17th, 2011, 07:46 PM
|
|
Contributing User
|
|
Join Date: Apr 2011
Posts: 50

Time spent in forums: 18 h 43 m 47 sec
Reputation Power: 3
|
|
|
No problem at all my friend.
If you would like a hand converting this into an applet just say so.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|