You need to create a class that extends from JComponent
(or one of its subclasses, like JPanel
in the below example) and overrides paintComponent(Graphics g)
. Here's an example:
class MyPanel extends JPanel {
private int squareX = 50;
private int squareY = 50;
private int squareW = 20;
private int squareH = 20;
protected void paintComponent(Graphics g) {
super.paintComponent(g); // do your superclass's painting routine first, and then paint on top of it.
g.setColor(Color.RED);
g.fillRect(squareX,squareY,squareW,squareH);
}
}