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?