1

I used ImageIO.read to get image (BackgroundImage) and painted the background image like this :

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);        
    g.drawImage(background, 0, 0, backDim.width, backDim.height, null);
}

and I added some component on that JPanel (background color of the panels inserted into it is new Color(0,0,0,0) (Transparent). The background image is shown correctly when started; however, when I drag it to the bottom edge of the screen, half of them is erased. When I drag it to the left or right edge of the screen, it is re-drawn.

Ahh, and when I removed the Box.createRigidArea() gap, only the background of the titles are shown correctly. Other space is just the default color of a JPanel (light-gray).

EDIT: I added a component listener to make the screen drawn every time I move the window, but it calles repaint frequently so the computer might be overloaded. Is there any other solutions?

EDIT: The problem is that some (or most) of the drawn image is erased when I move the window (which contains the nested JPanel with a background image) to the bottom of the screen and drag it back. However, the repaint() is not called.

Final edit: Solved. It was because I did not call setOpaque(false);

minmaxavg
  • 686
  • 6
  • 21
  • 3
    `however, when I drag it to the bottom edge of the screen, half of them is erased. When I drag it to the left or right edge of the screen, it is re-drawn.` == no idea whats happens, is this JPanel in JScrollPane, for better hepl sooner post an [SSCCE](http://sscce.org/), short, runnable, compilable with image sticked into this question or by generating [BufferedImage on fly](http://stackoverflow.com/a/7944388/714968) – mKorbel Jul 03 '13 at 13:28
  • 1
    Please edit your code and question to match your current problem, it's currently impossible to understand what the problem is. – Harald K Jul 03 '13 at 13:35
  • @jinoh67 no one from edits to help somehow, in guessing statuses still, you issue is in your code ..... :-), – mKorbel Jul 03 '13 at 13:43
  • 1
    @jinoh67 : I see a slight flaw in code, i.e. the use of null as `ImageObserver` (`g.drawImage(background, 0, 0, backDim.width, backDim.height, null);`), which in this case needs to be `g.drawImage(background, 0, 0, backDim.width, backDim.height, this);` – nIcE cOw Jul 04 '13 at 01:58
  • @nIcEcOw Ah, yes. It was my mistake. – minmaxavg Jul 04 '13 at 08:55

1 Answers1

4

background color of the panels inserted into it is new Color(0,0,0,0) (Transparent)

A JPanel's opacity is true by default. If you set a transparent background color, you need to set the opacity to false - otherwise you will get painting artefacts (as you experienced)

panel.setBackground(transparentColor);
panel.setOpaque(false);
kleopatra
  • 51,061
  • 28
  • 99
  • 211