Jump to content
  • Advertisement
Sign in to follow this  
Moen

Help with understanding "pong" code

This topic is 927 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:

JContentPane

Panel

This

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() {

super();

initialize();

// Listeners for the keyboard

this.addKeyListener(new KeyAdapter() {

//Method for the key pressed

public void keyPressed(KeyEvent evt) {

formKeyPressed(evt);

}

// Method for the key released

public void keyReleased(KeyEvent evt) {

formKeyReleased(evt);

}

});



}



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

private void formKeyPressed(KeyEvent evt)

{

panel.keyPressed(evt);

}



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

private void formKeyReleased(KeyEvent evt)

{

panel.keyReleased(evt);

}



/**

* This method initializes this

*

* @return void

*/

private void initialize() {

this.setResizable(false);

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.setContentPane(getJContentPane());

this.setTitle("Pong");

}



/**

* 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();

thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

thisClass.setVisible(true);

}

});

}

}

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

link to the original code:

http://forum.codecall.net/topic/45693-game-a-simple-pong/

Edited by Moen

Share this post


Link to post
Share on other sites
Advertisement

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!