I have an error when I add in another ball. The colors are the same how would it be possible to change the colors of the balls seperately when they are on the same screen. When I added the other ball in when one hit the edge the second one changed color aswell. Another thing: How would I change the Background color?

// The "ScreenSaver" class.
import java.awt.*;
import hsa.Console;

public class ScreenSaver
{
    static Console c;           // The output console

    public static void main (String[] args)
    {
        c = new Console ();

        int a = 1;

        int x, y, dx, dy;

        x = 1;
        y = 1;
        double rand;
        c.print ("How many pixels do you want the ball to move sideways? (int only)");
        dx = c.readInt ();
        c.print ("How many pixels do you want the ball to move up and down? (int only)");
        dy = c.readInt ();

        while (a == 1)
        {
            c.clear ();
            
            
            c.fillOval (x, y, 40, 40);

            if ((x + dx < 0) || (x + dx + 40 > c.getWidth ()))
            {
                dx = -dx;
                rand = Math.random();
                if (rand > .9){
                c.setColor (Color.red);
                }
                else if (rand > .8){
                c.setColor (Color.blue);
                }
                else if (rand > .7){
                c.setColor (Color.green);
                }
                else if (rand > .6){
                c.setColor (Color.magenta);
                }
                else if (rand > .5){
                c.setColor (Color.pink);
                }
                else if (rand > .4){
                c.setColor (Color.orange);
                }
                else if (rand > .3){
                c.setColor (Color.cyan);
                }
                else if (rand > .2){
                c.setColor (Color.yellow);
                }
                else if (rand > .1){
                c.setColor (Color.gray);
                }
                else {
                c.setColor (Color.black);
                }
                
                
                
                
            }
            if ((y + dy < 0) || (y + dy + 40 > c.getHeight ()))
            {
                dy = -dy;
                
                rand = Math.random();
                if (rand > .9){
                c.setColor (Color.red);
                }
                else if (rand > .8){
                c.setColor (Color.blue);
                }
                else if (rand > .7){
                c.setColor (Color.green);
                }
                else if (rand > .6){
                c.setColor (Color.magenta);
                }
                else if (rand > .5){
                c.setColor (Color.pink);
                }
                else if (rand > .4){
                c.setColor (Color.orange);
                }
                else if (rand > .3){
                c.setColor (Color.cyan);
                }
                else if (rand > .2){
                c.setColor (Color.yellow);
                }
                else if (rand > .1){
                c.setColor (Color.gray);
                }
                else {
                c.setColor (Color.black);
                }
            }

            for (int i = 0 ; i < 5000000 ; i++)
                ;
            c.clear ();

            x = x + dx;
            y = y + dy;

        }


        // Place your program here.  'c' is the output console
    } // main method
} // ScreenSaver class

Thanks for the help.

Not entirely sure about the background because you are using some third-party Console class that isn't part of the JDK. Check your API docs on Console.

As far as the balls though, set the color before you draw each one. Set color, draw shape, set color, draw shape... Your code above is setting the color twice before a single draw operation.

Edited 6 Years Ago by Ezzaral: n/a

but that would mean that the colors wouldn't change when they hit the sides. The drawing would be in the if statements but the if statements only work when the ball hits the corner.

Set a color, however you want, then draw the shape. You can set a new color and draw another shape if you wish. The code you posted just draws one shape - there is no second ball.

You really should use a small class for the ball that contains it's coordinates and color. Store them in a List and loop through that list when you need to draw them.

This article has been dead for over six months. Start a new discussion instead.