2014年12月21日日曜日

Processing で ポップアップスレッド (Pop up dialog thread for Processing sketch)



Processing で drawの中の状態に応じてポップアップダイアログを出す方法。
普通にdraw内に記述すると、drawが止まってしまう。だからスレッドを使う

If you want to use dialog pop up in your draw function,
you should use a thread for the dialog.

-------------
float   percent;    // progress bar
boolean isRunning;  // flag for thread running
Thread thread1; // thread for pop up dialog
boolean answer; // answer of user

void setup() {
  size(400, 300);
  isRunning = false;
  percent = 0;
  answer = false;
}

void draw() {
  background(255);
  if (answer) stroke(#AA0000);
  else stroke(#0000AA);
  line(10, 10, percent++, 10);

  if (percent == 300) {
    percent = 0;
    if (!isRunning) {
      thread1 = new Thread(new popupThread());
      thread1.start();
    }
  }
}

// pop up thread
class popupThread implements Runnable {
  public synchronized void run() {
    isRunning = true;
    popUpJPanel();
    isRunning = false;
  }
}


import java.awt.*;
import javax.swing.*;

// pop up dialog
int popUpJPanel() {
  JPanel panel = new JPanel();
  BoxLayout layout = new BoxLayout(panel, BoxLayout.Y_AXIS);
  panel.setLayout(layout);
  panel.setPreferredSize(new Dimension(400, 50));

  panel.add(new JLabel("Feel Good?"));
  //checkComment = new JTextField();
  //panel.add(checkComment);

  int r = JOptionPane.showConfirmDialog(
  null,
  panel, "Feel Good", // title of dialog
  JOptionPane.YES_NO_OPTION, // option
  JOptionPane.QUESTION_MESSAGE);  // message type

  if (r == 0) answer = true;
  else answer = false;

  return r;
};

0 件のコメント: