Preload image?

Posted on 2011-05-10
Last Modified: 2013-11-23
Hello everybody

I've coded my own JPanel. This panel should have a background-image that will be streched to the whole width and another image that will always appear on the right side of the panel (even if you resize it). Here's my code so far

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;
import javax.swing.*;

public class Header extends JPanel {	
	private Image headerBild = this.getToolkit().getImage(this.getClass().getResource("/images/header.jpg"));
	private Image headerBG = this.getToolkit().getImage(this.getClass().getResource("/images/headerBg.jpg"));
	public Header() {
		this.setPreferredSize(new Dimension(500, 50));

	protected void paintComponent(Graphics g) {
		int x = this.getWidth() - headerBild.getWidth(null);
		g.drawImage(headerBG, 0, 0, this.getWidth(), this.getHeight(), null, null);
		g.drawImage(headerBild, x, 0, 340, 60, null, null);
		g.fillRect(0, 0, 20, 20);

Open in new window

As you can see, I implemented my own paintComponent-method that draws the two images. For testing, I also added a fillRect, so I can see if the method was called.

Now I start my application with a JFrame that includes this panel. In the first moment, there's only the black rectangle, but no images. If I move the JFrame, the two images appear. It's like they haven't loaded the first time paintComponent is called.

Is there a way I can "preload" these images or how can I handle this?

Thank you

(I'm sorry for my bad english)
Question by:innovasoft
    LVL 47

    Accepted Solution


    check the solution at the bottomm of this trail,
    maybe youi have different issue, but still check
    LVL 1

    Author Comment

    great! That works!

    Thanks a lot!
    LVL 92

    Expert Comment


    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
    Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
    Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now