How can I use a image as background in a JPanel if the paint () method is already used for other purposes? (I'm tried to draw over a image in a panel).
Here is my code to draw as a pencil, but I don´t know how to add the image as background ?
@Override
public void paint(Graphics g) {
if (x >= 0 && y >= 0) {
g.setColor(Color.BLACK);
g.fillRect(x, y, 4, 4);
}
}
Thanks Diego
Suggestions:
paint(...)
method but rather use it'spaintComponent(...)
method. There are several reasons for this, one being that if you use thepaint(...)
method, then you are also responsible for drawing the JPanel's borders and child components and at risk of messing up the rendering of these guys. Also you lose Swing's automatic double buffering.g.drawImage(...)
,Hovercraft Full Of Eels gave good advice on one direction to take. Here is another.
ImageIcon
in a)JLabel
.createGraphics()
on theBufferedImage
to gain aGraphics2D
object.repaint()
on the label.E.G. as seen in this answer.