We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Design Implementation question
Page Index Toggle Pages: 1
Design Implementation question (Read 577 times)
Design Implementation question
Apr 11th, 2010, 10:46am
 
I don't understand how I can switch applets in the same window. I want to switch to an applet that plays a video file after I click a button on a button menu. On the video applet, I want a two buttons that asks the user to replay or go back to the button menu. So basically, a menu system that displays video and then replays the video or goes back to the menu if the user chooses. I've seen the two applet hack here on the threads, but that opens up a new window instead of using the current window (having the user open too many windows is bad).

In terms of code, I have two applets. One that has the buttons, and one that plays videos (taken from the examples).

Code:

// The Menu with the buttons

import controlP5.*;

ControlP5 controlP5;

void setup(){

size(500,500);
smooth();
controlP5 = new ControlP5(this);
controlP5.addButton("trainOne", 0,100,100,48,20);
controlP5.addButton("trainTwo", 0,200,200, 30, 20);
controlP5.addButton("trainThree", 0, 150,150, 20, 20);
}

void draw() {
background(0);
}

public void controlEvent(ControlEvent e) {
println(e.controller().name());
}

public void trainOne(int v) {
println("This should play train.mov");
}

public void trainTwo(int v) {
println("This should play train2.mov");
}

public void trainThree(int v) {
println("This should play train3.mov");
}




Code:

// Taken from the examples with slight modifications. Plays the movie train.mov. For now, just assume train.mov, train2.mov, and train3.mov are the same video file.

import processing.video.*;
import controlP5.*;

Movie myMovie;
ControlP5 controlP5;

void setup() {
size(640, 480, P2D);
background(0);

controlP5 = new ControlP5(this);
controlP5.addButton("Return to menu", 0,100,100,48,20);
controlP5.addButton("Replay", 0,100,100,48,20);

// Load and play the video in a loop
myMovie = new Movie(this, "train.mov");
myMovie.loop();
}

void movieEvent(Movie myMovie) {
myMovie.read();
}


public void controlEvent(ControlEvent e) {
println(e.controller().name());
}

public void Replay(int v) {
println("Assume this replays");
}


public void ReturnToMenu(int v) {
println("How to do this back to menu?");
}

void draw() {
tint(255, 20);
image(myMovie, mouseX-myMovie.width/2, mouseY-myMovie.height/2);
}



How do I tie these two together into the same window?
Re: Design Implementation question
Reply #1 - Apr 11th, 2010, 10:47pm
 
Make one sketch that has a flag for playing the video.
boolean isPlaying = false;

Display the buttons as usual, instead of starting the video right away, at the press of a button, hide and show the proper buttons, then "myMovie.loop();" and "isPlaying = true;".

Inside draw(), "if (isPlaying) image(myMovie...);"

There are movie controls such play and stop, etc.
http://processing.org/reference/libraries/video/index.html

Hope that at least gets you started.
Re: Design Implementation question
Reply #2 - Apr 17th, 2010, 7:56pm
 
Thanks for your input! Sorry about being away for a while; I had a lot of other work. Now I'm back!

I wrote up some really quick code. It seems like it should work, but it doesn't work exactly. It's also very very slow and eventually freezes. Sometimes when I click the ibutton, it'll show the other two buttons but then it'll switch back to showing only the ibutton. This shouldn't happen because once you click the ibutton, it'll display the movie menu (two buttons and the movie). Also, a lot of the time the mouse click doesn't seem to register probably because the program is running very slowly. The biggest thing though, is that the movie is no where to be seen.

On a side note, I want to keep using opengl because I'm planning to render stuff in 3D space later on.

Code:

import processing.opengl.*;
import java.awt.*;
import processing.video.*;

PGraphics pg = new PGraphics();

boolean isPlay = false;
Movie myMovie;

ImageButton ibutton = new ImageButton(200, 0, loadImage("s.jpg"), "select movie");
ImageButton iReplay = new ImageButton(100,100, loadImage("s.JPG"), "return to menu");
ImageButton iMenu = new ImageButton(300, 100, loadImage("s.JPG"), "replay movie");




void setup(){
size(720, 405, OPENGL);
smooth();  
}

void draw() {
background(10,10,10);

if(isPlay)
   playMovie();
else
   playMenu();
   
}

void playMovie() {
 displayMovie();
 displayMovieMenu();
}

void displayMovieMenu() {
 iMenu.display();
 iReplay.display();
 
 iMenu.update();
 iReplay.update();
}

void displayMovie() {
 myMovie = new Movie(this, "t.mp4");
 //myMovie.loop();
 
 image(myMovie, 400,400);
}

void movieEvent(Movie myMovie) {
 myMovie.read();
}

void playMenu() {
ibutton.display();
ibutton.update();
}



class Button {
 int x, y;
 boolean pressed = false;
 
 void pressed() {
   if(mousePressed) {
     pressed = true;
   }
   else {
     pressed = false;
   }
 }
}  

class ImageButton extends Button{
 PImage current;
 String name;
 ImageButton(int ix, int iy, PImage i, String name) {
   x = ix;
   y = iy;
   current = i;
   this.name = name;
 }
 
 void update() {
   pressed();
   if (pressed && name.equals("select movie")) {
     isPlay = true;
     println("pressed " + name);
   }
   
   if (pressed && name.equals("return to menu")) {
     isPlay = false;
     println("pressed " + name);
   }
   
   if (pressed && name.equals("replay movie")) {
     isPlay = true;
      myMovie.play();
     
     println("pressed " + name);
   }
 }
 
 void display() {
   image(current, x, y);
 }
}
     


I'm not doing something right. Anyone can help me a bit?
Page Index Toggle Pages: 1