Skip to content
Advertisement

JFrame Image, paint on top of it

I create a frame with an image with this code:

    JFrame f = new JFrame();
    try {
            f.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("image.jpg")))));
    } 
    catch (IOException e) {
            e.printStackTrace();
    }
    f.pack();
    f.setVisible(true);

That works fine, but now I want to paint something in that opened frame (on top of the image).

I’m quite new in Java, and I’ve allready tried to make a class that extends JFrame, with a paint(Graphics g) method in it, but it wouldn’t work, I only see the image…

Advertisement

Answer

Hmm well there are so many tutorials and without any code to go on its hard to say what you don’t know or have done wrong, look at: Java Updating Small Circles , http://www.roseindia.net/java/example/java/awt/how-to-create-circle-in-java.shtml and http://oreilly.com/catalog/java2d/chapter/ch04.html remember to not draw in the same colour as your image background or else you wont see anything no matter how hard you try 😉

Advertisement