Pinball Game in Java Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report Pinball is a classic arcade game that has been around since the 1930s. It involves the player using flippers to hit a ball around a table aiming to score points by hitting various targets and obstacles. A sample GIF is given below to get an idea about what we are going to do in this article. Implementation Java package abc; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class a extends JPanel implements ActionListener { private Timer timer; private int ballX, ballY; private int paddleX; private int dx, dy; public a() { setBackground(Color.BLACK); setPreferredSize(new Dimension(600, 400)); setFocusable(true); ballX = 200; ballY = 200; paddleX = 250; dx = 3; dy = 3; timer = new Timer(10, this); timer.start(); addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_LEFT) { paddleX -= 10; if (paddleX < 0) paddleX = 0; } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) { paddleX += 10; if (paddleX > getWidth() - 100) paddleX = getWidth() - 100; } } }); } public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.WHITE); g.fillOval(ballX, ballY, 20, 20); g.fillRect(paddleX, getHeight() - 20, 100, 10); } public void actionPerformed(ActionEvent e) { ballX += dx; ballY += dy; if (ballX < 0 || ballX > getWidth() - 20) dx = -dx; if (ballY < 0 || ballY > getHeight() - 20) dy = -dy; if (ballY > getHeight() - 30 && ballX >= paddleX && ballX <= paddleX + 100) dy = -dy; repaint(); } public static void main(String[] args) { JFrame frame = new JFrame("Pinball"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new a()); frame.pack(); frame.setVisible(true); } } Output Code ExplanationPinball class extends the JFrame class and implements the ActionListener interface which allows it to handle events.The timer object is created to update the game every 10 milliseconds.The ballX, ballY, and ballSize variables are used to keep track of the ball's position and size.The ballXdir and ballYdir variables are used to control the ball's direction and speed.The paddleX, paddleY, paddleWidth, and paddleHeight variables are used to keep track of the paddle's position and size.The Pinball constructor sets the size of the window sets the title of the game starts the timer adds a key listener to handle input, and sets the window to be visible.The paint method is called whenever the window needs to be redrawn. It draws the ball and paddle on the screen. Comment More infoAdvertise with us Next Article Pinball Game in Java S subramanyasmgm Follow Improve Article Tags : Java Java Programs Java Projects Practice Tags : Java Similar Reads Tic-Tac-Toe Game in Java Tic-Tac-Toe is a classic game that two people can enjoy together. It is played on a 3x3 grid where players take turns placing their marks, X or O, in empty spots. The main goal is to get three of the same marks in a row-horizontally, vertically, or diagonally.In this article, we are going to build a 7 min read Number Guessing Game in Java A number-guessing game in Java is a simple program where the computer randomly selects a number, and the user has to guess it within a limited number of attempts. The program provides feedback on whether the guessed number is too high or too low, guiding the user toward the correct answer.This proje 4 min read Hangman Game in Java Hangman is a popular word guessing game where the player endeavors to construct a lost word by speculating one letter at a time. After a certain number of off base surmises, the game finishes and the player loses. The game also finishes when the player accurately distinguishes all the letters of the 6 min read Moving Ball Using Thread in Java Prerequisites: Java.lang.Thread class in JavaMouseListener and MouseMotionListener in Java This is a simple Java code including the concept of Thread and Java AWT to implement three balls moving in a particular path in an AWT frame. In this implementation, three balls are taken in an AWT frame and h 3 min read Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its 8 min read Like