Fetch Moon Phase Image (or other image) from the Web -- Display Class
in
Share your Work
•
2 years ago
This isn't a complete sketch; it's a class that displays the phase of the moon which can be easily incorporated into sketches. You could change the url and pull in any image from the web. It refetches the image every two hours.
In your main sketch, declare the class: MoonImage myMoonImage;
In your main setup, call the class: myMoonImage = new MoonImage(width-((width/4))+60,(height/2),#3FB748,255);
You can set it's position; tint() and transparency.
In your main draw, call the drawmoon: myMoonImage.DrawMoonImage();
It doesn't calculate the moon phase, it pulls an image from http://tycho.usno.navy.mil//cgi-bin/phase.gif which seems very reliable. Accordingly, it won't work online unless you set up a proxy, wget, or similar.
It loads the image 'socket_1.png' if it can't fetch the moon (it hasn't happened yet). You need an image called socket_1.png in your data directory if you want to placeholder image when the phase isn't available. Otherwise, comment out line 44.
In your main sketch, declare the class: MoonImage myMoonImage;
In your main setup, call the class: myMoonImage = new MoonImage(width-((width/4))+60,(height/2),#3FB748,255);
You can set it's position; tint() and transparency.
In your main draw, call the drawmoon: myMoonImage.DrawMoonImage();
It doesn't calculate the moon phase, it pulls an image from http://tycho.usno.navy.mil//cgi-bin/phase.gif which seems very reliable. Accordingly, it won't work online unless you set up a proxy, wget, or similar.
It loads the image 'socket_1.png' if it can't fetch the moon (it hasn't happened yet). You need an image called socket_1.png in your data directory if you want to placeholder image when the phase isn't available. Otherwise, comment out line 44.
- public class MoonImage {
- int x,y;
- color c_back;
- int moonalpha;
- PImage ImageSource;
- PImage NoMoon;
- String URL;
- int UpdateInterval;
- int LastUpdate;
- public MoonImage (int xtop, int ytop, color color_c, int m_alpha) {
- x = xtop;
- y = ytop;
- c_back = color_c;
- moonalpha = m_alpha;
- URL = "http://tycho.usno.navy.mil//cgi-bin/phase.gif";
- GetMoon();
- //ImageSource = loadImage(URL,"png");
- UpdateInterval = 2*60*60*1000; // update every two hours
- //noLoop();
- }
- public void DrawMoonImage() {
- fill(c_back,moonalpha);
- tint(c_back);
- imageMode(CENTER);
- if (ImageSource.width >=1 && ImageSource.height >=1 ) {
- image(ImageSource,x,y);
- } else println("moon image not available");
- if (millis()-LastUpdate >= UpdateInterval) {
- myMoonImage.GetMoon();
- println("Updating Moon . . . ");
- }
- }
- public void GetMoon() {
- try {
- ImageSource = loadImage(URL,"png");
- } catch (NullPointerException e) {
- ImageSource = loadImage("socket_1.png");
- }
- LastUpdate = millis();
- }
- }
1