Jump to content
  • Advertisement
Sign in to follow this  

Help with understanding "pong" code

This topic is 1012 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hello, i am new to programming, but i have made my own quiz program where people can make their own questions, answers and alternatives and the start the guiz.

but now i want to move on to understand more complex game code, but i want to start small, and i have heard a "pong" code is the perfect approach, but i searched a little on the web, and found little of turorials to help me, but i fund something, but the problem is, i cant understand jpanel, and that sort of stuff, i can list the things that needs explaining to me here, and then i will post the code afterwards:




actually, almost everything need explaining and i would highly appreciate if you would help me smile.png, thx in advance


here is the code:

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class Main extends JFrame {

private static final long serialVersionUID = 1L; // Eclipse added this automatically

private JPanel jContentPane = null;

private PanelPelota panel = null; // This is the panel of the game class

private PanelPelota getPanel() {

if (panel == null) {

panel = new PanelPelota(); // The panel is created


return panel;



* This is the default constructor


public Main() {



// Listeners for the keyboard

this.addKeyListener(new KeyAdapter() {

//Method for the key pressed

public void keyPressed(KeyEvent evt) {



// Method for the key released

public void keyReleased(KeyEvent evt) {





// Here i'm stating the method that will send the key pressed to the game class

private void formKeyPressed(KeyEvent evt)




// Here i'm stating the method that will send the key released to the game class

private void formKeyReleased(KeyEvent evt)





* This method initializes this


* @return void


private void initialize() {


this.setBounds(new Rectangle(312, 184, 250, 250)); // Position on the desktop

this.setMinimumSize(new Dimension(250, 250));

this.setMaximumSize(new Dimension(250, 250));





* This method initializes jContentPane


* @return javax.swing.JPanel


private JPanel getJContentPane() {

if (jContentPane == null) {

jContentPane = new JPanel();

jContentPane.setLayout(new BorderLayout());

jContentPane.add(getPanel(), BorderLayout.CENTER);


return jContentPane;


public static void main(String[] args) {

// TODO Auto-generated method stub

SwingUtilities.invokeLater(new Runnable() {

public void run() {

Main thisClass = new Main();







i am sorry, the "code" thing wint add syntax highightning, but i hope thats okay. 

link to the original code:


Edited by Moen

Share this post

Link to post
Share on other sites

The JFrame and JPanel classes are part of java's Swing API. Swing is a library meant to assist you in creating windows. Swing is an 'updated' version of java's older awt library (imo awt components looks way better... but swing has more features that awt doesn't).


The JFrame class is for creating windows. By default it will just show a blank window if you just have the JFrame set up. Look at attached <b>"jframe.png"</b> for am example.


The JPanel class is for populating JFrame objects. You can add JPanel objects to JFrame objects via [JFrame reference variable].add([JPanel reference variable]). You can also use .setContentPane() which is what is happening at line 123 of your code. The JPanel class is important because it allows you to use the Java2D library for drawing stuff for your game by overriding the paintComponent() method in the JPanel object. See attached <b>"jpanel override paint method.png"</b> for an example.


For more information I recommend googling "Java Swing tutorial". The tutorials there will also show you how to add components, which are things like buttons, text fields, and other incredibly useful things that you can use as like a menu or something. But remember that those components rely on the paintComponent() method so by overriding them to draw your game will make them dissappear unless you call super.paintComponent() after you draw your game in the paintComponent() method.


Do not worry about the "this." keyword. It is redundant in most cases. You can even leave it out if you want to

Edited by draika

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!