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);
        }
    }
}

Recommended Answers

All 6 Replies

Have you gotten anything to draw yet?

Have you tried confirming the bounds of your paddle are in the visible region? A really quick test would be to change:

paddle = new Paddle(FRAME_WIDTH, FRAME_HEIGHT);

to

paddle = new Paddle(100,100);

Yes Jowierun when i change them to 100,100 and a number of other combinations it appears but when i use the 240,320 it dissapears

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);
            System.out.println( "BallPanel:"  + new Dimension(this.getWidth(),this.getHeight()));
            System.out.println( "paddle:"  + paddle.area());
        }
        /*
        public Dimension getPreferredSize() {
        return new Dimension(FRAME_WIDTH, FRAME_HEIGHT);
        }
         */
    }

run:
where are paddle? outside the BallPanel
BallPanel:java.awt.Dimension[width=234,height=295]
paddle:java.awt.Rectangle[x=2,y=310,width=20,height=10]
use commented getPreferredSize() method, do not force size of JFrame -MovingBall line 15 (for frame use pack() method.
result:
run:
BallPanel:java.awt.Dimension[width=240,height=320]
paddle:java.awt.Rectangle[x=166,y=310,width=20,height=10]

FRAME_WIDTH, FRAME_HEIGHT now have other meaning PANEL_WIDTH ,PANEL_HEIGHT

Hi manu.mani, welcome to DaniWeb.

Please check the dates on topics before responding - this one is two years old, so its unlikely to still be live. You can check the poster's history as well - in this case he hasn't been active on DaniWeb for 10 months.
Also, many of our members have English as their second (or third...) language, so "ur" or other semi-coded language will be unreasonably difficult for them. Please keep to proper English.
Thanks
J

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.