How do you make an image glow on processing?

edited November 2017 in How To...

I'm making a fairly simple sketch with 1 class and a planet (which I got a picture from online) but I want to know how to make that image have an outlining kind of flashing glow.

Hopefully someone can help me :)

This is my main sketch:

PImage img;
PImage sun;

int numberOfplanets = 0; //this variable controls how many planets
planets[] big = new planets[numberOfplanets]; //this creates an array of planets
//make an array list with a flexible number of objects in it
ArrayList<planets> biglist = new ArrayList<planets>();


void setup() 
{
  size(700, 393);
  noStroke();
  frameRate(30);
  ellipseMode(RADIUS);
  for ( int i=0; i<numberOfplanets; i=i+1) {
    big[i] = new planets(width/2, height/2); //this creates Ball object in the array spaces from 0 to 1 to 2 to....numberOfBalls
  }
}

void draw() 
{
  background(img = loadImage("background.jpg"));
  tint(255);
  sun = loadImage("sun.png");
  image(sun, 255, 100, 200, 180);
  tint(300);


  for ( int i=0; i<numberOfplanets; i=i+1) {
    big[i].move(); //this will move each Ball object in the array
    big[i].display(); //this will display each Ball object
  }

  // move and display all the Balls in the balls list
  for ( int i=0; i<biglist.size(); i=i+1) {
    planets temp = biglist.get(i); //this gets the ith object in the array list
    temp.display();
    temp.move();
    for ( int j=0; j<biglist.size(); j=j+1) {
      if (i != j ) { //avoid when i==j
        planets temp2 = biglist.get(j); //gets the other ball
        if (temp.checkCollide(temp2)) {
          ; //check if the ball collides with another one
          if ( temp2.getRad()>temp.getRad()) {
            //temp2 is bigger than temp
            biglist.remove(temp);
            temp2.setRad(temp2.getRad()+temp.getRad()); //adds the two values
          } else {
            biglist.remove(temp2);
            temp.setRad(temp2.getRad()+temp.getRad()); //adds the two values
          }
        }
      }
    }
  }
}  
//this method runs every time the mouse is clicked
void mousePressed() {
  //add one ball object to the bobslist arraylist

  biglist.add(new planets (mouseX, mouseY)); //.add means run the add method for arraylists
}

and this is my class:

public class planets {
  //all the code in here defines the planets

  //first, declare any class variables
  int rad;        // Width of the shape
  float xpos, ypos;    // Starting position of shape    

  float xspeed;  // Speed of the shape
  float yspeed;  // Speed of the shape

  int xdirection;  // Left or Right
  int ydirection;  // Top to Bottom

  PImage earth; //this makes a PImage object

   //a constructor: code that runs when we make an object
  public planets() {
    earth = loadImage("earth.png"); //this image is in data folder
    rad = (int)random(100); //This is to randomize the size of the balls without the balls flickering
    xspeed = random(10); //random speed for balls
    yspeed = random(10);
    xdirection = 1;
    ydirection = 1;
    xpos = random(rad, 400-rad);
    ypos = random(rad, 200-rad);
  }

   //an overload constructor with width and height
  public planets(int w, int h) { //w=width, h=height
   earth = loadImage("earth.png"); //this image is in data folder
    rad = (int)random(100); //This is to randomize the size of the balls without the balls flickering
    xspeed = random(10); //random speed for balls
    yspeed = random(10);
    xdirection = 1;
    ydirection = 1;
    xpos = random(rad, w-rad);
    ypos = random(rad, h-rad);
  }

  //overload constructor
  public planets(int w, int h, int s) { //w=width, h=height
    earth = loadImage("earth.png"); //this image is in data folder
    rad = (int)random(100); //This is to randomize the size of the balls without the balls flickering
    xspeed = random(10); //random speed for balls
    yspeed = random(10);
    xdirection = 1;
    ydirection = 1;
    xpos = random(rad, w-rad);
    ypos = random(rad, h-rad);
  }

  //a overloaded constructor: takes 2 location values
  public planets(int xposition, int yposition, int w, int h) { //parameters
   earth = loadImage("earth.png"); //this image is in data folder
    rad = (int)random(100); //This is to randomize the size of the balls without the balls flickering
    xspeed = random(10); //random speed for balls
    yspeed = random(10);

    float change = random(-100, 100);
    if (change < 0) {

      xdirection = -1;
    } else {
      xdirection = 1;
    }

    change = random(-100, 100);
    if (change < 0) {

      ydirection = -1;
    } else {
      ydirection = 1;
    }

    xpos = xposition; //object will start in the parameter location
    ypos = yposition;
  }

  //methods to control the Ball objects

  public void move() {
    // Update the position of the shape
    xpos = xpos + ( xspeed * xdirection );
    ypos = ypos + ( yspeed * ydirection );
    bounce();
  }

  public void bounce() {

    // Test to see if the shape exceeds the boundaries of the screen
    // If it does, reverse its direction by multiplying by -1
    if (xpos > width-rad || xpos < rad) {
      xdirection *= -1;
    }
    if (ypos > height-rad || ypos < rad) {
      ydirection *= -1;
    }
  }

  public void display() {
    image(earth, xpos, ypos, 90, 90);
  }

  //METHOD


  //this method checks if the ball object hits another one
  public boolean checkCollide(planets other) {
    if ( dist(xpos, ypos, other.xpos, other.ypos) <= rad + other.rad) {
      println("bounce");
      return true;
    } else {
      return false;
    }
  }

  public int getRad() {
    return rad; //this will send the NUMBER back
  }

  //a "setter" to change the size of the ball
  public void setRad(float newSize ) {
    rad = (int) newSize;
  }
}
Tagged:

Answers

  • Can we see the sketch, the image, and a mock-up picture of what you'e trying to achieve? It's basically impossible to help you otherwise.

  • as an aside, you can call construtors from constructors

    so

       //a constructor: code that runs when we make an object
      public planets() {
        earth = loadImage("earth.png"); //this image is in data folder
        rad = (int)random(100); //This is to randomize the size of the balls without the balls flickering
        xspeed = random(10); //random speed for balls
        yspeed = random(10);
        xdirection = 1;
        ydirection = 1;
        xpos = random(rad, 400-rad);
        ypos = random(rad, 200-rad);
      }
    
       //an overload constructor with width and height
      public planets(int w, int h) { //w=width, h=height
       earth = loadImage("earth.png"); //this image is in data folder
        rad = (int)random(100); //This is to randomize the size of the balls without the balls flickering
        xspeed = random(10); //random speed for balls
        yspeed = random(10);
        xdirection = 1;
        ydirection = 1;
        xpos = random(rad, w-rad);
        ypos = random(rad, h-rad);
      }
    

    can you can replace the first with

      public planets() {
        this(400, 200); // default values for w and h
      }
    

    (your third doesn't use s and is identical to the second)

Sign In or Register to comment.