GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Happy Coding
  • Tutorials
  • Blog

This view

  • Original page
  • New address

GeezerVison is showing happycoding.io — Processing Showcase

Processing Showcase

Java Tutorials

  • Java Setup

  • Hello World
    • Higher Lower (Human Player)
    • Higher Lower (Random Computer Player)
    • Free Paycheck Calculator

  • Creating Classes
    • Taylor Swift Markov Model

  • Errors

  • Inheritance

  • Swing
    • Circle Button
    • Higher Lower

  • Libraries
    • Mastodon Bot

  • Eclipse

  • Exporting Jar Files

  • Processing in Java
    • Swing Control Window
    • Processing Showcase

  • Unit Testing
  • p5.js Tutorials
  • HTML Tutorials
  • JavaScript Tutorials
  • Processing Tutorials
  • Java Tutorials
  • Server Tutorials
  • Google Cloud
  • Android Tutorials
  • LibGDX Tutorials
  • How-To Guides
  • Interviewing

  • Gallery

  • Blog

  • Teaching

  • Forum

  • Mastodon github
    etsy youtube RSS
Tutorials
/
Java Tutorials
/
Processing in Java
/
Processing Showcase

Previous:
Processing in Java

Processing Showcase


If you have a bunch of Processing sketches and want to show them one after the other, you can use this Java program:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import processing.core.PApplet;

public class ProcessingShowcase {

	public static void main(String[] args){

		List<PApplet> sketches = new ArrayList<>();
		sketches.add(new ProcessingSketchOne());
		sketches.add(new ProcessingSketchTwo());
		//add sketch classes here!


		//this is optional, but causes the sketches to appear in random order
		//copy this to where the index is reset to randomize whenever you loop
		Collections.shuffle(sketches);

		int currentSketchIndex = 0;

		while(true){

			//using a broad exception to just skip over any sketches that have problems
			try {
				PApplet sketch = sketches.get(currentSketchIndex);
				String[] processingArgs = {""};
				PApplet.runSketch(processingArgs, sketch);

				// this is useful after we've looped
				sketch.getSurface().resumeThread();

				//how long do you want each sketch to display for
				//60 seconds
				Thread.sleep(60*1000);

				// prevent sketches from continuing to run while they're hidden
				sketch.getSurface().pauseThread();

				//hide the sketch
				sketch.getSurface().setVisible(false);
			}
			catch (Exception e) {
				System.err.println("There was a problem running a sketch!");
				e.printStackTrace();
				System.err.println("Skipping to the next sketch.");
			}

			// move to the next sketch, loop back to the first after finishing
			currentSketchIndex++;
			if(currentSketchIndex >= sketches.size()){
				currentSketchIndex = 0;
			}

			//go back to the top of the while(true) loop
		}
	}
}

This code assumes that you have a bunch of classes that look like this:

import processing.core.PApplet;

public class MySketch extends PApplet {

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

	public void draw(){
		background(64);
		ellipse(mouseX, mouseY, 20, 20);
	}
}

You can get these classes by exporting as an application in the Processing editor, or you can manually create them using Processing as a Java library.

Anyway, now that you have a bunch of these files, the code first creates a List containing instances of these classes. For each sketch in that List, the code then runs the sketch and waits for 60 seconds. At the end of 60 seconds, the code pauses the sketch and hides it, and then moves to the next sketch. When it does has gone through every sketch, it starts back at the beginning, unpauses the first sketch, and shows it.

In other words, this program shows each sketch, one at a time, for 60 seconds at a time. This is useful if you want to showcase a bunch of sketches in a student presentation or an art gallery.

If you find this useful, I’d love to hear from you!

Tweak Ideas

  • Working with a group of students or artists? You could have them add their classes to a GitHub repository, that way you don’t have to manually copy everything over.
  • Create a gallery that shows previews of each sketch that you can click on to launch.

Previous:
Processing in Java

Processing in Java Examples

Swing Control Window Swing Control Window

Create a Processing sketch with a Swing control window.

Processing Showcase Processing Showcase

Create a Java program that cycles through Processing sketches.

Add Your Own Example Add Your Own Example

Add your own example to Happy Coding.

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.