Back Lines
in
Programming Questions
•
8 months ago
Hi,
I am trying to make lines that connect shapes to be in the background but I am probably missing something.
Any help?
int numBalls = 14;
int[] drivingArray = new int[numBalls];
Ball[] balls = new Ball[numBalls];
//for (int i = 0; i < myArray.length; i++){
void setup()
{
size(displayWidth - 20, 300);
frame.setBackground(new java.awt.Color(0, 0, 0));
for (int d = 0; d < numBalls; d++) {
drivingArray[d] = int(random(0, width));
}
drivingArray = sort(drivingArray);
for (int i = 0; i < numBalls; i++) {
balls[i] = new Ball(drivingArray[i], random(height), random(5, 55), random(75, 150), random(50, 200), random(100, 255), 255);
}
}
void draw()
{
background(0);
stroke(200);
line(0, 0, width, 0);
line(0, height-1, width, height-1);
for (int i = 0; i < numBalls; i++) {
balls[i].connect(i);
balls[i].display();
balls[i].move();
}
}
class Ball {
float x, y, red, green, blue, alpha;
float diameter;
float dy = 0.63;
float dx = 0.63;
Ball(float temp_x, float temp_y, float temp_d, float temp_red, float temp_green, float temp_blue, float temp_alpha) {
x = temp_x;
y = temp_y;
diameter = temp_d;
red = temp_red;
green = temp_green;
blue = temp_blue;
alpha = temp_alpha;
}
void move() {
y += dy * map(mouseY, 0, height, 0, 1);
if (y + diameter/2 > height) {
y = height - diameter/2;
dy *= -1;
}
else if (y - diameter/2 < 0) {
y = diameter/2;
dy *= -1;
}
x += dx * map(mouseX, 0, height, 0, 1);
if (x + diameter/2 > width) {
x = width - diameter/2;
dx *= -1;
}
else if (x - diameter/2 < 0) {
x = diameter/2;
dx *= -1;
}
}
void connect(int i) {
strokeWeight(1.5);
stroke(255, 200);
if (i == 0) {
line(balls[i].x, balls[i].y, 0, height);
}
else if (i == numBalls-1) {
line (balls[i].x, balls[i].y, width, height);
line(balls[i].x, balls[i].y, balls[i-1].x, balls[i-1].y);
}
else if (i > 0) {
line(balls[i-1].x, balls[i-1].y, balls[i].x, balls[i].y);
}
}
void display() {
fill(red, green, blue, alpha);
if (y < height/2) {
ellipse(x, y, diameter, diameter);
}
else if (y >= height/2) {
rectMode(CENTER);
rect(x, y, diameter, diameter);
}
}
}
1