Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Frame not showing Image until resized

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Thanked 0 Times in 0 Posts

    Default Frame not showing Image until resized

    EDIT Solved: I just realized I'm stupid and should have used "this" instead of "null" in drawImage().

    I'm trying to create a simple window that shows a picture I have created, but it won't show the image until I have resized the window. I'm guessing paint() isn't being called properly, but I don't know. Here is the code:

    import java.awt.*;
    import javax.swing.*;
    public class start extends JFrame{
    	public static void main(String[] args) {
    		new start();			
    	private String filename = "C:\\item.gif";
    	private Image i = Toolkit.getDefaultToolkit().getImage(filename);
    	public start() {
    		setSize(400, 300);
    	public void paint(Graphics g) {
    		g.drawImage(i, 50, 50, null);

    Note that this code works fine if I draw something other than an image(i.e. fillOval())
    Last edited by gamalytical; August 13th, 2011 at 12:43 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Eastern Florida
    Thanked 2,329 Times in 2,285 Posts

    Default Re: Frame not showing Image until resized

    You need to be sure the image has been loaded before trying to use it. The getImage starts reading the image and returns before it is fully read. The ImageIO class doesn't return until the image is loaded.
    Or use the MediaTracker class to wait until the image has finished loading.

Similar Threads

  1. Showing an Image on screen
    By doobybug in forum AWT / Java Swing
    Replies: 1
    Last Post: May 10th, 2011, 08:49 AM
  2. Hiding\Showing parts of the frame
    By liron50 in forum AWT / Java Swing
    Replies: 17
    Last Post: April 29th, 2011, 01:14 PM
  3. Replies: 2
    Last Post: February 14th, 2011, 05:36 PM
  4. Can't figure out why my image isn't showing up!!
    By thrashingboy in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 8th, 2010, 06:01 PM
  5. Drawing image on JPanel from another frame
    By jeryslo in forum AWT / Java Swing
    Replies: 3
    Last Post: December 8th, 2009, 04:01 PM