Ello,
Im Trying to write a simple ping pong game in a java netbeans.
the problem is that i got the ball to move but it doesnt seem to move around the screen just in the same direction. heres the code:
/
*
* PingPong.java
*
* Created on 23 November 2006, 15:33
*/
/**
*
* @author rshabbir
*/
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
public class PingPongGame extends javax.swing.JFrame {
/** Creates new form PingPong */
public PingPongGame() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jButton6 = new javax.swing.JButton();
jPanel3 = new DrawingPanel();
getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("PING PONG");
jPanel1.setBackground(new java.awt.Color(0, 0, 0));
jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 255, 255)));
jLabel1.setFont(new java.awt.Font("Brush Script MT", 1, 14));
jLabel1.setForeground(new java.awt.Color(255, 255, 255));
jLabel1.setText("CONTROL PANEL");
jButton1.setFont(new java.awt.Font("Brush Script MT", 1, 12));
jButton1.setText("BAT");
jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton1MouseClicked(evt);
}
});
jButton2.setFont(new java.awt.Font("Brush Script MT", 1, 12));
jButton2.setText("SPEED");
jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton2MouseClicked(evt);
}
});
jButton5.setFont(new java.awt.Font("Brush Script MT", 1, 12));
jButton5.setText("BALL");
jButton5.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton5MouseClicked(evt);
}
});
jButton6.setFont(new java.awt.Font("Brush Script MT", 1, 12));
jButton6.setText("EXIT");
jButton6.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton6MouseClicked(evt);
}
});
org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
.add(jLabel1)
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
.add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
.add(jButton2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED))
.add(org.jdesktop.layout.GroupLayout.TRAILING, jButton1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
.add(jButton6, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(jButton5, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap(60, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.add(jLabel1)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 15, Short.MAX_VALUE)
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jButton1)
.add(jButton5))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jButton6)
.add(jButton2))
.addContainerGap(38, Short.MAX_VALUE))
);
getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 290, 140));
jPanel3.setBackground(new java.awt.Color(0, 0, 0));
org.jdesktop.layout.GroupLayout jPanel3Layout = new org.jdesktop.layout.GroupLayout(jPanel3);
jPanel3.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(
jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(0, 290, Short.MAX_VALUE)
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(0, 370, Short.MAX_VALUE)
);
getContentPane().add(jPanel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 140, 290, 370));
pack();
}// </editor-fold>
private void jButton6MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
}
private void jButton5MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
}
private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {
//
}
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new PingPongGame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
private DrawingPanel jPanel3;
// End of variables declaration
}
class DrawingPanel extends JPanel
{
int x,y,xdir=1,ydir=2 ;
int interval = 30;
int x_pos = 10;
int y_pos = 10;
int radius = 5;
int x_speed = 10;
int y_speed = 10;
int jframesize_x = 280;
int jframesize_y = 280;
//int x = 150, y = 100, r=50; // Position and radius of the circle
//int dx = 8, dy = 5; // Trajectory of circle
public void paintComponent(Graphics g)
{
Rectangle2D rect;
Ellipse2D e;
GradientPaint gp;
Graphics2D gg;
super.paintComponent(g);
setBackground(new java.awt.Color(0, 0, 0));
gg = (Graphics2D)g;
gg.setColor(Color.red);
rect = new Rectangle2D.Float(40, 30, 60, 20);
gg.draw(rect);
gg.fill(rect);
gg = (Graphics2D)g;
gg.setColor(Color.red);
rect = new Rectangle2D.Float(110, 30, 60, 20);
gg.draw(rect);
gg.fill(rect);
gg = (Graphics2D)g;
gg.setColor(Color.red);
rect = new Rectangle2D.Float(180, 30, 60, 20);
gg.draw(rect);
gg.fill(rect);
gg = (Graphics2D)g;
gg.setColor(Color.green);
rect = new Rectangle2D.Float(40, 60, 60, 20);
gg.draw(rect);
gg.fill(rect);
gg = (Graphics2D)g;
gg.setColor(Color.green);
rect = new Rectangle2D.Float(110, 60, 60, 20);
gg.draw(rect);
gg.fill(rect);
gg = (Graphics2D)g;
gg.setColor(Color.green);
rect = new Rectangle2D.Float(180, 60, 60, 20);
gg.draw(rect);
gg.fill(rect);
gg = (Graphics2D)g;
gg.setColor(Color.blue);
rect = new Rectangle2D.Float(40, 90, 60, 20);
gg.draw(rect);
gg.fill(rect);
gg = (Graphics2D)g;
gg.setColor(Color.blue);
rect = new Rectangle2D.Float(110, 90, 60, 20);
gg.draw(rect);
gg.fill(rect);
gg = (Graphics2D)g;
gg.setColor(Color.blue);
rect = new Rectangle2D.Float(180, 90, 60, 20);
gg.draw(rect);
gg.fill(rect);
gg = (Graphics2D)g;
gg.setColor(Color.yellow);
rect = new Rectangle2D.Float(40, 120, 60, 20);
gg.draw(rect);
gg.fill(rect);
gg = (Graphics2D)g;
gg.setColor(Color.yellow);
rect = new Rectangle2D.Float(110, 120, 60, 20);
gg.draw(rect);
gg.fill(rect);
gg = (Graphics2D)g;
gg.setColor(Color.yellow);
rect = new Rectangle2D.Float(180, 120, 60, 20);
gg.draw(rect);
gg.fill(rect);
gg = (Graphics2D)g;
gg.setColor(Color.magenta);
rect = new Rectangle2D.Float(110, 150, 60, 20);
gg.draw(rect);
gg.fill(rect);
gg = (Graphics2D)g;
gg.setColor(Color.magenta);
rect = new Rectangle2D.Float(110, 180, 60, 20);
gg.draw(rect);
gg.fill(rect);
gg = (Graphics2D)g;
gg.setColor(Color.magenta);
rect = new Rectangle2D.Float(110, 210, 60, 20);
gg.draw(rect);
gg.fill(rect);
gg = (Graphics2D)g;
gg.setColor(Color.pink);
rect = new Rectangle2D.Float(180, 150, 60, 20);
gg.draw(rect);
gg.fill(rect);
gg = (Graphics2D)g;
gg.setColor(Color.pink);
rect = new Rectangle2D.Float(180, 180, 60, 20);
gg.draw(rect);
gg.fill(rect);
gg = (Graphics2D)g;
gg.setColor(Color.pink);
rect = new Rectangle2D.Float(180, 210, 60, 20);
gg.draw(rect);
gg.fill(rect);
gg = (Graphics2D)g;
gg.setColor(Color.cyan);
rect = new Rectangle2D.Float(40, 150, 60, 20);
gg.draw(rect);
gg.fill(rect);
gg = (Graphics2D)g;
gg.setColor(Color.cyan);
rect = new Rectangle2D.Float(40, 180, 60, 20);
gg.draw(rect);
gg.fill(rect);
gg = (Graphics2D)g;
gg.setColor(Color.cyan);
rect = new Rectangle2D.Float(40, 210, 60, 20);
gg.draw(rect);
gg.fill(rect);
gg = (Graphics2D)g;
gg.setColor(Color.white);
rect = new Rectangle2D.Float(110, 350, 60, 10);
gg.draw(rect);
gg.fill(rect);
//g.fillOval (x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);
try{
Thread.sleep(15);
} catch(InterruptedException E){}
repaint();
x_pos++;
y_pos++;
repaint();
e = new Ellipse2D.Float(x,y,10,10);
gp = new GradientPaint(150,50, Color.white, 200,100, Color.white, true);
gg.setPaint(gp);
gg.fill(e);
try{
Thread.sleep(15);
} catch(InterruptedException E){}
repaint();
x=x+1;
y=y+1;
repaint();
if(x > jframesize_x - radius)
{
x_speed = -1;
}
else if(x < radius)
{
x_speed = +1;
}
x += x_speed;
y += x_speed;
x--;
y--;
//if ((x - r + x_pos < 0) || (x + r + x_pos > jframesize_x)) x_pos = x_pos;
//if ((y - r + y_pos < 0) || (y + r + y_pos > jframesize_y)) y_pos = y_pos;
//x += x_pos; y += y_pos;
//repaint(x-r-x_pos, y-r-x_pos, 2*r, 2*r); // repaint old position of circle
//repaint(x-r, y-r, 2*r, 2*r);
// try { Thread.sleep(50); } catch (InterruptedException e) { ; }
}
}