Pong Rendering and KeyListeners

Hello Everyone,

I have this project due tomorrow so any help on this would be greatly appreciated. I have a render method that shows the paddle and ball in a JPanel. The paddle and ball are JLabels that move around. I get an error the second time the render method is called. How can I prevent this hang up? I also am having trouble using a KeyListener for the paddle movement.

Thanks in Advance

Heres the Code

import java.util.*;

import java.text.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

publicclass workextends JFrame{

int [][] paddle1position ={{200},{10}};

int [][] paddle2position ={{200},{490}};

int ballx;

int bally;

finalint ballwidth=10;

finalint ballheight=10;

finalint tablexmax = 500;

finalint tableymax = 400;

int xvect = 1;

int yvect = 1;

finalint lpaddlex= 0;

finalint rpaddlex= 480;

int lpaddley= 170;

int rpaddley=170;

finalint paddlelength = 60;

finalint paddlewidth = 20;

//paddle maneuvers

int delta1;

int delta2;

JPanel viewer=new JPanel();

// array of jbuttons

public JButton[] keyJButtons =new JButton[KeyEvent.KEY_LAST+1];

public JButton player1upJButton=new JButton();

public JButton player1downJButton =new JButton();

public JButton player2upJButton=new JButton();

public JButton player2downJButton =new JButton();

public JLabel leftpaddle=new JLabel();

public JLabel rightpaddle =new JLabel();

public JLabel balllabel =new JLabel();

//new

public work()

{

createUserInterface();

}

privatevoid createUserInterface()

{

Container contentPane = getContentPane();

contentPane.setBackground(Color.GREEN);

contentPane.setLayout(null );

viewer=new JPanel();

viewer.setLayout(null);

viewer.setBounds (25, 25, 500, 400);

viewer.setBackground(Color.BLACK);

viewer.setVisible(true);

contentPane.add(viewer);

player1upJButton=new JButton();

contentPane.add(player1upJButton);

keyJButtons[KeyEvent.VK_UP]= player1upJButton;

player1upJButton.addActionListener(

new ActionListener()

{

publicvoid actionPerformed(ActionEvent event)

{

}

});

player1downJButton=new JButton();

contentPane.add(player1downJButton);

keyJButtons[KeyEvent.VK_DOWN]= player1downJButton;

player2upJButton=new JButton();

contentPane.add(player2upJButton);

keyJButtons[KeyEvent.VK_Q]= player2upJButton;

player2downJButton=new JButton();

contentPane.add(player2downJButton);

keyJButtons[KeyEvent.VK_A]= player2downJButton;

//objects

setSize(800,600);

setVisible(true);

center();

render();

}

publicvoid poop(ActionEvent event)

{

System.out.print("hello");

}

publicvoid render()

{

leftpaddle=new JLabel();

leftpaddle.setIcon(new ImageIcon("paddle.jpg"));

leftpaddle.setBounds(lpaddlex, lpaddley, paddlewidth, paddlelength);

leftpaddle.setHorizontalAlignment(JLabel.CENTER);

viewer.add(leftpaddle);

leftpaddle.setVisible(true);

rightpaddle =new JLabel();

rightpaddle.setIcon(new ImageIcon("paddle.jpg"));

rightpaddle.setBounds(rpaddlex, rpaddley, paddlewidth, paddlelength);

rightpaddle.setHorizontalAlignment(JLabel.CENTER);

viewer.add(rightpaddle);

rightpaddle.setVisible(true);

balllabel=new JLabel();

balllabel.setIcon(new ImageIcon("ball.jpg"));

balllabel.setBounds(ballx, bally, ballwidth, ballheight);

balllabel.setHorizontalAlignment(JLabel.CENTER);

viewer.add(balllabel);

balllabel.setVisible(true);

frame();

}

publicvoid center()

{

ballx=250;

bally= 200;

}

publicvoid wallcollission()

{

yvect *= -1;

render();

}

publicvoid paddlecollission()

{

xvect *= -1;

}

publicvoid checkside()

{

if ((ballx)==paddlewidth||(ballx+ballwidth==tablexmax-paddlewidth))

{

checkpaddlepos();

}

if(bally==0||bally==tableymax)

{

wallcollission();

}

}

publicvoid checkpaddlepos()

{

if((bally)<=lpaddley+paddlelength&&(bally)>=lpaddley)

{

paddlecollission();

}

if ((bally)<=rpaddley+paddlelength&&(bally)>=rpaddley)

{

paddlecollission();

}

else

render();

}

publicvoid frame()

{

ballx+=xvect;

bally+=yvect;

checkside();

}

publicstaticvoid main( String[] args )

{

work application =new work();

application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

}// end method main

}

[9808 byte] By [eboteea] at [2007-11-27 5:49:38]
# 1

> I get an error the second time the render method is called

Well, I don't know why you would call the render() method a second time it should only be called once to build the gui.

> I also am having trouble using a KeyListener for the paddle movement.

[url http://java.sun.com/docs/books/tutorial/uiswing/misc/keybinding.html]How to Use Key Bindings[/url].

Swing related questions should be posted in the Swing forum. If you search the swing forum for my KeyboardNavigation example it will show a solution.

camickra at 2007-7-12 15:36:25 > top of Java-index,Java Essentials,Java Programming...
# 2
The render method has to be called everytime the ball changes position so that it will show that it has moved.
eboteea at 2007-7-12 15:36:25 > top of Java-index,Java Essentials,Java Programming...
# 3
The problem is that the old ball JLabel doesn't disappear for each new move. How can I get around this?
eboteea at 2007-7-12 15:36:25 > top of Java-index,Java Essentials,Java Programming...
# 4
As I said earlier you don't keep calling the render() method because it keeps creating new components. To move a component around the screen you simply use:component.setLocation(...);
camickra at 2007-7-12 15:36:25 > top of Java-index,Java Essentials,Java Programming...