I would like to be able to paint Image
s onto a JFrame
, but whenever I want (not in the JFrame.paint
method.
For the project I am working on, I have a class Bullseye extends BufferedImage
, and am trying to put it onto a JFrame
:
class DrawingFrame extends JFrame {
public void drawImage(Image img, int x, int y) {
getGraphics().drawImage(img,x,y,null);
repaint();
}
}
class Main {
public static void main(String[] args) {
DrawingFrame frame = new DrawingFrame();
Bullseye bullseye = new Bullseye(20,20); //width,height
// later
frame.setVisible(true);
frame.drawImage(bullseye,10,20);
frame.drawImage(bullseye,20,20);
frame.drawImage(bullseye,30,20);
}
}
However, nothing shows up. After some research, apparently this doesn't work because the changes to the graphics
object get cleared when I repaint()
.
How can I do this? Is this even the right approach?