DommyCastles
DommyCastles

Reputation: 425

Java ImageIcon.GetImage hanging

I am having trouble with ImageIcon().GetImage()

The strange thing is, is that it sometimes works and shows me the window with the maps on it, and other times it doesn't. It also works on other computers flawlessly but not on mine!

I have tried everything, reinstalling Java, reinstalling IntelliJ, also disabling my firewall, but to no avail. I have also written a similar program in C# which works perfectly, which leads me to believe it isn't a permissions error. I have also tested it on a basic Windows XP system with an on board graphics card which also works perfectly.

Here is my code:

public class main {

public static void main(String[] args) {
    System.out.println("Running main..");
    try
    {
        URL url = new URL("http://maps.googleapis.com/maps/api/staticmap?center=-33.80382155278416,18.567184266922002&zoom=17&size=1024x1024&maptype=hybrid&sensor=false&format=png&key=AIzaSyCVnp9iTXRSS3ZE5FjzF7uNZavazWhLko4");
        Image img=new ImageIcon(url).getImage();
        System.out.println("INFO :"+img);
        new ImageFrame(img);

    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

public static class ImageFrame extends JFrame{

    public ImageFrame(Image img){

        setPreferredSize(new Dimension(540, 480));
        setaImg(img);
        ImagePanel somePanel = new ImagePanel(540, 480);
        add(somePanel);
        setVisible(true);
    }

    private Image aImg;

    public Image getaImg() {
        return aImg;
    }

    public void setaImg(Image aImg) {
        this.aImg = aImg;
    }

    public class ImagePanel extends JPanel{

        public ImagePanel(int width, int height){
            setPreferredSize(new Dimension(width, height));
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(getaImg(), 0, 0, null); // see javadoc for more info on the parameters
        }
    }



}

 }

I have ran it through the step through debugger and it stops at this line:

Image img=new ImageIcon(url).getImage();

But with no error, it just hangs forever.

I am totally confused as to why it isn't working on my system, and only my system. Any help would be GREATLY appreciated.

Upvotes: 0

Views: 1344

Answers (2)

MadProgrammer
MadProgrammer

Reputation: 347184

Works fine for me

enter image description here

Google's not blocking you are they? From memory you have something like 10,000 queries a day or something.

Try downloading the URL manually using the InputStream (URL.openStream()) and see if you're getting some kind of response other than an image binary.

UPDATED

After investigation, found to be a problem with Java 7 and IPv6 as documented here Downloading files using Java randomly freezes

Upvotes: 6

Andrew Thompson
Andrew Thompson

Reputation: 168815

Always start and update the GUI on the EDT. See Concurrency in Swing for more details.


g.drawImage(getaImg(), 0, 0, null); // see javadoc for more info on the parameters

That comment is very good advice, since a 4 char edit should fix the problem.

g.drawImage(getaImg(), 0, 0, this); // Observer is good for asynchronous image load

Upvotes: 3

Related Questions