colors - Java Bouncing ball app with random colour -


i have been handed task of making ball bounce in program have created. trying focus on more advanced features, second ball (which have done successfully) , making ball change random colour each time bounces.

i quite new java, , coding in general, have managed make both both change random colour first time hit wall. however, cannot seem make change different colour each time bounces. have suggestions how accomplish this?

import java.awt.color; import java.awt.graphics; import java.util.random;   public class mainclass extends javax.swing.jframe implements runnable { int width ; int height ; int ball; int ball2; int posx; int posy; int posx2; int posy2; int dx = 8; int dy = 8;//dx dy varuiables movement of ball int dx2 = -8; int dy2 = -8; random rand = new random(); float r = rand.nextfloat(); float g = rand.nextfloat(); float b = rand.nextfloat(); float r2 = rand.nextfloat(); float g2 = rand.nextfloat(); float b2 = rand.nextfloat(); color randomcolor = new color(r, g, b); color randomcolor2 = new color(r2,g2,b2); color c1 = color.blue; color c2 = color.green; thread move = new thread (this);   public void paint(graphics g) {     g.setcolor(color.black);     g.fillrect(0,0,width,height);     g.setcolor(c1);     g.filloval(posx ,posy ,ball,ball);      g.setcolor(c2);     g.filloval(posx2,posy2, ball2,ball2);      // sets starting position of oval(ball)   }    public void run(){   try {         while (true){           posx=posx+dx;           posy=posy+dy;           posx2=posx2+dx2;           posy2=posy2+dy2;           //this speed of ball           repaint();            move.sleep(100);             //this shows ball on screen                 if (posy > height - ball)                 {                     dy = dy *-1;                     c1 = randomcolor;                   }                 if ( posy <0 - ball)                 {                     dy = dy *-1;                     c1 = randomcolor;                   }                 if(posx > width - ball)                 {                     c1 = randomcolor;                     dx = dx*-1;                  }                 if(posx <0 - ball )                 {                     c1 = randomcolor;                     dx = dx*-1;                  }                 // code first ball                 if(posx > width - ball2 || posx <0 - ball2)                 {                     dx2 = dx2*-1;                     c2 = randomcolor2;                 }                 if(posy > height - ball2 || posy <0 - ball2)                 {                     dy2 = dy2 *-1;                     c2 = randomcolor2;                 }       }     }   catch(exception ex) {       ex.printstacktrace();    } }  public mainclass() {     initcomponents();     this.setresizable(false);     width = getwidth();     height = getheight();     //this code creates program dimensions     posx = width/2;     posy = height/2;     posx2 = width/2;     posy2 = height/2;     ball = 20;     ball2= 20; 

change color of randomcolor each time after set next time set different.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -