Preload image?

Posted on 2011-05-10
Medium Priority
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
package zeiterfassung.gui.style;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;

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

for_yan earned 200 total points
ID: 35732856

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

Author Comment

ID: 35733161
great! That works!

Thanks a lot!
LVL 92

Expert Comment

ID: 35733175

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month14 days, 22 hours left to enroll

840 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