Jump to content

Why can't I add a image?


Recommended Posts

Posted

Title^^

    @Override
    public void onPaint(Graphics2D g) {
    	Image bg = getImage("http://s9.postimg.org/5tbnkub0r/Namnl_st_1.png");
    	
    	g.drawImage(bg, 100, 100, null);
    }
    
    private Image getImage(String url) {
	    try
	    {
	      return ImageIO.read(new URL(url));
	    }
	    catch (IOException e) {}
	    return null;
    }

It doesn't show anything.

Posted (edited)

Title^^

    @Override
    public void onPaint(Graphics2D g) {
    	Image bg = getImage("http://s9.postimg.org/5tbnkub0r/Namnl_st_1.png");
    	
    	g.drawImage(bg, 100, 100, null);
    }
    
    private Image getImage(String url) {
	    try
	    {
	      return ImageIO.read(new URL(url));
	    }
	    catch (IOException e) {}
	    return null;
    }

It doesn't show anything.

 

What you are doing is a bad idea. onPaint is called many times per second, and in every loop you are re-getting the image from that url.

 

You should only get the image once and store it.

Edited by Explv
  • Like 2
Posted (edited)

What you are doing is a bad idea. onPaint is called many times per second, and in every loop you are re-getting the image from that url.

 

You should only get the image once and store it.

 

It didn't work sir.

    Image bg = getImage("http://s9.postimg.org/5tbnkub0r/Namnl_st_1.png");
    
    @Override
    public void onPaint(Graphics2D g) {   	
    	g.drawImage(bg, 100, 100, null);
    }
    
    private Image getImage(String url) {
	    try
	    {
	      return ImageIO.read(new URL(url));
	    }
	    catch (IOException e) {}
	    return null;
    }

 

Can anyone help me out? Appreciate it very much!

Edited by Ruzi
Posted (edited)

 

It didn't work sir.

    Image bg = getImage("http://s9.postimg.org/5tbnkub0r/Namnl_st_1.png");
    
    @Override
    public void onPaint(Graphics2D g) {   	
    	g.drawImage(bg, 100, 100, null);
    }
    
    private Image getImage(String url) {
	    try
	    {
	      return ImageIO.read(new URL(url));
	    }
	    catch (IOException e) {}
	    return null;
    }

 

 

 

You should really learn how to debug yourself. For starters, you are catching an Exception and never handling it. If you had printed out the exception by doing this:

 Image bg = getImage("http://s9.postimg.org/5tbnkub0r/Namnl_st_1.png");

    @Override
    public void onPaint(Graphics2D g){
        if(bg != null){
            g.drawImage(bg, 100, 100, null);
            System.out.println("Drawing image");
        }
    }

    private Image getImage(String url) {
        try
        {
            return ImageIO.read(new URL(url));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

You would have seen this displayed (in debug mode):

 

51af38f61d.png

 

The website returns a 403 response code (Forbidden error), most likely because it looks at the request headers and sees that you are not visiting this url from a web browser.

 

You can either reupload the image to a different site, or you can set the User-Agent in your Java code to be a browser's User-Agent like this:

Image bg = getImage("http://s9.postimg.org/5tbnkub0r/Namnl_st_1.png");

    @Override
    public void onPaint(Graphics2D g){
        if(bg != null){
            g.drawImage(bg, 100, 100, null);
            System.out.println("Drawing image");
        }
    }

    private Image getImage(String url) {
        try
        {
            URL imgUrl = new URL(url);
            URLConnection connection = imgUrl.openConnection();
            connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36");
            return ImageIO.read(connection.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

Which works.

Edited by Explv
  • Like 2
Posted

You should really learn how to debug yourself. For starters, you are catching an Exception and never handling it. If you had printed out the exception by doing this:

 Image bg = getImage("http://s9.postimg.org/5tbnkub0r/Namnl_st_1.png");

    @Override
    public void onPaint(Graphics2D g){
        if(bg != null){
            g.drawImage(bg, 100, 100, null);
            System.out.println("Drawing image");
        }
    }

    private Image getImage(String url) {
        try
        {
            return ImageIO.read(new URL(url));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

You would have seen this displayed (in debug mode):

 

51af38f61d.png

 

The website returns a 403 response code (Forbidden error), most likely because it looks at the request headers and sees that you are not visiting this url from a web browser.

 

You can either reupload the image to a different site, or you can set the User-Agent in your Java code to be a browser's User-Agent like this:

Image bg = getImage("http://s9.postimg.org/5tbnkub0r/Namnl_st_1.png");

    @Override
    public void onPaint(Graphics2D g){
        if(bg != null){
            g.drawImage(bg, 100, 100, null);
            System.out.println("Drawing image");
        }
    }

    private Image getImage(String url) {
        try
        {
            URL imgUrl = new URL(url);
            URLConnection connection = imgUrl.openConnection();
            connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36");
            return ImageIO.read(connection.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

Which works.

 

Thank you man! 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...