hello guys i am a beginner in processing 3 and i want to do some complex programming need your help
i want to create 2 buttons 1. which can save the data in text file and 2.which can open that text file in notepad.
i am using following code but i saves the txt file only in the sketch file and i need to save it on my desired location and can open file using that path.
PrintWriter output;
void setup() {
// Create a new file in the sketch directory
size(500,500);
output = createWriter("positionnew.txt");
}
void draw() {
point(mouseX, mouseY);
output.println(mouseX + "t" + mouseY); // Write the coordinate to the file
}
void keyPressed() {
output.flush(); // Writes the remaining data to the file
output.close(); // Finishes the file
exit(); // Stops the program
}
Additionally to open currentFile in NotePad (in Windows!) :
void openInNotepad() {
// open in notepad
try
{
ProcessBuilder pb = new ProcessBuilder("notepad.exe", currentFile, "");
Process p = pb.start();
}
catch ( Exception /* IOException, URISyntaxException */ e )
{
e.printStackTrace();
}//catch
}//
//
Regards, Chrisir
Example
// http://forum.processing.org/topic/beginner-s-question-an-application-software-with-source-code-made-with-processing
//
import java.io.File;
// current text
String input = "";
// file name as String
String currentFile = "";
// cursor sign blinks
boolean cursorBlinkFlag; // on/off
int timerCursorBlink; // timer
//---------------------------------------------------------------------------
void setup() {
size(880, 800);
}
void draw() {
background(111);
text ("Type something"
+" "
+"Commands: F3 to open; F2 to save changes; F12 to save a new file; F10 New (deletes text); Alt-F4 to quit.", 10, 30 );
text ("current file : "
+ textOfCurrentFile(), 10, 50);
text (input + strBlink(), 10, 70);
}
//---------------------------------------------------------------------------
// Inputs
void keyPressed() {
// Is the key CODED?
if (key==CODED) {
// key is CODED --------------------------------------------
if ( keyCode == java.awt.event.KeyEvent.VK_F3 ) {
// open
load();
} else if ( keyCode == java.awt.event.KeyEvent.VK_F2 ) {
// save existing file
if ( !currentFile.equals("") ) {
// save existing file
saveExistingFileThatHasBeenChanged();
} else {
// no file to save to,
// save new File
save();
}
} else if ( keyCode == java.awt.event.KeyEvent.VK_F10 ) {
// delete
input = "";
currentFile = "";
} else if ( keyCode == java.awt.event.KeyEvent.VK_F12 ) {
// save new File
save();
} else
{
// do nothing
}
//
} // if --------------------------------------------
else
{ // key is not CODED --------------------------------------------
if (key==BACKSPACE) {
// shorten
if (input.length()>0)
input=input.substring(0, input.length()-1);
} else if (key==ESC) {
// kill Esc
key=0;
} else {
// add key (normal writing)
input+=key;
} // else
//
} // else // key is not CODED -----------------------------------------------
//
} // func
// ------------------------------------------------------
// Load
void load() {
File folderToStartFrom = new File( dataPath("") + "//*.*" );
selectInput("Select a file to open", "fileSelectedOpen", folderToStartFrom );
}
void fileSelectedOpen(File selection) {
if (selection == null) {
println("Window was closed or the user hit cancel.");
} else {
println("User selected " + selection.getAbsolutePath());
currentFile=selection.getAbsolutePath();
input = join ( loadStrings( selection.getAbsolutePath() ), "\n" );
}//else
}
// ---------------------------------------------------------
// Save As....
void save() {
selectOutput("Select a file to write to:", "fileSelectedWrite");
}
void fileSelectedWrite(File selection) {
if (selection == null) {
println("Window was closed or the user hit cancel.");
} else {
println("User selected " + selection.getAbsolutePath());
// Do we have a txt at the end?
if (selection.getName().length() < 4 || selection.getName().indexOf(".txt") != selection.getName().length()-4 ) {
// problem missing ".txt"
currentFile = selection.getAbsolutePath()+".txt"; // very rough approach...
} else {
currentFile = selection.getAbsolutePath();
}//else
String[] lines = new String[0];
lines = append ( lines, input );
// Writes the strings to a file, each on a separate line
saveStrings( currentFile, lines);
}
}
// ---------------------------------------------------------
// Save existing file
void saveExistingFileThatHasBeenChanged() {
println("saveExistingFileThatHasBeenChanged");
// Writes the strings to a file, each on a separate line (overwriting)
String[] lines = new String[0];
lines = append ( lines, input );
saveStrings( currentFile, lines );
}
// ---------------------------------------------------------
// Tools
String textOfCurrentFile() {
if (currentFile.equals(""))
return "<Not a file>";
else
return currentFile;
}
String strBlink() {
// manage and show the Blinking Cursor |
// timer to toggle the flag cursorBlinkFlag
if (millis()-timerCursorBlink > 330) {
cursorBlinkFlag = !cursorBlinkFlag; // toggle
timerCursorBlink = millis(); // set timer
}//if
// return the line "|" or nothing
if (cursorBlinkFlag)
return"|";
else return"";
}//func
//
rn i have 3 problems:-
1.
i am trying to use buttons for red and green for on and off and if button is on then value=value-10 else value remains same but the value is decreasing by 10 continuously what should i do?
2.i have add radio button but dont know how to perform operation on them
like…if 1 is selected value=value +10