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.


>> REGISTER NOW TO START POSTING


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
    Posts
    1
    Thanks
    0
    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);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setVisible(true);
    	}
     
     
    	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 12th, 2011 at 11:43 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,097
    Thanks
    52
    Thanked 2,284 Times in 2,255 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, 07:49 AM
  2. Hiding\Showing parts of the frame
    By liron50 in forum AWT / Java Swing
    Replies: 17
    Last Post: April 29th, 2011, 12:14 PM
  3. Replies: 2
    Last Post: February 14th, 2011, 04: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, 05:01 PM
  5. Drawing image on JPanel from another frame
    By jeryslo in forum AWT / Java Swing
    Replies: 3
    Last Post: December 8th, 2009, 03:01 PM