Textures Help
in
Programming Questions
•
4 months ago
So in this program, you make spheres with the click of a button. Im trying to add a texture to each sphere that I make, but nothing I try seems to work, even if I put it in the class.
Here is the texture Im trying to use. Here is the original code:
- //SPHRE PROGRAM MADE BY MILES JORDAN
- // This program is used to creat sphere objects that drop and bounce on the floor.
- // The lighting of the spheres change color when you press ENTER (RETURN if you are on a Mac).
- // You must make a ball with the mouse to change the color fist, so that the computer keyboard may recognise you are on the program.
- // The green, directional lights slightly change according to where the cursor is.
- // coden4602@gmail.com
- // UPDATE
- // The program now can increase the size of the spheres
- // Pressing the R key can now generate spheres at random areas
- // Spheres now dissapear after 7 seconds
- // A string is used to make several spheres appear in certain coordinates
- ArrayList<Ball> balls = new ArrayList<Ball>();
- float gravity = 0.1;
- //arrays are used to store objects, and an array list can be used to make multiple objects, as it can increase better then a normal array
- float lightvalue = 0;
- //the light value will be used to change the light type, 0 for regular lights function, 1 for directional, 2 for ambient, 3 for spot light. To change the light, press Enter
- float ballsize = 10;
- //the size of the ball will increase from 1-0
- float rX;
- float rY;
- // two variables specifying random values in the x and y value. rX is x value and rY is y value
- PFont f;
- //String[] lines = loadStrings("balls.csv");
- void setup() {
- size(1500, 700, P3D);
- // start with one ball
- balls.add( new Ball(width/2, 0, 0)); //
- background(0);
- frameRate(60);
- f = createFont("Arial",16,true); // Arial, 16 point, anti-aliasing on
- //String[] lines = loadStrings("balls.csv");
- // for ( int i = 0; i < lines.length; i++ ) {
- // String[] coordinates = trim( split( lines[i], ',' ) );
- // float x = float( coordinates[ 0 ] );
- // float y = float( coordinates[ 1 ] );
- // balls.add( new Ball( x, y, 0 ) );
- // }
- }
- //The key pressing here is used to try and change the the lightvalue, to later change the lights
- void keyPressed() {
- if (key == ENTER || key == RETURN) {
- lightvalue = lightvalue + 1;
- lightvalue %= 4; // 0 1 2 3 0 1 2 ...
- }
- if (key >= '0' && key <= '9') {
- ballsize = (int(key) - int('0')) * 10;
- if (ballsize == 0) ballsize = 100;
- }
- if (key == 'r' || key == 'R' ) {
- rX = random(width);
- rY = random(height);
- balls.add( new Ball(rX, rY, 0));
- }
- }
- void draw() {
- background(0);
- textFont(f,24);
- text ( "Welcome to the Sphere Simulator!" ,0,24);
- text ( "Click on the screen to generate a sphere!" ,0,48);
- text ( "Press the keys 1 through 0 to increase the size!" ,0,72);
- text ( "Press the R key to generate a sphere randomly!" ,0,96);
- text ( "Press Enter/Return to change the lights!" ,0,120);
- //println(mouseY);
- if (lightvalue == 0) {
- lights();
- }
- if (lightvalue == 1) {
- ambientLight(0,0,255);
- }
- if (lightvalue == 2) {
- directionalLight(0, 255, 0, mouseX, mouseY, 0);
- }
- if (lightvalue == 3) {
- spotLight(255, 0, 0, width/2, height/2, 400, 0, 0, -1, PI/2, 2);
- }
- // Ball is the datatypye of the stuff in the Array, b is the temporary name for each element in the array, balls is the name of the array to iterate through
- for( int i=balls.size()-1; i>=0; i--){ //update() and dislay() are methods, in which b is using, and the dot "." connects the two, allowing b to use the two methods
- balls.get(i).update(); //update is used to improve the falling of the balls
- balls.get(i).display(); //display is used to make the balls, constantly
- if( balls.get(i).isDead() ){
- balls.remove(i);
- }
- }
- }
- void mousePressed() {
- balls.add(new Ball( mouseX, mouseY, 0));
- //this adds the object
- }
- class Ball {
- float x;
- float y;
- float z;
- float speed;
- int t;
- // a constructor:
- Ball(float _x, float _y, float _z) {
- x = _x;
- y = _y;
- z = _z;
- speed = 0;
- t = millis() + 7000;
- }
- void display() {
- noStroke();
- pushMatrix();
- translate(x, y, z);
- sphere(ballsize);
- popMatrix();
- }
- void update() {
- y = y + speed;
- speed = speed + gravity;
- if (y > height) {
- speed = speed * -0.65;
- }
- }
- boolean isDead(){
- return( millis() > t );
- }
- }
1