JComponent is not required to draw all the pixels associated with it. It can remain transparent so you can see the components behind it.
To draw all the pixels, a JPanel is required.
Of course, if you do not call the super.paintComponent(..) method in any case, they will be more or less equivalent, since then you discard the guarantee that JPanel will draw the entire panel.
In short, the difference lies in already existing methods (namely the paint component).
EDIT:
An example of a ball class implemented using JComponent would look something like this:
(This will not work with JPanel since JPanel is opaque and the ball will have a square background. You could not call super.paintComponent , but as a rule, you should always or you break what the component really is. If I pass JPanel into something, they expect it to be a JPanel, not a JPanel hacked to behave like a JComponent)
public class Ball extends JComponent { public Ball(int x, int y, int diameter) { super(); this.setLocation(x, y); this.setSize(diameter, diameter); } public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.red); g.fillOval(0, 0, width, height); } }
source share