Open processing file when button is pressed

edited March 2015 in Library Questions

So I'm a student in my first year of engineering. I'm new to programming, we only had a few lessons in Greenfoot (java'ish) yet. But we have a project where we need to make a basic game. I'm making the GUI. So all code below is a lot of google-searching and "trail and error".

I got 2 .pde files. One wich cycles through images when you press the enter key. Another one created in controlP5 (library), it contains some buttons... I got the exit button working, it exits out of the screen. Phew. :p Next thing on my list is getting the "Thema"-button to open the file which cycles through the images. It doesn't really have to open it, if it just runs the code and has a button to return to the main GUI it would be perfect. :)

Code of the GUI:

import controlP5.*;

ControlP5 cp5;

int myColor = color(255);

int c1,c2;

float n,n1;

void setup() {
  size(700,700);
  noStroke();
  cp5 = new ControlP5(this);
  
  // create a new button with name 'buttonA'
  cp5.addButton("Speel")
     .setId(2)
     .setPosition(100,100)
     .setSize(500,30)
     .getCaptionLabel().align(CENTER,CENTER)
     ;
  
  cp5.addToggle("Thema")
     .setPosition(100,150)
     .setSize(500,30)
     .setId(0)
     .getCaptionLabel().align(CENTER,CENTER)
     ;
     
  cp5.addButton("Spelregels")
     .setPosition(100,200)
     .setSize(500,30)
     .setId(4)
     .getCaptionLabel().align(CENTER,CENTER)
     ;
          
  cp5.addToggle("Exit")
     .setPosition(100,250)
     .setSize(500,30)
     .setId(5)
     .getCaptionLabel().align(CENTER,CENTER)
     ;
  
  cp5.addSlider("Volume")
    .setValue(300)
    .setPosition(200,650)
    .setWidth(300)
    .setHeight(30)
    .setSliderMode(Slider.FLEXIBLE)
    .setRange(0,100)
    .getCaptionLabel().align(ControlP5.LEFT,ControlP5.TOP_OUTSIDE)
    ;
  
     
  PFont p = createFont("Verdana",16);
  cp5.setControlFont(p);

}

void draw() {
  background(myColor);
  myColor = lerpColor(c1,c2,n);
  n += (1-n)* 0.1; 
}

public void controlEvent(ControlEvent theEvent) {
  println(theEvent.getController().getName());
  n = 0;
}

public void Speel(int theValue) {
  println("a button event from Speel: "+theValue);
  c1 = c2;
  c2 = color(0,160,100);
}

public void Thema(int banaan) {
  println("a button event from Thema: "+banaan);
  c1 = c2;
  c2 = color(120,255,0);
if (banaan == 1){
  open("");
 }
}

public void Spelregels(int theValue) {
  println("a button event from Spelregels: "+theValue);
  c1 = c2;
  c2 = color(255,255,0);
}



public void Exit(int aap) {
  println("a button event from Exit: "+aap);
if (aap == 1) {
    exit();
  }  
}

public void Volume(int theValue) {
  println("a button event from Volume: "+theValue);
  c1 = c2;
  c2 = color (0,0,0);
  
}

The other file. Programmed it myself, kinda proud. :D

String[] filenames = {"Thema Luc.png", "Thema Sport.png", "Thema Unicorn.png", "Paint Thema Zomer.png", "Thema Pasen.png"};
 
PImage[] images = new PImage[filenames.length];

int imageIndex = 0;
   
void setup() {
  size(700,700); 
  
  for(int i = 0; ifilenames.length-1)
  imageIndex = 0;
}

void keyPressed(){
    if (key == ENTER){
  imageIndex++;
  }
    if (imageIndex>filenames.length-1)
  imageIndex = 0;

 }
Tagged:

Answers

  • edited March 2015

    Great job!

    I could be wrong, but I'm not sure you can run another processing script within an existing one. Your best bet is to integrate the second script code into the first. Controlp5 probably has show and hide methods for it's elements so you can hide your controls while you view your images. You could also use the graphics object, to overlay or switch to a new "window" for your image display part.

  • edited March 2015

    Aha! Worked :)

    Now another problem. I want to show an image starting when I hit a button. When I hit the return button (or hit the "k"-key for example) the image should dissaspear.

    public void Spelregels(int appel) {
      println("a button event from Spelregels: "+appel)  ;
      if (appel == 1){
      image(img,0,0,width,height);
     }
    

    This piece does the trick almost as I want it to. However when I hit the Bang. The image gets shown but only for a split second.... I tried working with an else statement or even a while-loop... None of it helps. Any tips how I can get this working?

    English is not my first language excuse me if the sentences I use don't look very polished. :)

  • edited March 2015

    Ok, the reason why you see your image only for one second is because your peace of code is only run once. In processing, the program loops through the draw() function lots of times per second and if you need an image to be displayed all the time, your image() should go inside draw(), or else it's gonna be visible only for a one frame. Another thing is why would you use 2 sketches? Processing supports tabs, if you need to separate your code, its better to use them.

  • Putting it in the draw method is tricky to me. It draws it as I run the program. I would have to link a condition to it (I don't know really)? (boolean?, I don't really understand these yet...). The condition is true when I hit the Bang-button, otherwise it should be false?

    And the thing about merging the 2 programs is fixed. I just copied the code of the smaller one into the bigger one (in there right place ofcourse). And it works Working with tabs might be better but I really have no idea how to go about it. Clear examples of these things are hard to find. :)

Sign In or Register to comment.