I am having a problem with trying to center just one line of text. I think I am applying “textAlign(CENTER)” to just one line, but it is also offsetting all the other text in the window. Here is my complete code. Thank you in advance for your help.
import controlP5.*;
ControlP5 cp5;
DropdownList d1;
DropdownList d2;
PFont font, font1, font2, font3;
import processing.serial.*; //import the Serial library
Serial myPort;
PrintWriter output;
String EepromData[];
String val;
String End;
String portName;
String SaveData;
String FileName;
String[] NamePath;
String GetFilePath;
String WriteFileName;
byte LoadFileCheck = 0;
int OnlyName;
int b = 0;
int ItemNr = 22;
int TimerStart = 0;
int serialListIndex;
int EEpromSize_iL7 = 4096;
int EEpromSize_iL8 = 16384;
boolean Restoring = false;
boolean WriteFinished = false;
boolean InvalidEeprom = false;
boolean EditType = false;
boolean EditCom = false;
boolean DataCheck = false;
boolean SerialCheck = true;
boolean DumpActive = false;
boolean RestoreCheck = false;
boolean FileLoadedCheck = false;
void setup() {
clear();
size(250, 250); // Create Window
cp5 = new ControlP5(this);
font = createFont("Arial Bold", 20);
font1 = createFont("Arial Bold", 10);
font2 = createFont("Arial Bold", 13);
font3 = createFont("Arial Bold", 12);
// output = createWriter("MyTest.txt");
// frameRate(60);
End = "END";
// Add Button
cp5.addButton("Restore") // "Start" is the name of the Button
.setPosition(140, 155) // x and y coordinates of upper left corner of Button
.setSize(80, 20) // (Width, Height)
.setFont(font1)
.setColorBackground(color(216, 216, 216))
.setColorForeground(color(232, 232, 232))
.setColorActive(color(186, 188, 188))
.setColorLabel(color(0, 0, 0))
;
cp5.addButton("Dump") // "Start" is the name of the Button
.setPosition(30, 155) // x and y coordinates of upper left corner of Button
.setSize(80, 20) // (Width, Height)
.setFont(font1)
.setColorBackground(color(216, 216, 216))
.setColorForeground(color(232, 232, 232))
.setColorActive(color(186, 188, 188))
.setColorLabel(color(0, 0, 0))
;
cp5.addButton("Open") // "Start" is the name of the Button
.setPosition(178, 102) // x and y coordinates of upper left corner of Button
.setSize(53, 20) // (Width, Height)
.setFont(font1)
.setColorBackground(color(216, 216, 216))
.setColorForeground(color(232, 232, 232))
.setColorActive(color(186, 188, 188))
.setColorLabel(color(0, 0, 0))
;
d1 = cp5.addDropdownList("myList-d1")
.setPosition(178, 76)
.setSize(53, 16)
.setBarHeight(16)
.setHeight(120)
.setItemHeight(16)
.setFont(font1)
.setColorBackground(color(216, 216, 216))
.setColorForeground(color(232, 232, 232))
.setColorActive(color(186, 188, 188))
.setColorLabel(color(0, 0, 0))
.setColorValueLabel(color(0, 0, 0))
.setColorCaptionLabel(color(0, 0, 0))
.setOpen(false);
;
// d1.getCaptionLabel().set("- - - - - -"); //set PORT before anything is selected
d1.getCaptionLabel().set("COM1"); //set PORT before anything is selected
portName = Serial.list()[0]; //0 as default
myPort = new Serial(this, portName, 115200);
myPort.bufferUntil('\n');
// create a DropdownList,
d2 = cp5.addDropdownList("myList-d2")
.setPosition(178, 50)
.setSize(53, 16)
.setOpen(false);
;
customize(d2); // customize the first list
// myPort = new Serial(this, Serial.list()[1], 115200);
//EepromData = loadStrings("data/EEprom.txt");
// EepromData = loadStrings("data/notes.txt");
}
void customize(DropdownList d2) {
// a convenience function to customize a DropdownList
d2.setColorBackground(color(216, 216, 216));
d2.setItemHeight(16);
d2.setBarHeight(16);
d2.setHeight(64);
d2.setFont(font1);
d2.setColorForeground(color(232, 232, 232));
d2.setColorActive(color(186, 188, 188));
d2.setColorLabel(color(0, 0, 0));
d2.setColorValueLabel(color(0, 0, 0));
d2.setColorCaptionLabel(color(0, 0, 0));
d2.getCaptionLabel().set("- - - - - -");
}
void draw() {
background(0, 120, 255); // Background Color (r, g, b) or (0 to 255)
textFont(font);
fill(255, 255, 255); // text color (r, g, b)
text("MANAGE EEPROM", 34, 28); // ("text", x coordinate, y coordinate)
textFont(font2);
text("Select iLoopino Version", 18, 62); // ("text", x coordinate, y coordinate)
// textFont(font1);
text("Select Serial COM Port", 18, 88); // ("text", x coordinate, y coordinate)
text("Select File To Restore", 18, 116); // ("text", x coordinate, y coordinate)
if (WriteFinished) {
textFont(font3);
text("Finished", 2, 246);
if (TimerStart + 5000 < millis()) {
WriteFinished = false;
}
}
if (InvalidEeprom) {
textFont(font3);
text("Invalid EEprom File", 2, 246);
if (TimerStart + 2000 < millis()) {
InvalidEeprom = false;
}
}
if ((!SerialCheck) && (DumpActive) && (SaveData != null)) {
textFont(font3);
text("Dumping EEprom", 2, 246);
}
if (LoadFileCheck == 1) {
textFont(font3);
text("Cancelled Opening File.", 2, 246);
if (TimerStart + 5000 < millis()) {
LoadFileCheck = 0;
}
}
if (LoadFileCheck == 2) {
textFont(font3);
textAlign(CENTER);
text(FileName, width/2, 246);
}
if (d1.isMouseOver()) {
EditCom = true;
// EditType = false;
d1.clear(); //Delete all the items
for (int i=0; i<Serial.list().length; i++) {
d1.addItem(Serial.list()[i], i); //add the items in the list
}
} else {
EditCom = false;
}
if (d2.isMouseOver()) {
EditType = true;
d2.clear(); //Delete all the items
d2.addItem("iL 7.x", 0);
d2.addItem("iL 8.x", 1);
EditType = true;
//EditCom = false;
} else {
EditType = false;
}
if (RestoreCheck) {
if ((ItemNr < 2) && (!DumpActive)) {
if ((EepromData[0].equals(val) == true) && (DataCheck) && (FileLoadedCheck)) { // Compare two Strings
for (int i = 1; i < EepromData.length; i++) {
// println(EepromData[i]);
myPort.write(EepromData[i]);
myPort.write('\n');
delay(2); //this will be the tempo?
}
StartTimer();
} else {
TimerStart = millis();
InvalidEeprom = true;
}
DataCheck = false;
RestoreCheck = false;
}
}
}
void serialEvent(Serial myP) {
if (DumpActive) {
SaveData = myP.readString();
SaveData = SaveData.trim();
if ("END".equals(SaveData)) {
SerialCheck = true;
}
if (!SerialCheck) {
output.println(SaveData);
}
// println(SaveData); //read until new input
SaveData = "";
if (SerialCheck) {
output.flush(); // Write the remaining data
output.close(); // Finish the file
myPort.clear(); //delete the port
SerialCheck = false;
DumpActive = false;
StartTimer();
}
}
}
void controlEvent(ControlEvent theEvent) { //when something in the list is selected
if (EditCom) {
myPort.clear(); //delete the port
myPort.stop(); //stop the port
// if (theEvent.isController() && d1.isMouseOver()) {
if (theEvent.isController()) {
portName = Serial.list()[int(theEvent.getController().getValue())]; //port name is set to the selected port in the dropDownMeny
myPort = new Serial(this, portName, 115200); //Create a new connection
// println("Serial index set to: " + theEvent.getController().getValue());
myPort.bufferUntil('\n');
delay(200);
}
}
if (EditType) {
ItemNr = int(theEvent.getController().getValue());
/*
ItemNr = int(theEvent.getController().getValue());
// println(ItemNr);
if (ItemNr == 0) {
EepromData = loadStrings("data/EEprom_iL7.il7");
val = "EEprom_iL7";
if (EepromData.length == EEpromSize_iL7 + 1) {
DataCheck = true;
}
} else if (ItemNr == 1) {
EepromData = loadStrings("data/EEprom_iL8.il8");
val = "EEPROM_iL8";
if (EepromData.length == EEpromSize_iL8 + 1) {
DataCheck = true;
}
}
*/
}
}
void Dump() {
// ItemNr = int(theEvent.getController().getValue());
//println(ItemNr);
if (ItemNr == 0) {
output = createWriter("EEprom_iL7.il7");
} else if (ItemNr == 1) {
output = createWriter("EEprom_iL8.il8");
}
DumpActive = true;
SerialCheck = false;
}
void Restore() {
if(FileLoadedCheck) {
textFont(font3);
text("Restoring EEprom", 2, 246);
RestoreCheck = true;
/*
if((ItemNr < 2) && (!DumpActive)) {
if ((EepromData[0].equals(val) == true) && (DataCheck)) { // Compare two Strings
for (int i = 1; i < EepromData.length; i++) {
println(EepromData[i]);
myPort.write(EepromData[i]);
myPort.write('\n');
delay(2); //this will be the tempo?
}
StartTimer();
// StartTimer = millis();
// WriteFinished = true;
} else {
// // println("EEprom Not Valid");
TimerStart = millis();
InvalidEeprom = true;
}
DataCheck = false;
}
*/
}
}
void Open() {
noLoop();
if (ItemNr == 0) {
selectInput ( "Open iLoopino8 Eeprom:", "fileSelected", dataFile( "*.il7" ));
FileLoadedCheck = true;
}
else if (ItemNr == 1) {
selectInput ( "Open iLoopino8 Eeprom:", "fileSelected", dataFile( "*.il8" ));
FileLoadedCheck = true;
}
else {
}
}
void fileSelected(File selection) {
textFont(font3);
if (selection == null) {
// text("Cancelled Opening File.", 2, 246);
LoadFileCheck = 1;
// TimerStart = millis();
// println("Cancelled.");
}
else {
LoadFileCheck = 2;
// }
EepromData = (loadStrings(selection.getAbsolutePath()));
if (ItemNr == 0) {
// selectInput ( "Open iLoopino8 Eeprom:", "fileSelected", dataFile( "*.il7" ));
val = "EEprom_iL7";
if (EepromData.length == EEpromSize_iL7 + 1) {
DataCheck = true;
}
}
else if (ItemNr == 1) {
// selectInput ( "Open iLoopino8 Eeprom:", "fileSelected", dataFile( "*.il8" ));
val = "EEPROM_iL8";
if (EepromData.length == EEpromSize_iL8 + 1) {
DataCheck = true;
}
}
GetFilePath = selection.getAbsolutePath();
NamePath = splitTokens(GetFilePath, System.getProperty("file.separator"));
OnlyName = NamePath.length;
FileName = (NamePath[OnlyName - 1]);
WriteFileName = FileName;
println();
println();
println(FileName);
text(FileName, 2, 146);
}
loop();
}
void StartTimer() {
TimerStart = millis();
WriteFinished = true;
}