Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Pinball Game in Java
Next article icon

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 Explanation

  • Pinball 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.

Next Article
Pinball Game in Java

S

subramanyasmgm
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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences