I’m using a swing gui to control some variables and if I just insert that into a sketch with nothing except a setup() and the code to get swing gui to launch, I can edit text and use that to update a slider. I can also adjust the slider and the text updates. It gets weird when I add draw() and now the text is no longer editable even though the slider still updates the text. in draw() I’m using the swing adjusted variables to draw on an image.
Any ideas what would cause that?
One complication is that I’m forced to use processing-2.2.1 because processing-3.5.3 no longer works on my chromebook. here’s the stripped down gui code:
//import java.awt.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.text.DecimalFormat;
MyPanel controlPanel;
public String text;
double GHA = Math.toRadians(-51.16667f); //(-128.83333f);
double GHA1 = Math.toRadians(51.16667f);
//double alt = Math.toRadians(62.6f);
int timer=0;
boolean Star1;
public class MyPanel extends JPanel {
private TextDemo jcomp1;
private JLabel jcomp2;
private JButton jcomp3;
//private JButton jcomp9;
private JButton jcomp4;
private JSlider jcomp5;
public MyPanel() {
//construct components
jcomp1 = new TextDemo(); //JTextArea (1, 16);
jcomp2 = new JLabel ("GHA : DDD.MMSS");
jcomp3 = new JButton ("GHA");
jcomp4 = new JButton("Star1");
jcomp5 = new JSlider(0,360,(int)(Math.toDegrees(-GHA))); //,(int)Math.toDegrees(GHA));
jcomp3.addActionListener(new Button1Click());
jcomp4.addActionListener(new Button2Click());
jcomp5.addChangeListener(new HSlider1Change());
jcomp5.setOrientation (JSlider.HORIZONTAL);
jcomp5.setMinorTickSpacing (10);
jcomp5.setMajorTickSpacing (60);
jcomp5.setPaintTicks (true);
jcomp5.setPaintLabels (true);
//adjust size and set layout
setPreferredSize (new Dimension (544, 756));
setLayout (null);
//add components
add (jcomp1);
add (jcomp2);
add (jcomp3);
add (jcomp4);
add (jcomp5);
//set component bounds (only needed by Absolute Positioning)
jcomp1.setBounds (70, 35, 205, 25);
jcomp2.setBounds (285, 35, 175, 25);
jcomp3.setBounds (70, 80, 100, 25);
jcomp4.setBounds (70,320,100,25);
jcomp5.setBounds(70,420,400,50);
}
class Button1Click implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JButton b = (JButton)e.getSource();
GHA = Double.parseDouble(text);
jcomp5.setValue((int)Double.parseDouble(text));
jcomp1.textField.setText(text);
println("GHA: " + GHA);
timer=millis()+1000;
}
}
class Button2Click implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JButton b = (JButton)e.getSource();
GHA1 = GHA;
println("GHA1: " + GHA1);
// dec1 = dec;
// alt1 = alt;
// Globe = true;
// Map = false;
Star1 = true;
timer=millis()+1000;
}
}
class HSlider1Change implements ChangeListener
{
public void stateChanged(ChangeEvent e)
{
JSlider source = (JSlider)e.getSource();
if (source.getValue() < 180){
GHA = radians( (- source.getValue()));
jcomp1.textField.setText(Double.toString(Math.toDegrees(-GHA)));
}
if (source.getValue() > 180) {
GHA = radians((360 - source.getValue()));
jcomp1.textField.setText(Double.toString(360-(Math.toDegrees(GHA))));
}
timer=millis()+1000;
println("slider: " + source.getValue() + " GHA: " + Math.toDegrees(GHA));
}
}
}
void setup()
{
JFrame frame =new JFrame("Controls");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
controlPanel = new MyPanel();
controlPanel.setOpaque(true); //content panes must be opaque
frame.setContentPane(controlPanel);
//frame.add(new TextDemo());
//Display the window.
frame.pack();
frame.setVisible(true);
}
public class TextDemo extends JPanel implements ActionListener {
protected JTextField textField;
protected JTextArea textArea;
private final static String newline = "\n";
public TextDemo() {
textField = new JTextField(20);
textField.addActionListener(this);
textField.setHorizontalAlignment(textField.CENTER);
textArea = new JTextArea(2, 20);
textArea.setEditable(false);
add(textField);
add(textArea);
}
public void actionPerformed(ActionEvent evt) {
text = textField.getText();
textArea.append(text + newline);
textField.selectAll();
GHA = Double.parseDouble(text);
textField.setText(Double.toString(GHA));
println(GHA);
//Make sure the new text is visible, even if there
//was a selection in the text area.
}
}