Difference between revisions of "CSC352 Java Threads: Producer-Consumer Lab"
(→Processing GUI version) |
(→Eclipse-Ready version) |
||
Line 24: | Line 24: | ||
import java.util.Random; | import java.util.Random; | ||
import processing.core.PApplet; | import processing.core.PApplet; | ||
− | |||
public class MainApplet extends PApplet { | public class MainApplet extends PApplet { | ||
Line 32: | Line 31: | ||
class Rect { | class Rect { | ||
int x; int y; int w; int h; | int x; int y; int w; int h; | ||
− | int | + | int col; |
− | Rect() { this.x = this.y = 0; this.w = this.h = 10; | + | 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; | + | 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() { | public Rect randomRect() { | ||
return new Rect( generator.nextInt( width ), generator.nextInt( height ), | return new Rect( generator.nextInt( width ), generator.nextInt( height ), | ||
Line 45: | Line 44: | ||
size( 600, 400 ); | size( 600, 400 ); | ||
smooth(); | smooth(); | ||
− | frameRate( | + | frameRate( 10 ); // draw() will be called 10 times a second |
} | } | ||
Line 51: | Line 50: | ||
Rect r = getNewRect(); | Rect r = getNewRect(); | ||
stroke( 0x000000 ); | stroke( 0x000000 ); | ||
− | fill( r. | + | fill( r.col ); |
rect( r.x, r.y, r.w, r.h ); | rect( r.x, r.y, r.w, r.h ); | ||
} | } | ||
Line 62: | Line 61: | ||
</source> | </source> | ||
<br /> | <br /> | ||
+ | |||
==Processing GUI version== | ==Processing GUI version== | ||
<br /> | <br /> |
Revision as of 21:26, 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 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 ) );
}
}
public void setup() {
size( 600, 400 );
smooth();
frameRate( 10 ); // draw() will be called 10 times a second
}
public void draw() {
Rect r = getNewRect();
stroke( 0x000000 );
fill( r.col );
rect( r.x, r.y, r.w, r.h );
}
private Rect getNewRect() {
return (new Rect()).randomRect();
}
}
Processing GUI version
// MainApplet.sketch
// D. Thiebaut
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();
}