Adding delay in Processing

2019-01-29 11:13发布

问题:

so I am new to Processing and basically I am doing a program that when it runs, it opens a window with 4 different images, each of the image have description underneath. In the methods below, I created two random methods, one for the reviews number and the other for the review comments, I would like the comments to not be generated all the time for every film - more like popping up randomly, because it cause too much chaos trying to read them all. Also to check weather I can add arrays together of string and integer for the average value of the review number.

Below is the code, would appreciate your help. thanks.

import g4p_controls.*;
import ddf.minim.*;

PFont font;
PImage img1,img2; // background images for two different windows
PImage fimg1, fimg2, fimg3, fimg4, fimg5, fimg6; //images of movies
int rectX,rectY;

GButton btn;
GWindow window;

Minim minim;
AudioPlayer player;

String[] rev_film1 = {"The Best Wolverine Movie","Logan is another level","Disappointment","Such a sad farewell"}; //Logan
String[] rev_film2 = {"A scary movie that isn't scary.","Terrifyingly brilliant.","The perfect blend of comedy and horror","The IT Factor"}; //IT
String[] rev_film3 = {"Soul-less,Confused,Loud.","Devastatingly Disappointed","A technical masterpiece","A visual wonder that lacks depth"}; //Dunkirk
String[] rev_film4 = {"Disgrace", "Worst Star Wars movie", "TERRIBLE","A Betrayal to the Legacy"}; //Starwars


int[] rat_film1 = {9,8,2,2}; 


float r;

void setup()
{
  size(1150,600,JAVA2D);
  surface.setTitle(" - The Theatre of Dreams Database - ");
  font = loadFont("BerlinSansFB-Reg-48.vlw");
  img1 = loadImage("film2.jpg");
  background(img1);

  btn = new GButton(this,900,350,100,50, "Enter Website");

  minim = new Minim(this);
  player = minim.loadFile("sound.mp3");
  player.play();
}

void draw()
{
  fill(255);
  textFont(font,32);
  text("Welcome to", 850, 85);
  text("The Theatre of Dreams", 760, 175);
  text("Database", 870, 220);
}

void handleButtonEvents(GButton button, GEvent event) 
{
  if (button == btn && event == GEvent.CLICKED) 
  {
    createWindow();
    btn.setEnabled(false);
  }
}

void createWindow() // creating new window with mouse click
{
  window =  GWindow.getWindow(this, " - Top 4 Movies in 2017 - ", 100, 50, 1150, 600, JAVA2D);
  window.addDrawHandler(this, "windowDraw");
  window.addOnCloseHandler(this, "windowClosing"); 
  window.setActionOnClose(GWindow.CLOSE_WINDOW);
}

void windowDraw(PApplet app, GWinData data)
{
  img2 = loadImage("film3.jpg");
  app.background(img2);

  app.text(" - Top 4 Movies in 2017 - ",440,85);
  app.fill(255);
  app.textFont(font,25);

  fimg1 = loadImage("logan.jpg");
  fimg2 = loadImage("it.jpg");
  fimg3 = loadImage("dunkirk.jpg");
  fimg4 = loadImage("starwars.jpg");

  //////////Film 1 - LOGAN
    app.image(fimg1,5,140,180,170);
    app.text("Rating: 8.1 / 10",5,340); //fixed rating

    app.text("Genres: Action | Drama", 5, 365);
    app.text("| Sci-Fi | Thriller",5,390);

    //Ratings that are constantly changing using the random function
    for (int i = 0; i < 50; i++) 
    {
      r = random(0, 6);
    }

    String user = "Ratings by users: " + nf(r,0,1) + " / 10";
    app.text(user, 5,430);

    // the random function of the comments
    int index = int(random(rev_film1.length)); 
    String user11 = "Reviews: " + "\n" + rev_film1[index];
    app.text(user11, 5,460);


  ////////////////////Film 2 - IT
    app.image(fimg2,960,360,180,170);
    app.text("Rating: 7.6 / 10", 700,400);
    app.text("Genres: Drama | Horror",700,430);
    app.text("| Thriller",700,460);

  //Ratings that are constantly changing using the random function
    for (int i = 0; i < 50; i++) 
    {
      r = random(5, 10);
    }
    String user2 = "Ratings by users: " + nf(r,0,1) + " / 10";
    app.text(user2, 700,500);
    int index2 = int(random(rev_film2.length)); // the random function of the comments
    String user22 = "Reviews: " + "\n" + rev_film2[index2];
    app.text(user22, 700,540);



  /////////Film 3 - DUNKIRK
    app.image(fimg3,320,250,180,170);
    app.text("Rating: 8.1 / 10",320,445); //fixed rating

    app.text("Genres: Action | Drama", 320, 470);
    app.text("| History | Thriller | War",320,495);

    //Ratings that are constantly changing using the random function
    for (int i = 0; i < 50; i++) 
    {
    r = random(0, 5);
    }

    String user3 = "Ratings by users: " + nf(r,0,1) + " / 10";
    app.text(user3, 320,530);
    int index3 = int(random(rev_film3.length)); // the random function of the comments
    String user33 = "Reviews: " + "\n" + rev_film3[index3];
    app.text(user33, 320,560);


  /////////////Film 4 - STAR WARS
    app.image(fimg4,570,120,180,170);
    app.text("Rating: 7.6 / 10", 760,140); //fixed rating

    app.text("Genres: Action | Adventure | Fantasy ", 760,168); 
    app.text("| Sci-Fi", 760,195);

    //Ratings that are constantly changing using the random function
    for (int i = 0; i < 50; i++) 
    {
      r = random(0, 2);
    }

    String user4 = "Ratings by users: " + nf(r,0,1) + " / 10";
    app.text(user4, 760,220);
    int index4 = int(random(rev_film4.length)); // the random function of the comments
    String user44 = "Reviews: " + "\n" + rev_film4[index4];
    app.text(user44, 760,250);
}


public void windowClosing(GWindow w)
{
  btn.setEnabled(false);
  player.close();
  minim.stop();
  exit();
}

回答1:

Please try to post a MCVE instead of your full program. For example, try creating a simple sketch that shows a circle every X seconds. That way we can focus on your problem instead of all the extra stuff that has nothing to do with your question.

But to answer your question, you can use the millis() function or the frameCount variable to check how much time has gone by, then do something every X seconds or every X frames.

Related posts:

  • How to make a delay in processing project?
  • How can I draw only every x frames?
  • Removing element from ArrayList every 500 frames
  • Timing based events in Processing
  • How to add +1 to variable every 10 seconds in Processing?
  • How to create something happen when time = x
  • making a “poke back” program in processing
  • Processing: How do i create an object every “x” time
  • Timer using frameRate and frame counter reliable?

Please also consult the Processing reference for more information.

If you still can't get it working, please post a MCVE (not your full project!) in a new question and we'll go from there. Good luck.