Welcome to Dream.In.Code
Become a Java Expert!

Join 149,478 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,622 people online right now. Registration is fast and FREE... Join Now!




Delay in Graphic Drawing

 
Reply to this topicStart new topic

Delay in Graphic Drawing

kwikness
17 May, 2007 - 08:16 AM
Post #1

New D.I.C Head
*

Joined: 22 Jan, 2007
Posts: 14


My Contributions
Hi guys, I just started messing around with Java graphics today. I'm a total n00b to this so my code is really messy. The problem with the following code is that when the application loads up, the image is not displayed until I maximize the screen. Why is this happening and how can I fix it?

CODE

import javax.imageio.*;
import javax.swing.*;
import java.awt.image.*;
import java.io.*;
import java.awt.*;

public class LoadImg extends Component
{

    BufferedImage img = null;

    public void paint(Graphics g)
    {
        g.drawImage(img, 0, 0, null);
    }

    public LoadImg() throws IOException
    {
        //load image from local directory
        img = ImageIO.read(new File("leaf1.gif"));
    }

    public static void main(String[] crap) throws IOException
    {
        //create da frame
        JFrame frame = new JFrame();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBounds(0,0,600,600);


        frame.add(new LoadImg());

    }
}//end LoadImg

User is offlineProfile CardPM
+Quote Post

dragon-slayer
RE: Delay In Graphic Drawing
17 May, 2007 - 09:17 AM
Post #2

D.I.C Head
Group Icon

Joined: 6 May, 2007
Posts: 69


Dream Kudos: 100
My Contributions
try changing the setVisible postion
so
CODE


import javax.imageio.*;
import javax.swing.*;
import java.awt.image.*;
import java.io.*;
import java.awt.*;

public class LoadImg extends Component
{

    BufferedImage img = null;

    public void paint(Graphics g)
    {
        g.drawImage(img, 0, 0, null);
    }

    public LoadImg() throws IOException
    {
        //load image from local directory
        img = ImageIO.read(new File("leaf1.gif"));
    }

    public static void main(String[] crap) throws IOException
    {
        //create da frame
        JFrame frame = new JFrame();
        
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBounds(0,0,600,600);


        frame.add(new LoadImg());
       frame.setVisible(true);//setVisible is at end

    }
}//end LoadImg

This should work.
Know it will add the image first then paint the screen

This post has been edited by dragon-slayer: 17 May, 2007 - 09:43 AM
User is offlineProfile CardPM
+Quote Post

kwikness
RE: Delay In Graphic Drawing
17 May, 2007 - 04:05 PM
Post #3

New D.I.C Head
*

Joined: 22 Jan, 2007
Posts: 14


My Contributions
Excellent, it worked great! Thanks Dragon.
User is offlineProfile CardPM
+Quote Post

dragon-slayer
RE: Delay In Graphic Drawing
18 May, 2007 - 03:48 AM
Post #4

D.I.C Head
Group Icon

Joined: 6 May, 2007
Posts: 69


Dream Kudos: 100
My Contributions
QUOTE(kwikness @ 17 May, 2007 - 05:05 PM) *

Excellent, it worked great! Thanks Dragon.


No problem it used to happen to me smile.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 03:41PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month