Difference between revisions of "CSC352 Java Threads: Producer-Consumer Lab"

From dftwiki3
Jump to: navigation, search
(Processing GUI version)
(Processing GUI version)
Line 68: Line 68:
 
import java.util.Random;
 
import java.util.Random;
  
 +
ArrayList<Rect> rects = new ArrayList<Rect>();
 +
Random generator = new Random( System.currentTimeMillis() );
  
 
+
class Rect {
  ArrayList<Rect> rects = new ArrayList<Rect>();
 
  Random generator = new Random( System.currentTimeMillis() );
 
 
 
  class Rect {
 
 
     int x; int y; int w; int h;
 
     int x; int y; int w; int h;
 
     int col;
 
     int col;
Line 83: Line 81:
 
               generator.nextInt( 0x77ffffff ) );
 
               generator.nextInt( 0x77ffffff ) );
 
     }
 
     }
  }
+
}
 
    
 
    
  void setup() {
+
void setup() {
 
     size( 600, 400 );
 
     size( 600, 400 );
 
     smooth();
 
     smooth();
 
     frameRate( 30 );
 
     frameRate( 30 );
  }
+
}
 
    
 
    
  void draw() {
+
void draw() {
 
     Rect r = getNewRect();
 
     Rect r = getNewRect();
 
     stroke( 0x000000 );
 
     stroke( 0x000000 );
 
     fill( r.col );
 
     fill( r.col );
 
     rect( r.x, r.y, r.w, r.h );
 
     rect( r.x, r.y, r.w, r.h );
  }
+
}
 
    
 
    
  Rect getNewRect() {
+
Rect getNewRect() {
 
     return (new Rect()).randomRect();
 
     return (new Rect()).randomRect();
  }
+
}
 
 
  
 
</source>
 
</source>

Revision as of 21:11, 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).




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();
}