I'm making a paddle and ball game, a bit like pong but instead of another paddle im using the top of the frame, plan to make it into a football sort of game eventually with a bit of work, I have been able to add the ball to the frame but i cant seem to get the paddle to add to the bottom of the frame, am i missing something simple. Here are the two classes.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Paddle
{
private Color paddleColour = Color.GREEN;
private int x,y;
private int paddleWidth = 20;
private int paddleHeight = 10;
private int move = 5;
private int height, width;
/** Creates a new instance of Paddle */
public Paddle(int frameWidth, int frameHeight)
{
width = frameWidth;
height = frameHeight;
x =(int)(Math.random()*(width - paddleWidth));
y = height - paddleHeight;
}
public Rectangle area()
{
return new Rectangle(x,y, paddleWidth, paddleHeight);
}
public void draw(Graphics g)
{
g.setColor(paddleColour);
g.fillRect(x,y,paddleWidth, paddleHeight);
}
}
And here is the class where im trying to add the paddle to the frame with
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MovingBall extends JFrame
{
protected final int FRAME_WIDTH = 240;
protected final int FRAME_HEIGHT = 320;
private BallPanel myPanel;
public MovingBall (String title)
{
super(title);
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myPanel = new BallPanel();
add(myPanel);
}
private class BallPanel extends JPanel
{
private Paddle paddle;
/** Creates a new instance of BallPanel */
public BallPanel()
{
paddle = new Paddle(FRAME_WIDTH, FRAME_HEIGHT);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
paddle.draw(g);
}
}
}