Bouncing ball

import javax.swing.*;

import java.awt.*;

import java.util.Random;


public class BouncingBalls extends JFrame {


    private static final int WIDTH = 800;

    private static final int HEIGHT = 600;

    private static final int BALL_SIZE = 20;

    private static final Color[] COLORS = {Color.RED, Color.BLUE, Color.GREEN, Color.ORANGE, Color.MAGENTA};


    private JPanel canvas;


    public BouncingBalls() {

        setTitle("Bouncing Balls");

        setSize(WIDTH, HEIGHT);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        canvas = new JPanel() {

            @Override

            protected void paintComponent(Graphics g) {

                super.paintComponent(g);

                for (Ball ball : balls) {

                    ball.draw(g);

                }

            }

        };


        getContentPane().add(canvas);

    }


    private static class Ball implements Runnable {

        private int x;

        private int y;

        private int dx;

        private int dy;

        private Color color;


        public Ball(int x, int y, int dx, int dy, Color color) {

            this.x = x;

            this.y = y;

            this.dx = dx;

            this.dy = dy;

            this.color = color;

        }


        public void draw(Graphics g) {

            g.setColor(color);

            g.fillOval(x, y, BALL_SIZE, BALL_SIZE);

        }


        @Override

        public void run() {

            while (true) {

                move();

                try {

                    Thread.sleep(30);

                } catch (InterruptedException e) {

                    e.printStackTrace();

                }

            }

        }


        private void move() {

            x += dx;

            y += dy;


            if (x <= 0 || x >= WIDTH - BALL_SIZE) {

                dx = -dx;

            }

            if (y <= 0 || y >= HEIGHT - BALL_SIZE) {

                dy = -dy;

            }

        }

    }


    private Ball[] balls;


    public void startAnimation() {

        balls = new Ball[5];

        Random random = new Random();


        for (int i = 0; i < balls.length; i++) {

            int x = random.nextInt(WIDTH - BALL_SIZE);

            int y = random.nextInt(HEIGHT - BALL_SIZE);

            int dx = random.nextInt(5) + 1;

            int dy = random.nextInt(5) + 1;

            Color color = COLORS[i % COLORS.length];

            balls[i] = new Ball(x, y, dx, dy, color);

            Thread thread = new Thread(balls[i]);

            thread.start();

        }


        while (true) {

            try {

                Thread.sleep(30);

            } catch (InterruptedException e) {

                e.printStackTrace();

            }

            canvas.repaint();

        }

    }


    public static void main(String[] args) {

        SwingUtilities.invokeLater(() -> {

            BouncingBalls frame = new BouncingBalls();

            frame.setVisible(true);

            frame.startAnimation();

        });

    }

}


Comments

Popular posts from this blog

JColorchooser

Swing aplicsn click