Hi @bam,
why not just provide a simple class for getting the required inputs on sketch startup ?
The class below could be improved in many ways (ie. allow a range for values [3.14,6.28] or specific Strings (like enum string values), etc.) but guess it demonstrates the general idea …
Hope that helps…
Cheers
— mnse
import processing.awt.*;
import javax.swing.*;
import java.awt.*;
ProgramInput input;
void setup() {
size(500, 500);
textSize(20);
input = new ProgramInput(new Object[][] {
{/*variable name*/ "someFloat", /*label*/ "Please enter a Float", /*type*/Float.class , /*default value*/3.14},
{"someInteger", "Please enter an Integer", Integer.class, 42},
{"someString", "Please enter a String", String.class, "Hallo!"}
});
}
void draw() {
background(0);
fill(255);
text("Value of someFloat : "+input.get("someFloat"), 20, 30);
text("Value of someInteger : "+input.get("someInteger"), 20, 50);
text("Value of someString : "+input.get("someString"), 20, 70);
}
// ProgramInput class, could be in a separate Tab
public class ProgramInput {
private HashMap<String, Object> inputs = new HashMap<>();
private Object[][] requirements;
JFrame frame;
public ProgramInput(Object[][] requirements) {
this.requirements = requirements;
this.frame=(JFrame) ((PSurfaceAWT.SmoothCanvas)getSurface().getNative()).getFrame();
createForm();
}
private void createForm() {
JPanel panel = new JPanel(new GridLayout(requirements.length, 2, 5, 5));
HashMap<String, JTextField> textFields = new HashMap<>();
for (Object[] requirement : requirements) {
String variableName = (String) requirement[0];
String prompt = (String) requirement[1];
Class<?> type = (Class<?>) requirement[2];
Object defaultVal = requirement[3];
if (textFields.get(variableName) != null) {
println("ERROR: variableName " + variableName + " already declared!");
continue;
}
panel.add(new JLabel(prompt + " (" + type.getSimpleName() + "):"));
JTextField textField = new JTextField();
textField.setText(defaultVal.toString());
textFields.put(variableName, textField);
panel.add(textField);
}
boolean isValid;
do {
isValid = true;
JOptionPane.showOptionDialog(frame, panel, "Specify program parameters", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, new Object[]{"OK"}, "OK");
for (Object[] requirement : requirements) {
String variableName = (String) requirement[0];
JTextField textField = textFields.get(variableName);
String text = textField.getText();
Class<?> type = (Class<?>) requirement[2];
try {
if (type == Integer.class) {
inputs.put(variableName, Integer.parseInt(text));
} else if (type == Float.class) {
inputs.put(variableName, Float.parseFloat(text));
} else if (type == String.class) {
inputs.put(variableName, text);
} else {
throw(new java.lang.IllegalArgumentException());
}
}
catch (NumberFormatException e) {
JOptionPane.showMessageDialog(frame, "Invalid input for " + variableName + ". Expected " + type.getSimpleName() + ".", "Error", JOptionPane.ERROR_MESSAGE);
isValid=false;
break;
}
catch (java.lang.IllegalArgumentException e) {
JOptionPane.showMessageDialog(frame, "Unsupported type: " + type.getSimpleName(), "Error", JOptionPane.ERROR_MESSAGE);
isValid=false;
break;
}
}
} while (!isValid);
}
public Object get(String key) {
return inputs.get(key);
}
}
errorhandling: