How to make the window invisible?
in
Integration and Hardware
•
1 year ago
I am trying to write a proof-of-concept program which uses the Java swing libraries to create a GUI which controls the brightness of an LED on an Arduino. I use the Java swing libraries to create a window in the application; the only problem is that the normal, default Processing window also shows up. Is there any way to make this window not appear when the application runs? I tried setting its dimensions to (0,0) which appeared to work at first but didn't actually allow the loop() function to run.
Here is my code, in case it's helpful:
Here is my code, in case it's helpful:
- import java.awt.*;
import javax.swing.*;
import cc.arduino.*;
import processing.serial.*;
Arduino arduino;
void setup() {
println(Arduino.list());
arduino = new Arduino(this, Arduino.list()[1], 57600);
arduino.pinMode(11, Arduino.OUTPUT);
arduino.pinMode(13, Arduino.OUTPUT);
arduino.digitalWrite(13, Arduino.HIGH);
createGUI();
size(1,1);
}
JSlider slider;
JLabel label;
JCheckBox cb;
static final int maxAmount = 255, minAmount = 0, initAmount = 100;
int value;
public void createGUI() {
//Main panel
JFrame frame = new JFrame("Arduino Sliders");
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new GridLayout(0,1));
//subpanels
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
//create slider
slider = new JSlider(JSlider.HORIZONTAL, minAmount, maxAmount, initAmount);
//create label
label = new JLabel();
//create checkbox
cb = new JCheckBox("LED On/Off", false);
//Show the whole thing
panel1.add(label);
panel2.add(slider);
panel3.add(cb);
mainPanel.add(panel1);
mainPanel.add(panel2);
mainPanel.add(panel3);
frame.add(mainPanel);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
void draw() {
if(cb.isSelected()) {
value = slider.getValue();
int valuePercent = (int) map(value, 0, 255, 0, 100);
label.setText("Brightness: " + valuePercent + "%");
arduino.analogWrite(11, value);
cb.setLabel("Turn off");
}
else {
arduino.analogWrite(11, 0);
label.setText("LED is off");
cb.setLabel("Turn On");
}
}
1