2017-02-07 25 views
0

這是我的圖像生成器,它可以與切換一起工作。我想在新窗口中有切換按鈕。我嘗試使用PApplet,但它似乎沒有顯示任何內容。如何爲切換添加額外的窗口?創建一個新的窗口來切換處理(ControlP5)

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 
    } 
} 
+0

在處理簽出**文件>示例> Contributed Libraries> ControlP5> extra> ControlP5frame **。在那個例子中,你可以看到Kevin提到的'runSketch',以及如何使用'''plugTo()''將ControlFrame'' applet中的控件插入到第一個applet的屬性中。 –

+0

你有沒有想過這個想法? –

+0

沒有想過它.. –

回答

0

你將不得不調用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); 
    } 
} 
+0

但隨後切換不與其他文件相一致嗎? –

+0

@NaomivanMaasakkers否,除非您使用ControlP5的** plugTo **功能 –

+0

@NaomivanMaasakkers您可以嘗試將'SecondApplet'實例(在我的代碼中它是'sa'變量)傳遞到'ControlP5'構造函數中,或者將'ControlP5' 'SecondApplet'類的東西。 –