How to draw on a JLabel?

James MV picture James MV · Dec 11, 2011 · Viewed 10.7k times · Source

I want to use the 2D Java API to draw on a JLabel that already has an image on it and then save the resulting edited picture.

I can't find any tutorials on this specific subject, does anyone have any code or references that show how to do it?

Answer

Jens Schauder picture Jens Schauder · Dec 11, 2011

override the paintComponent method of the JLabel. It should first call super.paintComponent, so you get whatever the JLabel contains, then add your own drawing code after that. Should look somewhat like this:

public void paintComponent(Graphics g){
    super.paintComponent(g)
    g.drawWhatever ...
}