from_scratch

UCSD applet Sun - 2

Oct 15th, 2015
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.34 KB | None | 0 0
  1. package guimodule;
  2.  
  3. import processing.core.PApplet;
  4. import processing.core.PImage;
  5.  
  6. public class MyPApplet2 extends PApplet{
  7.     private String url = "http://i.imgur.com/ZZoY9Ni.jpg";
  8.     private PImage backgroundPic;
  9.    
  10.     public void setup(){
  11.         size(400, 400);//(w,h)
  12.         background(255);//set canvas color R=255, G=255, B= 255
  13.         stroke(0);//set pen color R=0; G=0; B=0
  14.         backgroundPic = loadImage(url);
  15.         backgroundPic.resize(0, height);//resizing the pic to the full height of canvas
  16.         image(backgroundPic, 0, 0);//x = 0; y = 0;
  17.     }
  18.     public void draw(){
  19.         int[] color = sunColorSec(second());//Det finns hour() och minute() metoder också.
  20.         fill(color[0], color[1], color[2]);//passing R, G and B
  21.         ellipse(width-width/6, height/6, width/4, height/5);
  22.     }
  23.     private int[] sunColorSec(int s) {
  24.         int[] rgb = new int[3];//We want to return R, G and B colors
  25.         //Scale the brightness of the yellow color of the Sun based on seconds.
  26.         //0 seconds is black. 30 seconds is bright yellow
  27.         float diffFrom30 = Math.abs(30 - s);
  28.         float ratio = diffFrom30/30;//gives a number between 0 and 1
  29.         rgb[0] = (int)(255 * ratio);
  30.         rgb[1] = (int) (255 * ratio);
  31.         rgb[2] = 0; //The blue color is not needed to make different shades of yellow.
  32.         //System.out.println("R: " + rgb[0] + " G: " + rgb[1] + " B: " + rgb[2]);
  33.         return rgb;
  34.     }
  35. }//class MyApplet2 ends here.
Advertisement
Add Comment
Please, Sign In to add comment