Help with Traffic Cam Loader/Display Class
in
Programming Questions
•
2 years ago
The following sketch is supposed to load a series of new traffic camera images (they are updated approximately every five minutes) and then display each image for 4 seconds one at a time with a caption. For some reason, it's displaying the first image without a caption, and then displays the last image with a caption, and doesn't loop through or cycle through the images. Help would be appreciated. I think the problem is with lines 37 through 56 of the TrafficCams class.
Main Sketch (just calls the class with some setup variables, xpos,ypos,width,height,color,alpha,displaytime):
TrafficCams Class
Main Sketch (just calls the class with some setup variables, xpos,ypos,width,height,color,alpha,displaytime):
- TrafficCams myTrafficCams;
- void setup() {
- size(400,400);
- smooth();
- background(0);
- myTrafficCams = new TrafficCams(50,100,170,125,255,255,4); //
- }
- void draw() {
- myTrafficCams.DrawTrafficCams();
- }
TrafficCams Class
- public class TrafficCams {
- int x,y,wx,hy;
- color c_back;
- int trafficalpha;
- int cyclerate;
- PImage ImageSource; // not used
- PImage[] LoadedCams;
- PImage NoImage;
- String URL;
- String[] cams = new String[4];
- String Caption;
- int UpdateInterval;
- int LastUpdate;
- public TrafficCams (int xtop, int ytop, int w_x, int h_y, color color_c, int t_alpha, int c_rate) {
- x = xtop;
- y = ytop;
- wx = w_x;
- hy = h_y;
- c_back = color_c;
- cyclerate = c_rate * 1000;
- trafficalpha = t_alpha;
- NoImage = loadImage("socket_1.png");
- LoadedCams = new PImage[cams.length];
- cams[0] = "http://www.dot.ca.gov/dist1/d1tmc/hwypix/h101p89.jpg#101 North of Route 299, South";
- cams[1] = "http://www.dot.ca.gov/dist1/d1tmc/hwypix/h101p89n.jpg#101 North of Route 299, North";
- cams[2] = "http://www.dot.ca.gov/dist1/d1tmc/hwypix/h101p80n.jpg#101 at R Street, Eureka, North";
- cams[3] = "http://www.dot.ca.gov/dist1/d1tmc/hwypix/h101p80s.jpg#101 at R Street, Eureka, South";
- GetTrafficCams();
- delay(1000);
- UpdateInterval = 5*60*1000; // update every five minutes
- }
- public void DrawTrafficCams() {
- fill(c_back,trafficalpha);
- for (int i = 0; i < cams.length; i++) {
- //try {
- image(LoadedCams[i],x,y,wx,hy);
- String[] p = splitTokens(cams[i],"#");
- Caption = p[1];
- text(Caption,x+4,y+hy-8);
- //} catch (NullPointerException e) {
- // image(NoImage,x,y);
- //}
- delay(cyclerate);
- println("cyclevalue: " + str(i) + " caption: " + Caption);
- }
- if (millis()-LastUpdate >= UpdateInterval) {
- myTrafficCams.GetTrafficCams();
- println("Updating Traffic Cams . . . ");
- }
- }
- public void GetTrafficCams() {
- for (int i = 0; i < cams.length; i++) {
- String[] p = splitTokens(cams[i],"#");
- URL = p[0];
- println("Loading Traffic Cam: " + URL);
- try {
- LoadedCams[i] = loadImage(URL,"JPG");
- } catch (NullPointerException e) {
- LoadedCams[i] = NoImage;
- }
- delay(2000);
- println("LoadedCams size: " + str(LoadedCams.length));
- }
- LastUpdate = millis();
- }
- }
1