Loading...
Logo
Processing Forum
Hi Everyone,

I am working on a simple program. Basically, the program is supposed to do the following:

A) Fetch co-ordinates for circles to be displayed on the screen from .txt file. (Done, see code below)
B) Display circles (Done, see code below)
C) Ask for user input (??)
D) Store user input in .txt file next to a value for the number of circles displayed (??) So, if there are 10 circles displayed, and the user types 12, the in the text file, the following should be displayed 12,10.

How can I amend the code below, to also include C and D?


 /*
  *** Our text file will look something like below, #
  where each line corresponds to the x,y,z coordinate of a point!
  coordX, coordY, coorzZ
  coordX, coordY, coorzZ
  coordX, coordY, coorzZ
  ... etc
  */ 
 
  /*-----------------------------------------------------------------------------
 *** GLOBAL VARIABLES ***
 -----------------------------------------------------------------------------*/
ArrayList pointList;                   // arraylist to store the points in
 
/*-----------------------------------------------------------------------------
 *** GLOBAL INIT ***
 -----------------------------------------------------------------------------*/
void setup(){
 
  //-------------------------------- GENERAL
  size(800,800);
  background(0);
 
  //-------------------------------- POINTS
  pointList = new ArrayList();    // instantiate an ArrayList
  importTextFile();               // call our custom function
 
}
 
/*-----------------------------------------------------------------------------
 *** MAIN PROGRAM LOOP ***
 -----------------------------------------------------------------------------*/
void draw(){
 
  //-------------------------------- GENERAL
  background(0); 
  stroke(210);
  fill(210,90);
 
  //-------------------------------- VISUALIZE THE POINT SET
  for(int i = 0; i < pointList.size(); ++i){       
    PVector V = (PVector) pointList.get(i);
    ellipse(V.x,V.y, 20, 20);
  }
 
}
 
/*-----------------------------------------------------------------------------
 *** CUSTOM IMPORT FUNCTION ***
 -----------------------------------------------------------------------------*/
 
void importTextFile(){  
  String[] strLines = loadStrings("points_16_9.txt"); // the name and extension of the file to import!
  for(int i = 0; i < strLines.length; ++i){
    String[] arrTokens = split(strLines[i], ',');    // use the split array with character to isolate each component
    float xx = float(arrTokens[0]);                  // cast string value to a float values!
    float yy = float(arrTokens[1]);                  // cast string value to a float values!
    pointList.add( new PVector(xx,yy) );             // add values to a new array slot
 }
}



Replies(3)

Perhaps something like this...
Copy code
  1. import javax.swing.JOptionPane;
  2. int circles = 10;
  3. void setup() {
  4.   String in = getInput();
  5.   println(in);
  6.   if (in != null) {
  7.     PrintWriter output = createWriter("out.txt");
  8.     output.println(in + "," + circles);
  9.     output.flush(); // Writes the remaining data to the file
  10.     output.close(); // Finishes the file
  11.   }
  12. }
  13. String getInput() {
  14.   return JOptionPane.showInputDialog("How many circles?");
  15. }

Thank you Jeff,

I think I can use this. But I am a beginner... How do I get my code and your code to play? I tried to put your code at the button of my code, but it doesn't' work...