13 Replies - 1207 Views - Last Post: 30 July 2014 - 06:03 PM Rate Topic: ***** 1 Votes

#1 program0025  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 21-May 14

Ball class for Pong won't add to JPanel

Posted 28 July 2014 - 04:49 PM

Hello I am trying to make a game of pong and am having trouble adding my ball class to a JPanel. Here is what I have so far.

package pong;
import java.awt.*;

public class Ball{

	private int xPos,yPos;
	
	public int dx = 5, dy = -5;
	
	public Ball()
	{
		
		Rectangle Ball = new Rectangle(5,5,5,5);
		
		
	}
	
	public void setPos(int x, int y){
		this.xPos = x;
		this.yPos = y;
	}
	
	public int getX(){
		return xPos;
	}
	
	public int getY(){
		return yPos;
	}
	
	public void move(){
		
		setPos(this.getX() + dx, this.getY() + dy);
	}	
	
	public void reset(){
		
		setPos(5,5);
		dx = 5;
		dy = -5;
	}
}




I want to be able to add this to a JPanel but it is not allowing me to add the ball. Here is my JPanel
package pong;


import java.awt.*;

import javax.swing.JPanel;

public class BallTestPanel 
{
	JPanel panel;
	Ball ball;
	
	public BallTestPanel()
	{
		panel =  new JPanel();
		ball = new Ball();
		panel.add(ball);
		panel.setBackground(Color.black);
		ball.setPos(5, 5);
		
		
	}
	
}


It reads
panel.add(ball);
as an error. I says that I need to change the type ball to component. Any help would be appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Ball class for Pong won't add to JPanel

#2 Gorototh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 14-July 14

Re: Ball class for Pong won't add to JPanel

Posted 28 July 2014 - 06:21 PM

http://technojeeves....java1/17-errors
Was This Post Helpful? 0
  • +
  • -

#3 program0025  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 21-May 14

Re: Ball class for Pong won't add to JPanel

Posted 28 July 2014 - 06:38 PM

View PostGorototh, on 28 July 2014 - 06:21 PM, said:


OK so the line of code has a red line under it and it says "The method add(Component) in the type Container is not applicable for the arguments (Ball)". Is this what you where looking for?
Was This Post Helpful? 0
  • +
  • -

#4 ben255  Icon User is offline

  • D.I.C Addict

Reputation: 39
  • View blog
  • Posts: 508
  • Joined: 09-September 13

Re: Ball class for Pong won't add to JPanel

Posted 28 July 2014 - 07:16 PM

you have to paint the ball

void paintTheBall(Graphics g){
   //paint the ball here
   g.drawOval(x, y, width, height);
}

Was This Post Helpful? 0
  • +
  • -

#5 program0025  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 21-May 14

Re: Ball class for Pong won't add to JPanel

Posted 28 July 2014 - 07:20 PM

View Postben255, on 28 July 2014 - 07:16 PM, said:

you have to paint the ball

void paintTheBall(Graphics g){
   //paint the ball here
   g.drawOval(x, y, width, height);
}


Is there anyway I could include that line of code in my ball class? I want the ball class to be self contained so I can just add it too the JPanel.
Was This Post Helpful? 0
  • +
  • -

#6 ben255  Icon User is offline

  • D.I.C Addict

Reputation: 39
  • View blog
  • Posts: 508
  • Joined: 09-September 13

Re: Ball class for Pong won't add to JPanel

Posted 28 July 2014 - 09:17 PM

yes you can, but you will still have to paint it on to the panel
Was This Post Helpful? 0
  • +
  • -

#7 mike73  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 250
  • View blog
  • Posts: 918
  • Joined: 24-April 10

Re: Ball class for Pong won't add to JPanel

Posted 29 July 2014 - 12:58 AM

You can stick that paintBall method in your ball class. You DON'T add the ball the JPanel. It is not component so you can't, and you also don't need to. Ball is a variable of your class, that's all.

Why not make BallTestPanel extend JPanel? Then you could have something like:
public class BallTestPanel extends JPanel
{
	Ball ball;
	
	public BallTestPanel()
	{
		ball = new Ball();
		this.add(ball);
		this.setBackground(Color.black);
		ball.setPos(5, 5);		
	}
	
	@Override
	public void paintComponent(Graphics g) {
		super.paintComponent(g); // always call super
		ball.paintBall(g);
	}
}


You should probably have a JFrame somewhere to actually display everything

This post has been edited by mike73: 29 July 2014 - 12:59 AM

Was This Post Helpful? 1
  • +
  • -

#8 program0025  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 21-May 14

Re: Ball class for Pong won't add to JPanel

Posted 29 July 2014 - 08:30 AM

View Postmike73, on 29 July 2014 - 12:58 AM, said:

You can stick that paintBall method in your ball class. You DON'T add the ball the JPanel. It is not component so you can't, and you also don't need to. Ball is a variable of your class, that's all.

Why not make BallTestPanel extend JPanel? Then you could have something like:
public class BallTestPanel extends JPanel
{
	Ball ball;
	
	public BallTestPanel()
	{
		ball = new Ball();
		this.add(ball);
		this.setBackground(Color.black);
		ball.setPos(5, 5);		
	}
	
	@Override
	public void paintComponent(Graphics g) {
		super.paintComponent(g); // always call super
		ball.paintBall(g);
	}
}


You should probably have a JFrame somewhere to actually display everything

It's still not working. It won't let me add the ball to the panel nor will it allow me to paint the ball. It won't even allow me to draw a rectangle on the panel.
package pong;


import java.awt.*;

import javax.swing.JPanel;

public class BallTestPanel extends JPanel
{
	JPanel panel;
	Ball ball;
	
	public BallTestPanel()
	{
		ball = new Ball();
		Rectangle rect = new Rectangle(5,5,5,5);
		this.add(rect);
		this.add(ball);
		this.setBackground(Color.black);
		ball.setPos(5, 5);
		
		
	}
	
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		ball.paintBall(this,g,ball.getX(),ball.getY());
		
	}
	
}


It has this part of the code underlined and says "The method paintBall(BallTestPanel, Graphics, int, int) is undefined for the type Ball"
ball.paintBall(this,g,ball.getX(),ball.getY());
. It also has these lines of code underlined and it says "The method add(Component) in the type Container is not applicable for the arguments (Ball)" and "The method add(Component) in the type Container is not applicable for the arguments (Rectangle".
this.add(rect);
		this.add(ball);

Was This Post Helpful? 0
  • +
  • -

#9 ben255  Icon User is offline

  • D.I.C Addict

Reputation: 39
  • View blog
  • Posts: 508
  • Joined: 09-September 13

Re: Ball class for Pong won't add to JPanel

Posted 29 July 2014 - 08:43 AM

here
public class BallTest extends JFrame{
	BallTest(){
		this.setSize(500, 500);
		this.setVisible(true);
		this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
		this.add(new PaintComp());
	}
	public static void main(String[] args){
		new BallTest();
	}
	class PaintComp extends JPanel{
		Ball ball;
		PaintComp(){
			this.setVisible(true);
			ball = new Ball();
		}
		public void paint(Graphics g){
			super.paint(g);
			ball.paint(g);
		}
	}
}
class Ball {
	int x = 100;
	int y = 100;
	int width = 50;
	int height = 50;
	Ball(){
	}
	public void paint(Graphics g){
		g.setColor(Color.blue);
		g.fillOval(x, y, width, height);
	}
}

Was This Post Helpful? 1
  • +
  • -

#10 program0025  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 21-May 14

Re: Ball class for Pong won't add to JPanel

Posted 29 July 2014 - 04:22 PM

View Postben255, on 29 July 2014 - 08:43 AM, said:

here
public class BallTest extends JFrame{
	BallTest(){
		this.setSize(500, 500);
		this.setVisible(true);
		this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
		this.add(new PaintComp());
	}
	public static void main(String[] args){
		new BallTest();
	}
	class PaintComp extends JPanel{
		Ball ball;
		PaintComp(){
			this.setVisible(true);
			ball = new Ball();
		}
		public void paint(Graphics g){
			super.paint(g);
			ball.paint(g);
		}
	}
}
class Ball {
	int x = 100;
	int y = 100;
	int width = 50;
	int height = 50;
	Ball(){
	}
	public void paint(Graphics g){
		g.setColor(Color.blue);
		g.fillOval(x, y, width, height);
	}
}


The code is still not working. It has underlined
public class BallTest extends JFrame
{
	BallTest()
	{
		this.setSize(500, 500);
		this.setVisible(true);
		this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
		this.add(new PaintComp());
	}
and says that "JFrame cannot be resolved to a type".
Was This Post Helpful? 0
  • +
  • -

#11 ben255  Icon User is offline

  • D.I.C Addict

Reputation: 39
  • View blog
  • Posts: 508
  • Joined: 09-September 13

Re: Ball class for Pong won't add to JPanel

Posted 29 July 2014 - 05:43 PM

have you imported the JFrame? and all other components?
Was This Post Helpful? 0
  • +
  • -

#12 program0025  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 21-May 14

Re: Ball class for Pong won't add to JPanel

Posted 29 July 2014 - 05:45 PM

View Postben255, on 29 July 2014 - 05:43 PM, said:

have you imported the JFrame? and all other components?

I imported everything from javax.swing.*; and java.awt.*;
Was This Post Helpful? 0
  • +
  • -

#13 ben255  Icon User is offline

  • D.I.C Addict

Reputation: 39
  • View blog
  • Posts: 508
  • Joined: 09-September 13

Re: Ball class for Pong won't add to JPanel

Posted 29 July 2014 - 05:57 PM

try restarting the IDE, or refreshing the project
Was This Post Helpful? 0
  • +
  • -

#14 program0025  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 21-May 14

Re: Ball class for Pong won't add to JPanel

Posted 30 July 2014 - 06:03 PM

View Postben255, on 29 July 2014 - 05:57 PM, said:

try restarting the IDE, or refreshing the project

OK its working now. Thanks!

View Postben255, on 29 July 2014 - 05:57 PM, said:

try restarting the IDE, or refreshing the project

OK its working now. Thanks!

View Postben255, on 29 July 2014 - 05:57 PM, said:

try restarting the IDE, or refreshing the project

OK its working now. Thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1