Help drawing images for pong game
Hi, my friend and I are working on our final project for the end of the year in Java. We really haven't worked outside of the topics in regular AP Computer Science A, so we need a little help. We want to create a basic Pong game with basic functionality. Right now we are trying to get the images to show up in our window, everything compiles correctly, but we are unable to get the paddle nor the ball drawn into our window. Any suggestions would be greatly appreciated. This is what we have so far:
Code:
//Ball Class
import java.awt.*;
public class Ball
{
protected int x = 400;
protected int y = 300;
protected int height = 20;
protected int width = 20;
protected int xdir = 2;
protected int ydir = 0;
public Ball()
{
}
public void draw(Graphics g)
{
g.setColor(Color.GREEN);
g.fillOval(x,y,width,height);
}
public void move()
{
x +=xdir;
y +=ydir;
if(x > 570 ){
setXDir(-8);
setYDir(randomNum());
}
if(x < 0 ){
setXDir(8);
setYDir(randomNum());
}
if(y < 2){
setYDir(8);
setXDir(randomNum());
}
if(y > 520){
setYDir(-8);
setXDir(randomNum());
}
}
public int randomNum()
{
double r = Math.random();
int myNumber = (int)(r*2f);
int num = 0;
if(myNumber==0)num = -8;
if(myNumber==1)num = 8;
return num;
}
public void resetState()
{
x =400;
y = 300;
xdir = 0;
ydir = 0;
}
public void setYDir(int y)
{
ydir = y;
}
public void setXDir(int x)
{
xdir = x;
}
public int getYDir()
{
return ydir;
}
public int getXDir()
{
return xdir;
}
}
//GamePaddle class.
import java.awt.*;
public class GamePaddle
{
protected int x = 575;
protected int y = 300;
protected int height = 110;
protected int width = 15;
int num;
public GamePaddle ()
{
}
public void moveRight()
{
if (x > 500)
{
x += 0;
}
else x+=40;
}
public void moveLeft()
{
if (x < 15)
{
x -= 0;
}
else
x-=40;
}
public Rectangle getRect()
{
return new Rectangle(x, y,width, height);
}
public void draw(Graphics block)
{
block.setColor(Color.YELLOW);
block.fillRect(x,y,width,height);
}
}
//Pong Class
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
public class Pong extends JFrame
{
private JPanel jContentPane = null;
// This is the panel of the game class
private PongGame panel = null;
private PongGame getPanel()
{
if (panel == null)
{
// This creates a new game panel
panel = new PongGame();
}
return panel;
}
/**
* This is the default constructor
*/
public Pong()
{
initialize();
// Listens for the keyboard buttons and controls
this.addKeyListener(new KeyAdapter()
{
//The button that is pressed
@Override
public void keyPressed(KeyEvent evt)
{
formKeyPressed(evt);
}
// the button that is released
@Override
public void keyReleased(KeyEvent evt)
{
formKeyReleased(evt);
}
});
}
public void formKeyPressed(KeyEvent evt)
{
panel.keyPressed(evt);
}
public void formKeyReleased(KeyEvent evt)
{
panel.keyReleased(evt);
}
// Sends which keys were pressed and release to the game class
//This creates the frame and its dimensions
private void initialize()
{
this.setResizable(false);
this.setSize(300, 300);
this.setContentPane(getJContentPane());
this.setTitle("Pong");
}
//intialize jContentPane
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)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
Pong thisClass = new Pong();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}
}
//PongGame Class
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.KeyEvent.*;
public class PongGame
{
private Ball ball;
private GamePaddle Player1;
private GamePaddle Player2;
public PongGame()
{
Ball ball = new Ball();
GamePaddle Player1 = new GamePaddle();
GamePaddle Player2 = new GamePaddle();
}
public void keyPressed(KeyEvent evt)
{
if(evt.getKeyChar() == VK_A)
Player1.moveLeft();
else if(evt.getKeyChar() == VK_D)
Player1.moveRight();
else if(evt.getKeyChar() == VK_KP_LEFT)
Player2.moveLeft();
else if(evt.getKeyChar() == VK_KP_RIGHT)
Player2.moveRight();
}
}