0

I am new to java and still don't fully understand how the library works.

What I want to do is, I want to have a JFrame, and inside that I want two JPanels, one for buttons, the other for an animation on it.

I have created a custom JComponent and I want to animate that on a JPanel. Here is the code:

public class PaintPart extends JPanel {        

    public PaintPart () {
        Ball b = new Ball(5,5);
        this.setPreferredSize(new Dimension(400,400));
        this.add(b);
        this.setVisible(true);
    }


    public class Ball extends JComponent {
        public Ball (int x, int y) {
            m_x = x;
            m_y = y;
        }

        @Override
        public void paintComponent(Graphics g)  {
            super.paintComponent(g);

            g.setColor(Color.yellow);

            g.fillOval(m_x, m_y, 50, 50);

        }

        public int m_x, m_y;
    }
}

I want to add the component on the Jpanel at (m_x, m_y). This approach worked when I did it directly on JFrame, but in this case, it just doesn't display anything.

Also, what is interesting is that I can perfectly add a JButton for example but with my custom component, it somewhy doesn't work. What am I doing wrong?

James Parsons
  • 6,097
  • 12
  • 68
  • 108
khajvah
  • 4,889
  • 9
  • 41
  • 63
  • I'm not sure how your class PaintPart is used, but if it is your main class, it should rather be a JFrame. See:http://stackoverflow.com/questions/13212431/jpanel-vs-jframe-in-java also: private variables are nice. – null Dec 12 '14 at 23:08
  • Okay, don't extend `Ball` from `JComponent`, this assumes that you want to play around with the layout management API, which you don't. Create a custom component which acts as a container for a `Ball` object, which knows it's location and how to draw itself, then let the ball container paint them – MadProgrammer Dec 12 '14 at 23:08
  • For [example](http://stackoverflow.com/questions/13022754/java-bouncing-ball/13022788#13022788) – MadProgrammer Dec 12 '14 at 23:09
  • The main problem you are having is, `JPanel` uses a `FlowLayout` by default. `FlowLayout` will use the preferred size of the component to make decisions about how to layout the components. A component has a default preferred size of `0x0`... – MadProgrammer Dec 12 '14 at 23:10
  • @MadProgrammer I see how you suggested approach should work. If I understand correctly, in my case, when I am overriding `paintComponent(...)`, I do not have access to parent jpanel, so I cannot put the component to wherever I want, am I correct? But if it is the case, why does it work with jframe? – khajvah Dec 12 '14 at 23:28
  • You should split the ball object into it's own class and keep info about it, x, y etc.. Then you should use the JPanel for drawing. No need to extend the ball object to a JComponent, and don't forget to call `super()` in the `PaintPart` constructor as it's extending the JPanel. – PalinDrome555 Dec 12 '14 at 23:57
  • A component added to a container goes under the control of the containers layout manager, meaning you lose control over how it is sized and positioned, by painting it yourself you gain that control with out a lot of additional work. You could implement you're own layout manager, but that's a lot of extra work – MadProgrammer Dec 13 '14 at 00:27

0 Answers0