1

I've had this problem for a while now, searched many forums and sites (including this one), and still did not find an answer to my question.

This is my problem: I am building a visual calendar. I have a parent panel with multiple panels in it. I repaint the parent panel, and make the 3 overlaying opaque(false). The paint of the parent panel is not showing until I resize the frame (or use the buttons that overlay one of the 3, but those are left out in this example because it makes the code longer)

Anyway, here is the code, I simplified it to the problem part:

public class Calendar extends JPanel{

  public static void main(String[] args){
    JFrame frame = new JFrame();
    frame.setSize(1600,150);
    frame.add(new Calendar());
    frame.setVisible(true);
  }

  public Calendar(){
    setLayout(new GridBagLayout());
    GridBagConstraints cc = new GridBagConstraints();
    cc.weightx = 1;
    cc.weighty = 1;
    cc.gridx = 0;
    cc.fill = GridBagConstraints.BOTH;

    //Initiate Panels
    JPanel yearpanel = new JPanel();
    JPanel monthpanel = new JPanel();
    JPanel daypanel = new JPanel();

    yearpanel.setLayout(new GridBagLayout());
    monthpanel.setLayout(new GridBagLayout());
    daypanel.setLayout(new GridBagLayout());

    // Set sizes
    int width = (int) this.getPreferredSize().getWidth();
    int height = (int) (this.getPreferredSize().getHeight() / 3);
    yearpanel.setSize(width,height);
    daypanel.setSize(width,height);
    monthpanel.setSize(width,height);

    //make transparent
    yearpanel.setOpaque(false);
    monthpanel.setOpaque(false);
    daypanel.setOpaque(false);
  }        

  @Override
  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Image image = Toolkit.getDefaultToolkit().getImage("Images/CalendarBackground.jpg");
    g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), null);
  }
}

I have no idea why it does that + I could not find an answer online, only people with the same problem whose question got abandoned :/

Can anyone help me?

amaidment
  • 6,942
  • 5
  • 52
  • 88
Arnaud H
  • 881
  • 2
  • 10
  • 16
  • 1
    Would you like to amend your [SSCCE](http://www.sscce.org) - at the moment it doesn't work. You're creating empty `JPanel`s, which are never added to the top-level `JPanel` - i.e `Calendar`. – amaidment May 31 '12 at 14:54

2 Answers2

3
public void paintComponent(Graphics g)
{
    super.paintComponent(g);
    Image image = Toolkit.getDefaultToolkit().getImage("Images/CalendarBackground.jpg");
    g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), null);
}
  1. The image should not be loaded in the paint method. Instead it should be declared as a class attribute and preloaded.
  2. The Toolkit method is asynchronous, so it is even more important to use the component as the ImageObserver.

    g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), this);


Here is a working SSCCE that is something like what you seem to be attempting.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • I was about to write the same suggestions. Also, instead of using the Toolkit, you can use ImageIO.read(new File(...)). This returns a BufferedImage that can be used immediately. You should most definitely not be loading the image in the paint call. – Tony May 31 '12 at 14:58
  • @Tony Service with a smile. :) – Andrew Thompson May 31 '12 at 15:00
  • @Tony *"you can use ImageIO.read.."* Which is exactly what the linked code does. But to be on the safe side, the `ImagePanel` declares itself as the `ImageObserver`. That will get the job done even with an image that is asynchronously loaded. This helps, for example, when using an `ImageIcon` to load an *animated* GIF (which is a bit disconcerting as a BG). – Andrew Thompson May 31 '12 at 15:03
  • It's working now! Thank you all, would have never thought of this myself. :) – Arnaud H May 31 '12 at 15:42
0

check the return value of Graphics.drawImage(). I bet it's returning false, which means the image you're painting isn't fully loaded and scaled yet. Try loading the image somewhere other than your paintComponent method, like your Calendar constructor.

Dan O
  • 6,022
  • 2
  • 32
  • 50