I wrote this small program to test the use of a a gui in a separate JFrame. It works fine with the radiobuttons, sliders, and checkboxes, but when I tried to add a button for saving the sketch to an image, I got a "cannot call invokeAndWait from the event dispatcher thread" error. This must be some sort of conflict between processing and swing's event handling?
[How could I get around this - use a JFileChooser instead? I would then have to get the text from the File object. to use in the processing loadImage method... myFile.getName(), maybe. I will try this and see if it works.] - edit - this is for the loading file issue, not saving -but has same problem.
Any other suggestions welcome
Code:import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JCheckBox;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JSlider;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
JFrame frame;
JPanel panel;
JLabel label;
JSlider Slider;
JButton button;
JRadioButton Red, Blue, Green;
ButtonGroup Group;
JCheckBox Smooth, big;
int mode =1; //1=small, 2=big;
int angle;
void setup()
{
size(400, 400);
rectMode(CENTER);
label = new JLabel("Drawing Options");
frame= new JFrame("Drawing");
panel = new JPanel();
button= new JButton("Save");
Smooth = new JCheckBox("smooth");
big= new JCheckBox("big");
Red = new JRadioButton("Red", true);
Green = new JRadioButton("Green");
Blue= new JRadioButton("Blue");
Group = new ButtonGroup();
Group.add(Red);
Group.add(Blue);
Group.add(Green);
guilistener listener = new guilistener();
Smooth.addItemListener(listener);
big.addItemListener(listener);
Green.addActionListener(listener);
Red.addActionListener(listener);
Blue.addActionListener(listener);
button.addActionListener(listener);
Slider = new JSlider (JSlider.HORIZONTAL, 0, 360, 0);
Slider.setMajorTickSpacing (50);
Slider.setMinorTickSpacing (10);
Slider.setPaintTicks (true);
Slider.setPaintLabels (true);
Slider.addChangeListener(listener);
panel.add(label);
panel.add(Smooth);
panel.add(big);
panel.add(Red);
panel.add(Blue);
panel.add(Green);
panel.add(Slider);
panel.add(button);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
noStroke();
background(255);
fill(255,0,0);
}
void draw()
{
background(255);
pushMatrix();
translate(width/2, height/2);
rotate(radians(angle));
if (mode==1)
rect(0, 0 ,100, 100);
else rect(0,0,200, 200);
popMatrix();
}
class guilistener implements ItemListener, ChangeListener, ActionListener
{
public void itemStateChanged(ItemEvent event) // for checkboxes
{
if(Smooth.isSelected()) smooth();
else noSmooth();
if (big.isSelected()) mode=2;
else mode=1;
}
public void stateChanged (ChangeEvent event) //for sliders
{
angle = Slider.getValue();
}
public void actionPerformed(ActionEvent event) //for radio buttons
{
Object source = event.getSource();
if (source==Green) fill(0,255,0);
else if (source==Red) fill(255,0,0);
else if (source==Blue) fill(0,0,255);
else if(source==button)
{
String savepath=selectOutput();
if(savepath!=null) save(savepath);
}
}
}