Bouncing Ball
Hello friends, we are going to make a ball which bounce back whenever touches the window. Open your IntelliJ or any other IDE and create a new Project. Now, name this project whatever you likes and go to Main.java file.
Now, write the following code in Main.java file.
Main.java :
import javax.swing.*;
public class Main extends JPanel {
public static int height = 600;
public static int width = 600;
public static void main(String[] args) {
JFrame obj = new JFrame();
Ball ball = new Ball();
obj.setBounds(100,100,width,height);
obj.setTitle("Bouncing Ball");
obj.setResizable(false);
obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
obj.add(ball);
obj.setVisible(true);
}
}
public class Main extends JPanel {
public static int height = 600;
public static int width = 600;
public static void main(String[] args) {
JFrame obj = new JFrame();
Ball ball = new Ball();
obj.setBounds(100,100,width,height);
obj.setTitle("Bouncing Ball");
obj.setResizable(false);
obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
obj.add(ball);
obj.setVisible(true);
}
}
Now, go to Projects tab.
Right Click on src folder, go to New and Click on JAVA Class.
Name the new JAVA Class as Ball.java
Write the following code in Ball.java.
Ball.java :
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Ball extends JPanel {
public static int diameter = 50;
public int x = 250;
public int y = 250;
public int velocityX = 10;
public int velocityY = 10;
public int interval = 20;
public Timer timer;
public Ball() {
timer = new Timer(interval, new TimerAction());
timer.start();
}
class TimerAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
move();
repaint();
}
}
public void move() {
x += velocityX;
y += velocityY;
if (x < 0) {
x = 0;
velocityX = -velocityX;
}
else if (x > getWidth() - diameter) {
velocityX = -velocityX;
}
if (y < 0) {
y = 0;
velocityY = -velocityY;
}
else if (y > getHeight() - diameter) {
velocityY = -velocityY;
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
g.fillOval(x, y, diameter, diameter);
}
}
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Ball extends JPanel {
public static int diameter = 50;
public int x = 250;
public int y = 250;
public int velocityX = 10;
public int velocityY = 10;
public int interval = 20;
public Timer timer;
public Ball() {
timer = new Timer(interval, new TimerAction());
timer.start();
}
class TimerAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
move();
repaint();
}
}
public void move() {
x += velocityX;
y += velocityY;
if (x < 0) {
x = 0;
velocityX = -velocityX;
}
else if (x > getWidth() - diameter) {
velocityX = -velocityX;
}
if (y < 0) {
y = 0;
velocityY = -velocityY;
}
else if (y > getHeight() - diameter) {
velocityY = -velocityY;
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
g.fillOval(x, y, diameter, diameter);
}
}
Other JAVA projects :
Ball Game using JAVA
Notepad using JAVA
Chat Application using JAVA
Simple Clock using JAVA