Shapes keep going off screen before bouncing off the side of the window

I’m not sure where my code is going wrong, but I wanted the shapes (circles and stars) to bounce off all sides when they get there, rather than go off screen and then bounce back… I’ve been sitting here thinking about how to fix my code so that collision detection would work properly but the collision detection works for 2/4 sides (top and left work, but bottom and right go off screen and then bounce….). Below is the code that I am currently using for this trial of mines, hopefully someone will be able to assist in finding the problem is resolving it…

import java.util.Random; import java.awt.Color; import java.awt.Graphics; import javax.swing.JFrame; public class TestGraphics extends JFrame {     static int width = 800, height = 800, angle = 90;     static int R, G, B;     static int X1, X2, X3, X4, X5, X6, Y1, Y2, Y3, Y4, Y5, Y6;     static int Zoom1, Zoom2, Zoom3, Zoom4, Zoom5, Zoom6;     static int BallSize1, BallSize2, BallSize3, StarSize1, StarSize2, StarSize3;     static int SpeedX1, SpeedX2, SpeedX3, SpeedX4, SpeedX5, SpeedX6;     static int SpeedY1, SpeedY2, SpeedY3, SpeedY4, SpeedY5, SpeedY6;     static double rad, pi = 3.14159;     static Color Color1, Color2, Color3, Color4, Color5, Color6;     public TestGraphics(){         super("CS210 Assignment 4");         setBounds(600, 200, width, height);  // at your monitor         setResizable(false);         setVisible(true);         setDefaultCloseOperation(EXIT_ON_CLOSE);     }     public static void Ball1(){         Random rand = new Random();         R = rand.nextInt(256);         G = rand.nextInt(256);         B = rand.nextInt(256);         Color1 = new Color(R, G, B);         X1 = rand.nextInt(width/5);         Y1 = rand.nextInt(height/2);         BallSize1 = rand.nextInt(50)+40;         SpeedX1 = rand.nextInt(10)+1;         SpeedY1 = rand.nextInt(10)+1;         Zoom1 = rand.nextInt(3)+1;     }     public static void Ball2(){         Random rand = new Random();         R = rand.nextInt(256);         G = rand.nextInt(256);         B = rand.nextInt(256);         Color2 = new Color(R, G, B);         X2 = rand.nextInt(width/5);         Y2 = rand.nextInt(height/2);         BallSize2 = rand.nextInt(50)+40;         SpeedX2 = rand.nextInt(10)+1;         SpeedY2 = rand.nextInt(10)+1;         Zoom2 = rand.nextInt(3)+1;     }     public static void Ball3(){         Random rand = new Random();         R = rand.nextInt(256);         G = rand.nextInt(256);         B = rand.nextInt(256);         Color3 = new Color(R, G, B);         X3 = rand.nextInt(width/5);         Y3 = rand.nextInt(height/2);         BallSize3 = rand.nextInt(50)+40;         SpeedX3 = rand.nextInt(10)+1;         SpeedY3 = rand.nextInt(10)+1;         Zoom3 = rand.nextInt(3)+1;     }     public static void StarOne(){         Random rand = new Random();         R = rand.nextInt(256);         G = rand.nextInt(256);         B = rand.nextInt(256);         Color4 = new Color(R, G, B);         X4 = rand.nextInt(width/5);         Y4 = rand.nextInt(height/2);         StarSize1 = rand.nextInt(100);         SpeedX4 = rand.nextInt(10)+1;         SpeedY4 = rand.nextInt(10)+1;         Zoom4 = rand.nextInt(3)+1;     }     public static void StarTwo(){         Random rand = new Random();         R = rand.nextInt(256);         G = rand.nextInt(256);         B = rand.nextInt(256);         Color5 = new Color(R, G, B);         X5 = rand.nextInt(width/5);         Y5 = rand.nextInt(height/2);         StarSize2 = rand.nextInt(100);         SpeedX5 = rand.nextInt(10)+1;         SpeedY5 = rand.nextInt(10)+1;         Zoom5 = rand.nextInt(3)+1;     }     public static void StarThree(){         Random rand = new Random();         R = rand.nextInt(256);         G = rand.nextInt(256);         B = rand.nextInt(256);         Color6 = new Color(R, G, B);         X6 = rand.nextInt(width/5);         Y6 = rand.nextInt(height/2);         StarSize3 = rand.nextInt(100);         SpeedX6 = rand.nextInt(10)+1;         SpeedY6 = rand.nextInt(10)+1;         Zoom6 = rand.nextInt(3)+1;     }     public void paint(Graphics g) {         g.setColor(Color.BLACK);         g.fillRect(0, 0, getWidth(), getHeight());         DrawBallOne(g);         DrawBallTwo(g);         DrawBallThree(g);         DrawStarOne(g);         DrawStarTwo(g);         DrawStarThree(g);         try{             Thread.sleep(50);         } catch (Exception exc){}         repaint();     }     public void DrawBallOne(Graphics g){         g.setColor(Color1);         X1+=SpeedX1;         Y1+=SpeedY1;         BallSize1+=Zoom1;         g.fillOval(X1, Y1, BallSize1, BallSize1);         if (X1 <0 || X1 > width){             SpeedX1*=-1;         }         if (Y1 < 0 || Y1 > height){             SpeedY1*=-1;         }         if (BallSize1 < 0 || BallSize1 > 100){             Zoom1*=-1;         }     }     public void DrawBallTwo(Graphics g){         g.setColor(Color2);         X2+=SpeedX2;         Y2+=SpeedY2;         BallSize2+=Zoom2;         g.fillOval(X2, Y2, BallSize2, BallSize2);         if (X2>width || X2 <0){             SpeedX2*=-1;         }         if (Y2>height || Y2<0){             SpeedY2*=-1;         }         if (BallSize2 > 100 || BallSize2 < 0){             Zoom2*=-1;         }     }     public void DrawBallThree(Graphics g){         g.setColor(Color3);         X3+=SpeedX3;         Y3+=SpeedY3;         BallSize3+=Zoom3;         g.fillOval(X3, Y3, BallSize3, BallSize3);         if (X3>width || X3 <0){             SpeedX3*=-1;         }         if (Y3>height || Y2<0){             SpeedY3*=-1;         }         if (BallSize3 > 100 || BallSize3 < 0){             Zoom3*=-1;         }     }     public void DrawStarOne(Graphics g){         int [] Sx = new int [10];         int [] Sy = new int [10];         for(int i = 0; i<10; i++){             if (i % 2 == 0) {                 rad = StarSize1*.38;             } else {                 rad = StarSize1;             }             Sx[i] += (int) (rad * Math.cos(pi * angle/180));             Sy[i] += (int) (rad * Math.sin(pi * angle/180));             angle += 36;             Sx[i] += X4;             Sy[i] += Y4;         }         g.setColor(Color4);         X4+=SpeedX4;         Y4+=SpeedY4;         StarSize1+=Zoom4;         g.fillPolygon(Sx, Sy, 10);         if (X4 > width || X4 < 0) {             SpeedX4*=-1;         }         if (Y4 > height || Y4 < 0) {             SpeedY4*=-1;         }         if (StarSize1 > 100 || StarSize1 < 0) {             Zoom4 *= -1;         }     }     public void DrawStarTwo(Graphics g){         int [] Sx = new int [10];         int [] Sy = new int [10];         for(int i = 0; i<10; i++){             if (i % 2 == 0) {                 rad = StarSize2*.38;             } else {                 rad = StarSize2;             }             Sx[i] += (int) (rad * Math.cos(pi * angle/180));             Sy[i] += (int) (rad * Math.sin(pi * angle/180));             angle += 36;             Sx[i] += X5;             Sy[i] += Y5;         }         g.setColor(Color5);         X5+=SpeedX5;         Y5+=SpeedY5;         StarSize2+=Zoom5;         g.fillPolygon(Sx, Sy, 10);         if (X5 > width || X5 < 0) {             SpeedX5*=-1;         }         if (Y5 > height || Y5 < 0) {             SpeedY5*=-1;         }         if (StarSize2 > 100 || StarSize2 < 0) {             Zoom5 *= -1;         }     }     public void DrawStarThree(Graphics g){         int [] Sx = new int [10];         int [] Sy = new int [10];         for(int i = 0; i<10; i++){             if (i % 2 == 0) {                 rad = StarSize3*.38;             } else {                 rad = StarSize3;             }             Sx[i] += (int) (rad * Math.cos(pi * angle/180));             Sy[i] += (int) (rad * Math.sin(pi * angle/180));             angle += 36;             Sx[i] += X6;             Sy[i] += Y6;         }         g.setColor(Color6);         X6+=SpeedX6;         Y6+=SpeedY6;         StarSize3+=Zoom6;         g.fillPolygon(Sx, Sy, 10);         if (X6 > width || X6 < 0) {             SpeedX6*=-1;         }         if (Y6 > height || Y6 < 0) {             SpeedY6*=-1;         }         if (StarSize3 > 100 || StarSize3 < 0) {             Zoom6 *= -1;         }     }     public static void main(String[] args){         Ball1();         Ball2();         Ball3();         StarOne();         StarTwo();         StarThree();         new TestGraphics();     } }