Difference between revisions of "CSC352 Java Threads: Producer-Consumer Lab"
(Created page with "--~~~~ ---- <bluebox> The goal of this lab is to see how threads can be used along size a main application that is already threaded. A perfect example of this is a Processing...") |
(→Processing GUI version) |
||
Line 64: | Line 64: | ||
==Processing GUI version== | ==Processing GUI version== | ||
<br /> | <br /> | ||
− | <source> | + | <source lang="java"> |
import java.util.ArrayList; | import java.util.ArrayList; | ||
import java.util.Random; | import java.util.Random; |
Revision as of 21:10, 18 September 2013
--D. Thiebaut (talk) 21:10, 18 September 2013 (EDT)
The goal of this lab is to see how threads can be used along size a main application that is already threaded. A perfect example of this is a Processing application where the draw() method is called by a thread that is timed to run approximately 30 times a second (or whatever interval is specified by the frameRate() method).
Contents
Version 1: Displaying 30 random rectangles a second
- The following Java program is a Processing app. taylored for Eclipse. The version that follows is the one for the Processing IDE. They are both similar, but in one case the application is included in a class extending the PApplet class of Processing. In the second case, the Processing GUI hides the PApplet implementation.
- Please refer to the tutorials at http://cs.smith.edu/dftwiki/index.php/Tutorials#Processing_and_Eclipse if you are interested in running Processing applets from Eclipse.
Eclipse-Ready version
// MainApplet.java
// D. Thiebaut
// This application needs the core.jar library of the Processing package to be included in the
// build path of the application. See
import java.util.ArrayList;
import java.util.Random;
import processing.core.PApplet;
public class MainApplet extends PApplet {
ArrayList<Rect> rects = new ArrayList<Rect>();
Random generator = new Random( System.currentTimeMillis() );
class Rect {
int x; int y; int w; int h;
int color;
Rect() { this.x = this.y = 0; this.w = this.h = 10; color= 0xff6699cc; }
Rect( int x, int y, int w, int h, int c ) { this.x = x; this.y = y; this.w = w; this.h = h; color = c; }
public Rect randomRect() {
return new Rect( generator.nextInt( width ), generator.nextInt( height ),
generator.nextInt( width ), generator.nextInt( height ),
generator.nextInt( 0x77ffffff ) );
}
}
public void setup() {
size( 600, 400 );
smooth();
frameRate( 30 );
}
public void draw() {
Rect r = getNewRect();
stroke( 0x000000 );
fill( r.color );
rect( r.x, r.y, r.w, r.h );
}
private Rect getNewRect() {
return (new Rect()).randomRect();
}
}
Processing GUI version
import java.util.ArrayList;
import java.util.Random;
ArrayList<Rect> rects = new ArrayList<Rect>();
Random generator = new Random( System.currentTimeMillis() );
class Rect {
int x; int y; int w; int h;
int col;
Rect() { this.x = this.y = 0; this.w = this.h = 10; col= 0xff6699cc; }
Rect( int x, int y, int w, int h, int c ) { this.x = x; this.y = y; this.w = w; this.h = h; col = c; }
public Rect randomRect() {
return new Rect( generator.nextInt( width ), generator.nextInt( height ),
generator.nextInt( width ), generator.nextInt( height ),
generator.nextInt( 0x77ffffff ) );
}
}
void setup() {
size( 600, 400 );
smooth();
frameRate( 30 );
}
void draw() {
Rect r = getNewRect();
stroke( 0x000000 );
fill( r.col );
rect( r.x, r.y, r.w, r.h );
}
Rect getNewRect() {
return (new Rect()).randomRect();
}