0

I try to draw a .PNG Image using graphic2D.drawImage() method. And I make it right, but I've one JPanel on my Frame, and when I draw my image, it appears in background. I want it to appear on the foreground, and obviously in front of my JPanel.

Carrie Kendall
  • 11,124
  • 5
  • 61
  • 81
Neewd
  • 93
  • 2
  • 13
  • What do you mean by "foreground"? I assume you are drawing it correctly, that is, inside an overriden paintComponent method. In that case, yes, specifically it is drawn on top of the panel's background but behind the border and child components. – Radiodef Oct 24 '13 at 21:06
  • i think he draws image with JFrame's paint method. – yilmazburk Oct 24 '13 at 21:12
  • 2
    IF you are drawing the image on the `JPanel` then it should appear on top of/before the `JFrame`, but it will appear beneath the content of the panel. If you want to draw the image above everything, you need to consider using the glass pane layer instead. See [How to use Root Panes](http://docs.oracle.com/javase/tutorial/uiswing/components/rootpane.html) for more details – MadProgrammer Oct 24 '13 at 22:28

2 Answers2

0

Use JLayer. (http://docs.oracle.com/javase/tutorial/uiswing/misc/jlayer.html)

Override the paint method on the LayerUI set to the JLayer and draw your .PNG image there. Add your JPanel to the JLayer.

-1

You should add one more JPanel and draw image in its paintComponent method like that.

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (yourImage != null)
            g.drawImage(yourImage, 0, 0, this);
    }

and than you can hide other JPanel and show this JPanel with setVisible() function.

yilmazburk
  • 907
  • 9
  • 17