Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8965353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:47:07+00:00 2026-06-15T16:47:07+00:00

I’m trying to do a Java Pong game. I have very little experience with

  • 0

I’m trying to do a Java Pong game. I have very little experience with Java and need some help. I’ve read a lot of stuff online, and below is the code I’ve come up with.
I’ve created the following classes, but all I get is a grey screen. I’m just trying to implement the simplest solution for this. Could anyone point out any issues this code has? Thank you.

    package pong_test_1;

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;

    /**
     *
     * @author jtrinidad
     */

    public class main_class extends JFrame implements KeyListener{
        static final int FRAME_WIDTH = 800;
        static final int FRAME_HEIGHT = 500;
        JFrame f;

        public main_class()
        {
            super();
            addKeyListener (this);
            setFocusable (true);

            f = new JFrame("Pong");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            f.setResizable(false);

            f.setSize(FRAME_WIDTH,FRAME_HEIGHT);
            f.setVisible(true);

        }

        @Override
         public void keyTyped(KeyEvent e) {
        }
        @Override
        public void keyPressed(KeyEvent e) {
            Pong_Test_1 p = new Pong_Test_1();
            int keyCode = e.getKeyCode();
            switch( keyCode ) { 
                case KeyEvent.VK_UP:
                    Pong_Test_1.p2_pos_y--; 
                    break;
                case KeyEvent.VK_DOWN:
                    Pong_Test_1.p2_pos_y++; 
                    break;
                case KeyEvent.VK_Q:
                    Pong_Test_1.p1_pos_y++; 
                    break;
                case KeyEvent.VK_A :
                    Pong_Test_1.p1_pos_y++; 
                    break;
            }
            add(p);
            repaint();
        }

        /** Handle the key released event from the text field. */
         @Override
        public void keyReleased(KeyEvent e) {

        }

         public static void main(String[] args) {
            // TODO code application logic here
             main_class c = new main_class();
        }


    }

and

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package pong_test_1;

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;



    /**
     *
     * @author jtrinidad
     */
    public class Pong_Test_1 extends JPanel implements ActionListener{

        /**
         * @param args the command line arguments
         */
        //Sizes of each object
        static final int WIDTH = 10;
        static final int HEIGHT = 120;
        static final int RADIUS = 10;
        //Position of Player 1's paddle
        static int p1_pos_x = 10;
        static int p1_pos_y = main_class.FRAME_HEIGHT/2;
        //Position of Player 2's paddle
        static int p2_pos_x = main_class.FRAME_WIDTH - 20;
        static int p2_pos_y = main_class.FRAME_HEIGHT/2;;
        //Position of the ball
        static int ball_pos_x;
        static int ball_pos_y;


        Timer animator;

        public Pong_Test_1()
        {
            animator = new Timer (10, this);
            animator.start();
        }


        public void paintComponent(Graphics g){
            super.paintComponent(g);
            this.setBackground(Color.BLACK); //Sets background color

            g.setColor(Color.WHITE);
            g.fillRect(p1_pos_x, p1_pos_y, WIDTH, HEIGHT);
            g.fillRect(p2_pos_x, p2_pos_y, WIDTH, HEIGHT);
            g.fillOval(100, 100, RADIUS, RADIUS);
        }

        public void actionPerformed( ActionEvent e ) {
            repaint();
            revalidate();  // new line
        }

        private void addKeyListener(Pong_Test_1 aThis) {
            throw new UnsupportedOperationException("Not yet implemented");
        }


    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T16:47:08+00:00Added an answer on June 15, 2026 at 4:47 pm

    See if this SSCCE works more to your expectations, the main problem was both extending and having an instance of a JFrame.

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    
    public class main_class implements KeyListener {
    
        static final int FRAME_WIDTH = 800;
        static final int FRAME_HEIGHT = 500;
        JFrame f;
    
        public main_class() {
            f = new JFrame("Pong");
    
            JPanel gui = new JPanel();
            gui.setFocusable(true);
            gui.addKeyListener(this);
            f.setContentPane(gui);
    
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            f.setResizable(false);
    
            f.setSize(FRAME_WIDTH, FRAME_HEIGHT);
            f.setVisible(true);
        }
    
        @Override
        public void keyTyped(KeyEvent e) {
        }
    
        @Override
        public void keyPressed(KeyEvent e) {
            // this will steal the focus for KeyEvents, or rather..
            // Pong_Test_1 p = new Pong_Test_1();
            int keyCode = e.getKeyCode();
            System.out.println(e);
            switch (keyCode) {
                case KeyEvent.VK_UP:
                    Pong_Test_1.p2_pos_y--;
                    break;
                case KeyEvent.VK_DOWN:
                    Pong_Test_1.p2_pos_y++;
                    break;
                case KeyEvent.VK_Q:
                    Pong_Test_1.p1_pos_y++;
                    break;
                case KeyEvent.VK_A:
                    Pong_Test_1.p1_pos_y++;
                    break;
            }
            // ..adding it will steal the content area & focus
            //add(p);
            f.repaint();
        }
    
        /**
         * Handle the key released event from the text field.
         */
        @Override
        public void keyReleased(KeyEvent e) {
        }
    
        public static void main(String[] args) {
            // TODO code application logic here
            main_class c = new main_class();
        }
    }
    

    As general advice:

    • Don’t extend frame, simply keep a reference to one.
    • For Swing, use Key Bindings rather than a KeyListener.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have thousands of HTML files to process using Groovy/Java and I need to
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have been unable to fix a problem with Java Unicode and encoding. The
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.