Processing : Créer une seconde fenêtre

De Centre de Ressources Numériques - Labomedia
Aller à : navigation, rechercher

Processing 3

void settings() {
  size(200, 400);
  //fullScreen(2);
}

void setup() {
  PApplet.runSketch(platformNames, new SecondApplet());
}

void draw() {
  background(0);
  fill(255, 0, 0);
  ellipse(width / 2, height / 2, 100, 100);
}     

public class SecondApplet extends PApplet {

  public void settings() {
    size(400, 200);
    //fullScreen(1);
  }

  public void setup() {
    surface.setTitle("Titre de ma seconde fenetre");
  }

  public void draw() {
    background(255);
    fill(0, 255, 0);
    ellipse(width / 2, height / 2, 100, 100);
  }
}

Processing 2

Comment créer une seconde fenêtre et comment interagir avec elle :

import java.awt.*;

PFrame f;
autreApplet s;

boolean drapeau;

void setup() {
  size(200, 200);
  s = new autreApplet();
  f = new PFrame(s);
  drapeau = false;
}

void draw() {
  background(255, 0, 0);
  if (s.temoin != drapeau) {
    drapeau = s.temoin;
  }
  if (drapeau) {
    fill(0, 255, 0);
  } else {
    noFill();
  }
  rect(50, 50, 100, 100);
}

void keyPressed() {
  if (key == 't') {
    f.setSize(600, 600);
  }
  if (key == ' ') {
    if (f.isShowing()) {
      f.setVisible(false);
    } else {
      f.setVisible(true);
    }
  }
}

void mousePressed() {
  if ((mouseX > 50) && (mouseX < 150) && (mouseY > 50) && (mouseY < 150)) {
    drapeau = !drapeau;
    s.temoin = !s.temoin;
  }
}

public class PFrame extends Frame {
  public PFrame(autreApplet autre) {
    setBounds(100, 100, 200, 200);
    add(autre);
    autre.init();
    setVisible(true);
  }
}

public class autreApplet extends PApplet {
  boolean temoin;

  public void setup() {
    size(200, 200);
    temoin = false;
  }

  public void draw() {
    background(0, 0, 255);
    if (temoin) {
      fill(0, 255, 0);
    } else {
      noFill();
    }
    rect(50, 50, 100, 100);
  }

  void mousePressed() {
    if ((mouseX > 50) && (mouseX < 150) && (mouseY > 50) && (mouseY < 150)) {
      temoin = !temoin;
    }
  }
}

Une deuxième méthode issue du forum Processing :

static final void main(final String[] args) {
  final String sketch = Thread.currentThread()
    .getStackTrace()[1].getClassName();

  main(sketch, args);

  final Class[] nested;
  try {
    nested = Class.forName(sketch).getClasses();
  }
  catch (final ClassNotFoundException cause) {
    throw new RuntimeException(cause);
  }

  println(nested);
  println();

  for (int i = 0, ii = max (0, nested.length-2); i != ii; ++i)
  try {
    main(nested[i].getName(), args);
  }
  catch (final Exception cause) {
    println(nested[i] + " isn't a PApplet or isn't public static!");
  }
}

void setup() {
  size(600, 400, P2D);
  smooth(8);
  noLoop();
  background((color) random(#000000));
}

public static final class MyFrame extends PApplet {
  void setup() {
    size(300, 200, JAVA2D);
    noLoop();
    println("MyFrame");
  }

  void draw() {
    background(0);
  }
}

public static final class MyApp extends PApplet {
  void setup() {
    size(300, 200, JAVA2D);
    noLoop();
    println("MyApp");
  }

  void draw() {
    background(255);
  }
}