We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Yet another Noob with a fairly simple question
Page Index Toggle Pages: 1
Yet another Noob with a fairly simple question (Read 365 times)
Yet another Noob with a fairly simple question
Aug 19th, 2009, 7:14pm
 
Hi,
I'm new to Processing, and my situation is:

I'm trying to create a continuously scrolling graph, along with some text, on an 800x800 canvas. The graph is of some data gathered from an Arduino board. I have been able to capture the data without a problem. When I try to graph it continuously, though, there's a lot of flicker in the image.

Right now, I'm trying to use PGraphics to buffer each frame and reduce flicker. This reduces flicker in the graph, but it seems like I'm unable to write text to the buffer. Every time I try to use text() or related functions within the PGraphics object, I get a lot of errors and no image.

The text seems to write to the canvas if I put it in the code after I write the buffered image to the canvas. This still causes the text  to flicker, though.

So my questions are:
1. Should I be using PGraphics, or something else, to buffer each frame?
2. Is it possible to write text to a PGraphics object? If so, how can this be done?

Here's my code. Commented-out area seems to be causing the issues. Many thanks for any help y'all provide.


import processing.serial.*;
Serial myPort;

PGraphics mainGraph;

int intOriginX;
int intOriginY;
int intLOX;
int intLOY;

float currentRdg;

int thisTime = 0;
int lastTime = 0;

int[] readings;

PFont font;



void setup()

{
 size(800,800);
 mainGraph = createGraphics(800,800,P2D);
 
 font = loadFont("CourierNew15.vlw");
 textFont(font,15);

 intOriginX = int(0.2*width);
 intOriginY = int(0.75*height);

 intLOX = int(0.6*width);
 intLOY = int(0.6*height);
 
 readings = new int[intLOX];
 
 println(Serial.list());
 myPort = new Serial(this, Serial.list()[2], 9600);
 myPort.bufferUntil('\n');

 background(0);
}

void draw()
{

}


void serialEvent(Serial myPort)
{

 mainGraph.beginDraw();
 mainGraph.background(0);
 
 mainGraph.stroke(0,255,0);
 mainGraph.line (intOriginX, intOriginY, intOriginX+intLOX, intOriginY);
 mainGraph.line (intOriginX, intOriginY, intOriginX, intOriginY-intLOY);
 
 String inString = myPort.readStringUntil('\n');
 
 inString = trim(inString);
 float currentRdg = float(inString);
 currentRdg = map(currentRdg, 0, 1023, 0, intLOY);

 //mainGraph.textAlign(LEFT);
 //mainGraph.text("Reading Value: ", 10, 15);
 //mainGraph.text(str(currentRdg), 150,15);

 for (int i = 0; i < (intLOX-1); i++)
 {
   readings[i] = readings[i+1];
 }
 
 readings[intLOX-1] = int(currentRdg);


 //mainGraph.text("Array Value: ", 10, 30);
 //mainGraph.text(str(readings[intLOX-1]), 150,30);  

 
 mainGraph.stroke(255,0,0);
 for (int i=0; i < intLOX; i++)
 {

 mainGraph.line ((intOriginX+i+1),(intOriginY-(intLOY-readings[i])-1),(intOriginX+i+1),intOriginY-1);
 }
 mainGraph.endDraw();
 image(mainGraph,0,0);

}


Re: Yet another Noob with a fairly simple question
Reply #1 - Aug 19th, 2009, 9:02pm
 
Huh, I just tried adding mainGraph.textFont(font,15); and it still says "Use textFont() before text()", not sure I can help you there.

Quick fix: Just take the text stuff and move it down below the image() call, and draw it right to the canvas.

IE:
Quote:
void serialEvent(Serial myPort)
{

 mainGraph.beginDraw();
 mainGraph.background(0);
 
 mainGraph.stroke(0,255,0);
 mainGraph.line (intOriginX, intOriginY, intOriginX+intLOX, intOriginY);
 mainGraph.line (intOriginX, intOriginY, intOriginX, intOriginY-intLOY);
 
 String inString = myPort.readStringUntil('\n');
 
 inString = trim(inString);
 float currentRdg = float(inString);
 currentRdg = map(currentRdg, 0, 1023, 0, intLOY);

 for (int i = 0; i < (intLOX-1); i++)
 {
   readings[i] = readings[i+1];
 }
 
 readings[intLOX-1] = int(currentRdg);

 mainGraph.stroke(255,0,0);
 for (int i=0; i < intLOX; i++)
 {

 mainGraph.line ((intOriginX+i+1),(intOriginY-(intLOY-readings[i])-1),(intOriginX+i+1),intOriginY-1);
 }
 mainGraph.endDraw();
 image(mainGraph,0,0);
 
 textAlign(LEFT);
 text("Reading Value: ", 10, 15);
 text(str(currentRdg), 150,15);
 text("Array Value: ", 10, 30);
 text(str(readings[intLOX-1]), 150,30);
 
}

Page Index Toggle Pages: 1