【问题标题】:Creating a new window for toggles Processing (ControlP5)为切换处理创建一个新窗口 (ControlP5)
【发布时间】:2017-02-07 15:45:31
【问题描述】:

这是我的图像生成器,可用于切换。我想在新窗口中有切换按钮。我尝试使用 PApplet,但它似乎没有显示任何内容。如何为切换添加额外的窗口?

import controlP5.*; 

PFont myFont;

boolean showBackground = false;
boolean showObjects = false;
boolean showGrids = false;
boolean showType = false;

ControlP5 cp5;

PImage[] objects = new PImage[30];
PImage[] grids = new PImage[29];
PImage[] backgrounds = new PImage[30];
PImage[] type = new PImage[30];

int currentBackgrounds;
int currentGrids;
int currentObjects;
int currentType;


void setup() {
  size(1436, 847);

  //load objects
  for (int i=0; i<objects.length; i++) {
    objects[i] = loadImage  ( "o" + i + ".png");
  }
  //load grids
  for (int i = 0; i < grids.length; i++) {
    grids[i] = loadImage  ( "g" + i + ".png");
  }
  //load backgrounds
  for (int i = 0; i < backgrounds.length; i++) {
    backgrounds[i] = loadImage( "b" + i + ".jpg");
  }
  //load type
  for (int i = 0; i < type.length; i++) {
    type[i] = loadImage  ("t" + i + ".png");
  }
  //setup UI
String[] args = {"TwoFrameTest"};
SecondApplet sa = new SecondApplet();
PApplet.runSketch(args, sa);

}
public class SecondApplet extends PApplet {

public void settings() {
    size(200, 100);
  }
  // create a toggle and change the default look to a switch look
  public void draw() {
  cp5 = new ControlP5(this);

  PFont font = createFont("EurostileLTStd-BoldEx2.otf", 10);


  cp5.addToggle("showBackground")
    .setColorForeground(color(255, 255, 255)) 
    .setColorBackground(color(255, 255, 255))
    .setColorActive(color(255, 0, 0))
    .setPosition(40, 250)
    .setFont(font)
    .setSize(20, 20)
    .setValue(false)
    .setMode(ControlP5.SWITCH);

  cp5.addToggle("showObjects")
    .setPosition(40, 400)
    .setFont(font)
    .setSize(50, 20)
    .setValue(false)
    .setMode(ControlP5.SWITCH);

  cp5.addToggle("showGrids")
    .setPosition(40, 600)
    .setFont(font)
    .setSize(50, 20)
    .setValue(false)
    .setMode(ControlP5.SWITCH);

  cp5.addToggle("showType")
    .setPosition(40, 800)
    .setFont(font)
    .setSize(50, 20)
    .setValue(false)
    .setMode(ControlP5.SWITCH);
  }
}

void mousePressed() {
  //next img 
  currentObjects = currentObjects + int(random(10));
  currentGrids = currentGrids + int(random(10));
  currentType = currentType + int(random(10));
  currentBackgrounds = currentBackgrounds + int(random(10));

  if (currentObjects >= objects.length) {
    currentObjects = 0;
  }
  if (currentGrids >= grids.length) {
    currentGrids = 0;
  }
  if (currentType >= type.length) {
    currentType = 0;
  }
  if (currentBackgrounds >= backgrounds.length) {
    currentBackgrounds = 0;

  }
}

void draw() {
  //clear frame
  background(211);//

  if (showBackground==true) {
    image(backgrounds[currentBackgrounds], 0, 0, 1436, 847); // b
  } 
  if (showGrids==true) {
    image(grids[currentGrids], 0, 0, 1436, 847); // g
  } 
  if (showObjects==true) {
    image(objects[currentObjects], 0, 0, 1436, 847); // o
  }
  if (showType==true) {
    image(type[currentType], 0, 0, 1436, 847); // o
  }
}

【问题讨论】:

  • In Processing check out File > Examples > Contributed Libraries > ControlP5 > extra > ControlP5frame。在该示例中,您可以看到提到的 runSketch Kevin,以及如何使用 plugTo() 将来自 ControlFrame 小程序的控件插入到第一个小程序的属性中。
  • 你有想过这个问题吗?
  • 不,从来没有弄清楚..

标签: image window toggle processing generator


【解决方案1】:

您必须调用runSketch() 函数才能弹出第二个窗口并成为它自己的处理草图。像这样的:

void setup() {
  String[] args = {"TwoFrameTest"};
  SecondApplet sa = new SecondApplet();
  PApplet.runSketch(args, sa);
}

void settings() {  
  size(300, 100);
}

void draw() {
  background(0);
  fill(255);
  text("Hello world!", 50, 40);
}   


public class SecondApplet extends PApplet {

  public void settings() {
    size(200, 200);
  }

  public void draw() {
    background(255);
    fill(0);  
    text("Hello world!", 50, 40);
  }
}

【讨论】:

  • 但是切换开关没有与其他文件连接,对吗?
  • @NaomivanMaasakkers 否,除非您使用 ControlP5 的 plugTo 功能
  • @NaomivanMaasakkers 您可以尝试将SecondApplet 实例(在我的代码中为sa 变量)传递给ControlP5 构造函数,或者将ControlP5 的内容移至SecondApplet 类.
猜你喜欢
  • 1970-01-01
  • 2018-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-10
  • 1970-01-01
  • 1970-01-01
  • 2011-03-29
相关资源
最近更新 更多