-2

I have seen a lot of questions that look like mine, and I have looked through all the answers, but I can't seem to find the answer I need.

I have a Paddle class and a Ball class for a game I am making.

In the Ball class, I have a paint method that draws and moves the ball. And in my Paddle class, I have a paint method that draws and moves the paddle. In my main class, I have a JFrame.

I would like to add my ball and paddle class to my JFrame, but the JFrame only displays the class that has been added last. I have tried adding my classes to a JPanel and then the JPanel to the JFrame, but that doesn't work either.

The code shown bellow has no errors (I may have made a typo) but simply only displays the paddle on the JFrame. (It is vice versa when I add the ball to the frame after I add the paddle to the frame). I would really appreciate any answers!

public class game {
    JFrame frame = new JFrame();
    //set size and visibility and all the other things

    Ball ball = new Ball();
    frame.add(ball); 

    Paddle paddle = new Paddle();
    frame.add(paddle);

    //repeats code underneath every 10 milliseconds using swing timer
    ball.revalidate();
    ball.repaint();

    paddle.revalidate();
    paddle.repaint();
}

My paint method in my ball class looks like this:

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.fillOval(ballx, bally, balldiamter, balldiamter);
}

My paint method in my paddle class looks like this:

@Override 
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.fillRect(paddlex, paddley, paddleLength, paddleWidth);
}
VK17
  • 1
  • 2
  • 3
    We can't really tell what's happening without seeing the code for `Ball` and `Paddle`. Please visit the [help] and also read [ask]. – Jim Garrison Jan 04 '16 at 00:35
  • Two things immediately, your `JFrame` by default is using a `BorderLayout`, so it will only manage the layout requirements of the last component added to any of the 5 available slots and you should be calling `setVisible` LAST after you have established the basic UI – MadProgrammer Jan 04 '16 at 00:39
  • 3
    As a general suggestion, you shouldn't be using components for games, the layout management and painting requirements are to completed, you focus on using custom painting instead – MadProgrammer Jan 04 '16 at 00:51
  • I can't explain animation in a comment or a Stack Overflow answer. Take a look at my [Horse Race GUI](http://java-articles.info/articles/?p=425) article to see how to create a game like Pong or Breakout. – Gilbert Le Blanc Jan 04 '16 at 04:12

1 Answers1

-2

You should make a custom Frame that you can have more control over.

class MyFrame extends JFrame{
    MyFrame(String s){
        super(s);

        Ball ball = new Ball();
        add(ball); 

        Paddle paddle = new Paddle();
        add(paddle);
        setBounds(0, 0, SCREEN_HEIGHT, SCREEN_WIDTH);
        setVisible(true);
    }
    public void paint(Graphics g){
        ball.paint(g);
        paddle.paint(g);
    }
}

This can make painting the balls and paddle a lot easier. Also, Ball and Paddle need to be extends JPanel and they need to have a paint method like this:

public void paint(Graphics g){
    g.drawRect(...); // your logic
}

Also, maybe your own paint methods has some problem.

sguan
  • 1,039
  • 10
  • 26
  • 2
    No and no. There is no need to extend directly from a `JFrame`, ever AND you should NEVER be overriding it's `paint` method, nor should you be called the `paint` methods of components which have already been added to the UI, the API will take care of that. As a matter of convention, you shouldn't be overriding `paint` of Swing components and should prefer `paintComponent` instead and you should be honouring the requirements of the paint chain and should be calling the paint methods super method – MadProgrammer Jan 04 '16 at 00:41
  • 1
    You should also prefer `pack` of `setSize` – MadProgrammer Jan 04 '16 at 00:41